Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | aa57e86 | 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 | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 41 | // See if the decl is deprecated. |
| 42 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | aa57e86 | 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 | fb1bb82 | 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 | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 69 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 70 | } |
| 71 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 72 | // See if this is a deleted function. |
| 73 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 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 | } |
| 79 | |
| 80 | // See if the decl is unavailable |
| 81 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 82 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 83 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | // Standard Promotions and Conversions |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 94 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 95 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 96 | QualType Ty = E->getType(); |
| 97 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 98 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 99 | if (Ty->isFunctionType()) |
| 100 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 101 | else if (Ty->isArrayType()) { |
| 102 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 103 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 104 | // type 'array of type' is converted to an expression that has type 'pointer |
| 105 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 106 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 107 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 108 | // |
| 109 | // C++ 4.2p1: |
| 110 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 111 | // T" can be converted to an rvalue of type "pointer to T". |
| 112 | // |
| 113 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 114 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 115 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 116 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 120 | /// operators (C99 6.3). The conversions of array and function types are |
| 121 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 122 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 123 | /// In these instances, this routine should *not* be called. |
| 124 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 125 | QualType Ty = Expr->getType(); |
| 126 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 127 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 128 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 129 | ImpCastExprToType(Expr, Context.IntTy); |
| 130 | else |
| 131 | DefaultFunctionArrayConversion(Expr); |
| 132 | |
| 133 | return Expr; |
| 134 | } |
| 135 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 136 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 137 | /// do not have a prototype. Arguments that have type float are promoted to |
| 138 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 139 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 140 | QualType Ty = Expr->getType(); |
| 141 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 142 | |
| 143 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 144 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 145 | if (BT->getKind() == BuiltinType::Float) |
| 146 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 147 | |
| 148 | UsualUnaryConversions(Expr); |
| 149 | } |
| 150 | |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 151 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 152 | // will warn if the resulting type is not a POD type. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 153 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 154 | DefaultArgumentPromotion(Expr); |
| 155 | |
| 156 | if (!Expr->getType()->isPODType()) { |
| 157 | Diag(Expr->getLocStart(), |
| 158 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 159 | Expr->getType() << CT; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 164 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 165 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 166 | /// routine returns the first non-arithmetic type found. The client is |
| 167 | /// responsible for emitting appropriate error diagnostics. |
| 168 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 169 | /// GCC. |
| 170 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 171 | bool isCompAssign) { |
| 172 | if (!isCompAssign) { |
| 173 | UsualUnaryConversions(lhsExpr); |
| 174 | UsualUnaryConversions(rhsExpr); |
| 175 | } |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 177 | // For conversion purposes, we ignore any qualifiers. |
| 178 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 179 | QualType lhs = |
| 180 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 181 | QualType rhs = |
| 182 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 183 | |
| 184 | // If both types are identical, no conversion is needed. |
| 185 | if (lhs == rhs) |
| 186 | return lhs; |
| 187 | |
| 188 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 189 | // The caller can deal with this (e.g. pointer + int). |
| 190 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 191 | return lhs; |
| 192 | |
| 193 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
| 194 | if (!isCompAssign) { |
| 195 | ImpCastExprToType(lhsExpr, destType); |
| 196 | ImpCastExprToType(rhsExpr, destType); |
| 197 | } |
| 198 | return destType; |
| 199 | } |
| 200 | |
| 201 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 202 | // Perform the usual unary conversions. We do this early so that |
| 203 | // integral promotions to "int" can allow us to exit early, in the |
| 204 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 205 | // qualifiers. For example, "const float" and "float" are |
| 206 | // equivalent. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 207 | if (lhs->isPromotableIntegerType()) |
| 208 | lhs = Context.IntTy; |
| 209 | else |
| 210 | lhs = lhs.getUnqualifiedType(); |
| 211 | if (rhs->isPromotableIntegerType()) |
| 212 | rhs = Context.IntTy; |
| 213 | else |
| 214 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 216 | // If both types are identical, no conversion is needed. |
| 217 | if (lhs == rhs) |
| 218 | return lhs; |
| 219 | |
| 220 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 221 | // The caller can deal with this (e.g. pointer + int). |
| 222 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 223 | return lhs; |
| 224 | |
| 225 | // At this point, we have two different arithmetic types. |
| 226 | |
| 227 | // Handle complex types first (C99 6.3.1.8p1). |
| 228 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 229 | // if we have an integer operand, the result is the complex type. |
| 230 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 231 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 232 | return lhs; |
| 233 | } |
| 234 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 235 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 236 | return rhs; |
| 237 | } |
| 238 | // This handles complex/complex, complex/float, or float/complex. |
| 239 | // When both operands are complex, the shorter operand is converted to the |
| 240 | // type of the longer, and that is the type of the result. This corresponds |
| 241 | // to what is done when combining two real floating-point operands. |
| 242 | // The fun begins when size promotion occur across type domains. |
| 243 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 244 | // floating-point type, the less precise type is converted, within it's |
| 245 | // real or complex domain, to the precision of the other type. For example, |
| 246 | // when combining a "long double" with a "double _Complex", the |
| 247 | // "double _Complex" is promoted to "long double _Complex". |
| 248 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 249 | |
| 250 | if (result > 0) { // The left side is bigger, convert rhs. |
| 251 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 252 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 253 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 254 | } |
| 255 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 256 | // domains match. This is a requirement for our implementation, C99 |
| 257 | // does not require this promotion. |
| 258 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 259 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 260 | return rhs; |
| 261 | } else { // handle "_Complex double, double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 262 | return lhs; |
| 263 | } |
| 264 | } |
| 265 | return lhs; // The domain/size match exactly. |
| 266 | } |
| 267 | // Now handle "real" floating types (i.e. float, double, long double). |
| 268 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 269 | // if we have an integer operand, the result is the real floating type. |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 270 | if (rhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 271 | // convert rhs to the lhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 272 | return lhs; |
| 273 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 274 | if (rhs->isComplexIntegerType()) { |
| 275 | // convert rhs to the complex floating point type. |
| 276 | return Context.getComplexType(lhs); |
| 277 | } |
| 278 | if (lhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | // convert lhs to the rhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 280 | return rhs; |
| 281 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 282 | if (lhs->isComplexIntegerType()) { |
| 283 | // convert lhs to the complex floating point type. |
| 284 | return Context.getComplexType(rhs); |
| 285 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 286 | // We have two real floating types, float/complex combos were handled above. |
| 287 | // Convert the smaller operand to the bigger result. |
| 288 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 289 | if (result > 0) // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 290 | return lhs; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 291 | assert(result < 0 && "illegal float comparison"); |
| 292 | return rhs; // convert the lhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 293 | } |
| 294 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 295 | // Handle GCC complex int extension. |
| 296 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 297 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 298 | |
| 299 | if (lhsComplexInt && rhsComplexInt) { |
| 300 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 301 | rhsComplexInt->getElementType()) >= 0) |
| 302 | return lhs; // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 303 | return rhs; |
| 304 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 305 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 306 | return lhs; |
| 307 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 308 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 309 | return rhs; |
| 310 | } |
| 311 | } |
| 312 | // Finally, we have two differing integer types. |
| 313 | // The rules for this case are in C99 6.3.1.8 |
| 314 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 315 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 316 | rhsSigned = rhs->isSignedIntegerType(); |
| 317 | QualType destType; |
| 318 | if (lhsSigned == rhsSigned) { |
| 319 | // Same signedness; use the higher-ranked type |
| 320 | destType = compare >= 0 ? lhs : rhs; |
| 321 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 322 | // The unsigned type has greater than or equal rank to the |
| 323 | // signed type, so use the unsigned type |
| 324 | destType = lhsSigned ? rhs : lhs; |
| 325 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 326 | // The two types are different widths; if we are here, that |
| 327 | // means the signed type is larger than the unsigned type, so |
| 328 | // use the signed type. |
| 329 | destType = lhsSigned ? lhs : rhs; |
| 330 | } else { |
| 331 | // The signed type is higher-ranked than the unsigned type, |
| 332 | // but isn't actually any bigger (like unsigned int and long |
| 333 | // on most 32-bit systems). Use the unsigned type corresponding |
| 334 | // to the signed type. |
| 335 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 336 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 337 | return destType; |
| 338 | } |
| 339 | |
| 340 | //===----------------------------------------------------------------------===// |
| 341 | // Semantic Analysis for various Expression Types |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | |
| 344 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 345 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 346 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 347 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 348 | /// multiple tokens. However, the common case is that StringToks points to one |
| 349 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 350 | /// |
| 351 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 352 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 353 | assert(NumStringToks && "Must have at least one string!"); |
| 354 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 355 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 357 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 358 | |
| 359 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 360 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 361 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 362 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 363 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 364 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 365 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 366 | |
| 367 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 368 | if (getLangOptions().CPlusPlus) |
| 369 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 370 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 371 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 372 | // the nul terminator character as well as the string length for pascal |
| 373 | // strings. |
| 374 | StrTy = Context.getConstantArrayType(StrTy, |
| 375 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 376 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 378 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 379 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 380 | Literal.GetStringLength(), |
| 381 | Literal.AnyWide, StrTy, |
| 382 | &StringTokLocs[0], |
| 383 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 386 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 387 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 388 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 389 | /// for values inside the block or for globals). |
| 390 | /// |
| 391 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 392 | /// function references, etc which is suboptimal :) and breaks |
| 393 | /// things like "integer constant expression" tests. |
| 394 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 395 | ValueDecl *VD) { |
| 396 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 397 | // we wanted to. |
| 398 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 399 | return false; |
| 400 | |
| 401 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 402 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 403 | return false; |
| 404 | |
| 405 | // If this is a reference to an extern, static, or global variable, no need to |
| 406 | // snapshot it. |
| 407 | // FIXME: What about 'const' variables in C++? |
| 408 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 409 | return Var->hasLocalStorage(); |
| 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 416 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 417 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 418 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 419 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 420 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 421 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 422 | IdentifierInfo &II, |
| 423 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 424 | const CXXScopeSpec *SS, |
| 425 | bool isAddressOfOperand) { |
| 426 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 427 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 430 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 431 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 432 | /// nested-name-specifier. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 433 | DeclRefExpr * |
| 434 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 435 | bool TypeDependent, bool ValueDependent, |
| 436 | const CXXScopeSpec *SS) { |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 437 | if (SS && !SS->isEmpty()) |
| 438 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 439 | ValueDependent, SS->getRange().getBegin()); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 440 | else |
| 441 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 444 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 445 | /// variable corresponding to the anonymous union or struct whose type |
| 446 | /// is Record. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 447 | static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 448 | assert(Record->isAnonymousStructOrUnion() && |
| 449 | "Record must be an anonymous struct or union!"); |
| 450 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 451 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 452 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 453 | // vector (which itself will be eliminated). DeclGroups might make |
| 454 | // this even better. |
| 455 | DeclContext *Ctx = Record->getDeclContext(); |
| 456 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 457 | DEnd = Ctx->decls_end(); |
| 458 | D != DEnd; ++D) { |
| 459 | if (*D == Record) { |
| 460 | // The object for the anonymous struct/union directly |
| 461 | // follows its type in the list of declarations. |
| 462 | ++D; |
| 463 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 464 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 465 | return *D; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | assert(false && "Missing object for anonymous record"); |
| 470 | return 0; |
| 471 | } |
| 472 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 473 | Sema::OwningExprResult |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 474 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 475 | FieldDecl *Field, |
| 476 | Expr *BaseObjectExpr, |
| 477 | SourceLocation OpLoc) { |
| 478 | assert(Field->getDeclContext()->isRecord() && |
| 479 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 480 | && "Field must be stored inside an anonymous struct or union"); |
| 481 | |
| 482 | // Construct the sequence of field member references |
| 483 | // we'll have to perform to get to the field in the anonymous |
| 484 | // union/struct. The list of members is built from the field |
| 485 | // outward, so traverse it backwards to go from an object in |
| 486 | // the current context to the field we found. |
| 487 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 488 | AnonFields.push_back(Field); |
| 489 | VarDecl *BaseObject = 0; |
| 490 | DeclContext *Ctx = Field->getDeclContext(); |
| 491 | do { |
| 492 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 493 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 494 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 495 | AnonFields.push_back(AnonField); |
| 496 | else { |
| 497 | BaseObject = cast<VarDecl>(AnonObject); |
| 498 | break; |
| 499 | } |
| 500 | Ctx = Ctx->getParent(); |
| 501 | } while (Ctx->isRecord() && |
| 502 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 503 | |
| 504 | // Build the expression that refers to the base object, from |
| 505 | // which we will build a sequence of member references to each |
| 506 | // of the anonymous union objects and, eventually, the field we |
| 507 | // found via name lookup. |
| 508 | bool BaseObjectIsPointer = false; |
| 509 | unsigned ExtraQuals = 0; |
| 510 | if (BaseObject) { |
| 511 | // BaseObject is an anonymous struct/union variable (and is, |
| 512 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 513 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 514 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 515 | SourceLocation()); |
| 516 | ExtraQuals |
| 517 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 518 | } else if (BaseObjectExpr) { |
| 519 | // The caller provided the base object expression. Determine |
| 520 | // whether its a pointer and whether it adds any qualifiers to the |
| 521 | // anonymous struct/union fields we're looking into. |
| 522 | QualType ObjectType = BaseObjectExpr->getType(); |
| 523 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 524 | BaseObjectIsPointer = true; |
| 525 | ObjectType = ObjectPtr->getPointeeType(); |
| 526 | } |
| 527 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 528 | } else { |
| 529 | // We've found a member of an anonymous struct/union that is |
| 530 | // inside a non-anonymous struct/union, so in a well-formed |
| 531 | // program our base object expression is "this". |
| 532 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 533 | if (!MD->isStatic()) { |
| 534 | QualType AnonFieldType |
| 535 | = Context.getTagDeclType( |
| 536 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 537 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 538 | if ((Context.getCanonicalType(AnonFieldType) |
| 539 | == Context.getCanonicalType(ThisType)) || |
| 540 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 541 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 542 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 543 | MD->getThisType(Context)); |
| 544 | BaseObjectIsPointer = true; |
| 545 | } |
| 546 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 547 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 548 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 549 | } |
| 550 | ExtraQuals = MD->getTypeQualifiers(); |
| 551 | } |
| 552 | |
| 553 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 554 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 555 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // Build the implicit member references to the field of the |
| 559 | // anonymous struct/union. |
| 560 | Expr *Result = BaseObjectExpr; |
| 561 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 562 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 563 | FI != FIEnd; ++FI) { |
| 564 | QualType MemberType = (*FI)->getType(); |
| 565 | if (!(*FI)->isMutable()) { |
| 566 | unsigned combinedQualifiers |
| 567 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 568 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 569 | } |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 570 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 571 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 572 | BaseObjectIsPointer = false; |
| 573 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
| 574 | OpLoc = SourceLocation(); |
| 575 | } |
| 576 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 577 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 580 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 581 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 582 | /// performs lookup on that name and returns an expression that refers |
| 583 | /// to that name. This routine isn't directly called from the parser, |
| 584 | /// because the parser doesn't know about DeclarationName. Rather, |
| 585 | /// this routine is called by ActOnIdentifierExpr, |
| 586 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 587 | /// which form the DeclarationName from the corresponding syntactic |
| 588 | /// forms. |
| 589 | /// |
| 590 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 591 | /// function call context. LookupCtx is only used for a C++ |
| 592 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 593 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 594 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 595 | /// isAddressOfOperand means that this expression is the direct operand |
| 596 | /// of an address-of operator. This matters because this is the only |
| 597 | /// situation where a qualified name referencing a non-static member may |
| 598 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 599 | Sema::OwningExprResult |
| 600 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 601 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 602 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 603 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 604 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 605 | if (SS && SS->isInvalid()) |
| 606 | return ExprError(); |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 607 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 608 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 610 | NamedDecl *D = 0; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 611 | if (Lookup.isAmbiguous()) { |
| 612 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 613 | SS && SS->isSet() ? SS->getRange() |
| 614 | : SourceRange()); |
| 615 | return ExprError(); |
| 616 | } else |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 617 | D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 618 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 619 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 620 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 621 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 622 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 623 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 624 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 625 | // found a decl, but that decl is outside the current method (i.e. a global |
| 626 | // variable). In these two cases, we do a lookup for an ivar with this |
| 627 | // name, if the lookup suceeds, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 628 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 629 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 630 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 631 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 632 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 633 | return ExprError(); |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 635 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 636 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 637 | IdentifierInfo &II = Context.Idents.get("self"); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 638 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 639 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 640 | Loc, static_cast<Expr*>(SelfExpr.release()), |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 641 | true, true); |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 642 | Context.setFieldDecl(IFace, IV, MRef); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 643 | return Owned(MRef); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 646 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 647 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 648 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 649 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 650 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 652 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 653 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 654 | // Determine whether this name might be a candidate for |
| 655 | // argument-dependent lookup. |
| 656 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 657 | HasTrailingLParen; |
| 658 | |
| 659 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 660 | // We've seen something of the form |
| 661 | // |
| 662 | // identifier( |
| 663 | // |
| 664 | // and we did not find any entity by the name |
| 665 | // "identifier". However, this identifier is still subject to |
| 666 | // argument-dependent lookup, so keep track of the name. |
| 667 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 668 | Context.OverloadTy, |
| 669 | Loc)); |
| 670 | } |
| 671 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 672 | if (D == 0) { |
| 673 | // Otherwise, this could be an implicitly declared function reference (legal |
| 674 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 675 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 676 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 677 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 678 | else { |
| 679 | // If this name wasn't predeclared and if this is not a function call, |
| 680 | // diagnose the problem. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 681 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 682 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 683 | << Name << SS->getRange()); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 684 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 685 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 686 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 687 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 688 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 689 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 692 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 693 | // If this is an expression of the form &Class::member, don't build an |
| 694 | // implicit member ref, because we want a pointer to the member in general, |
| 695 | // not any specific instance's member. |
| 696 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 697 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 698 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 699 | QualType DType; |
| 700 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 701 | DType = FD->getType().getNonReferenceType(); |
| 702 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 703 | DType = Method->getType(); |
| 704 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 705 | DType = Context.OverloadTy; |
| 706 | } |
| 707 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 708 | if (!DType.isNull()) { |
| 709 | // The pointer is type- and value-dependent if it points into something |
| 710 | // dependent. |
| 711 | bool Dependent = false; |
| 712 | for (; DC; DC = DC->getParent()) { |
| 713 | // FIXME: could stop early at namespace scope. |
| 714 | if (DC->isRecord()) { |
| 715 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 716 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 717 | Dependent = true; |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 722 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 727 | // We may have found a field within an anonymous union or struct |
| 728 | // (C++ [class.union]). |
| 729 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 730 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 731 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 733 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 734 | if (!MD->isStatic()) { |
| 735 | // C++ [class.mfct.nonstatic]p2: |
| 736 | // [...] if name lookup (3.4.1) resolves the name in the |
| 737 | // id-expression to a nonstatic nontype member of class X or of |
| 738 | // a base class of X, the id-expression is transformed into a |
| 739 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 740 | // as the postfix-expression to the left of the '.' operator. |
| 741 | DeclContext *Ctx = 0; |
| 742 | QualType MemberType; |
| 743 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 744 | Ctx = FD->getDeclContext(); |
| 745 | MemberType = FD->getType(); |
| 746 | |
| 747 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 748 | MemberType = RefType->getPointeeType(); |
| 749 | else if (!FD->isMutable()) { |
| 750 | unsigned combinedQualifiers |
| 751 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 752 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 753 | } |
| 754 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 755 | if (!Method->isStatic()) { |
| 756 | Ctx = Method->getParent(); |
| 757 | MemberType = Method->getType(); |
| 758 | } |
| 759 | } else if (OverloadedFunctionDecl *Ovl |
| 760 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 761 | for (OverloadedFunctionDecl::function_iterator |
| 762 | Func = Ovl->function_begin(), |
| 763 | FuncEnd = Ovl->function_end(); |
| 764 | Func != FuncEnd; ++Func) { |
| 765 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 766 | if (!DMethod->isStatic()) { |
| 767 | Ctx = Ovl->getDeclContext(); |
| 768 | MemberType = Context.OverloadTy; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 773 | |
| 774 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 775 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 776 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 777 | if ((Context.getCanonicalType(CtxType) |
| 778 | == Context.getCanonicalType(ThisType)) || |
| 779 | IsDerivedFrom(ThisType, CtxType)) { |
| 780 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 781 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 782 | MD->getThisType(Context)); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 783 | return Owned(new (Context) MemberExpr(This, true, D, |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 784 | SourceLocation(), MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 790 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 791 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 792 | if (MD->isStatic()) |
| 793 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 794 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 795 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 798 | // Any other ways we could have found the field in a well-formed |
| 799 | // program would have been turned into implicit member expressions |
| 800 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 801 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 802 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 803 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 805 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 806 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 807 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 808 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 809 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 810 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 811 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 812 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 813 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 814 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 815 | false, false, SS)); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 816 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 817 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 818 | false, false, SS)); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 819 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 820 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 821 | // Check whether this declaration can be used. Note that we suppress |
| 822 | // this check when we're going to perform argument-dependent lookup |
| 823 | // on this function name, because this might not be the function |
| 824 | // that overload resolution actually selects. |
| 825 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 826 | return ExprError(); |
| 827 | |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 828 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 829 | // Warn about constructs like: |
| 830 | // if (void *X = foo()) { ... } else { X }. |
| 831 | // In the else block, the pointer is always false. |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 832 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 833 | Scope *CheckS = S; |
| 834 | while (CheckS) { |
| 835 | if (CheckS->isWithinElse() && |
| 836 | CheckS->getControlParent()->isDeclScope(Var)) { |
| 837 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 838 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 839 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 840 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 841 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 842 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | |
| 846 | // Move up one more control parent to check again. |
| 847 | CheckS = CheckS->getControlParent(); |
| 848 | if (CheckS) |
| 849 | CheckS = CheckS->getParent(); |
| 850 | } |
| 851 | } |
| 852 | } |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 853 | |
| 854 | // Only create DeclRefExpr's for valid Decl's. |
| 855 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 856 | return ExprError(); |
| 857 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 858 | // If the identifier reference is inside a block, and it refers to a value |
| 859 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 860 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 861 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 862 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 863 | // We do not do this for things like enum constants, global variables, etc, |
| 864 | // as they do not get snapshotted. |
| 865 | // |
| 866 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 867 | // The BlocksAttr indicates the variable is bound by-reference. |
| 868 | if (VD->getAttr<BlocksAttr>()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 869 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 870 | VD->getType().getNonReferenceType(), Loc, true)); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 871 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 872 | // Variable will be bound by-copy, make it const within the closure. |
| 873 | VD->getType().addConst(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 874 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 875 | VD->getType().getNonReferenceType(), Loc, false)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 876 | } |
| 877 | // If this reference is not in a block or if the referenced variable is |
| 878 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 880 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 881 | bool ValueDependent = false; |
| 882 | if (getLangOptions().CPlusPlus) { |
| 883 | // C++ [temp.dep.expr]p3: |
| 884 | // An id-expression is type-dependent if it contains: |
| 885 | // - an identifier that was declared with a dependent type, |
| 886 | if (VD->getType()->isDependentType()) |
| 887 | TypeDependent = true; |
| 888 | // - FIXME: a template-id that is dependent, |
| 889 | // - a conversion-function-id that specifies a dependent type, |
| 890 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 891 | Name.getCXXNameType()->isDependentType()) |
| 892 | TypeDependent = true; |
| 893 | // - a nested-name-specifier that contains a class-name that |
| 894 | // names a dependent type. |
| 895 | else if (SS && !SS->isEmpty()) { |
| 896 | for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 897 | DC; DC = DC->getParent()) { |
| 898 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 899 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 900 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 901 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 902 | TypeDependent = true; |
| 903 | break; |
| 904 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 909 | // C++ [temp.dep.constexpr]p2: |
| 910 | // |
| 911 | // An identifier is value-dependent if it is: |
| 912 | // - a name declared with a dependent type, |
| 913 | if (TypeDependent) |
| 914 | ValueDependent = true; |
| 915 | // - the name of a non-type template parameter, |
| 916 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 917 | ValueDependent = true; |
| 918 | // - a constant with integral or enumeration type and is |
| 919 | // initialized with an expression that is value-dependent |
| 920 | // (FIXME!). |
| 921 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 922 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 923 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 924 | TypeDependent, ValueDependent, SS)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 927 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 928 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 929 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 930 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 931 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 932 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 933 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 934 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 935 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 936 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 937 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 938 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 939 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 940 | unsigned Length; |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 941 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 942 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 943 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 944 | Length = MD->getSynthesizedMethodSize(); |
| 945 | else { |
| 946 | Diag(Loc, diag::ext_predef_outside_function); |
| 947 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 948 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 949 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 950 | |
| 951 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 952 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 953 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 954 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 955 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 958 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 959 | llvm::SmallString<16> CharBuffer; |
| 960 | CharBuffer.resize(Tok.getLength()); |
| 961 | const char *ThisTokBegin = &CharBuffer[0]; |
| 962 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 963 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 964 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 965 | Tok.getLocation(), PP); |
| 966 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 967 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 968 | |
| 969 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 970 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 971 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 972 | Literal.isWide(), |
| 973 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 976 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 977 | // Fast path for a single digit (which is quite common). A single digit |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 978 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 979 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 980 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 981 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 982 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 983 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 984 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 986 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 987 | // Add padding so that NumericLiteralParser can overread by one character. |
| 988 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 989 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 991 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 992 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 994 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 995 | Tok.getLocation(), PP); |
| 996 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 997 | return ExprError(); |
| 998 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 999 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1001 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1002 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1003 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1004 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1005 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1006 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1007 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1008 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1009 | |
| 1010 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1011 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1012 | // isExact will be set by GetFloatValue(). |
| 1013 | bool isExact = false; |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1014 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1015 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1017 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1018 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1019 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1020 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1021 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1022 | // long long is a C99 feature. |
| 1023 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1024 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1025 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1026 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1027 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1028 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1030 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1031 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1032 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1033 | Ty = Context.UnsignedLongLongTy; |
| 1034 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1035 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1036 | } else { |
| 1037 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1038 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1040 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1041 | // be an unsigned int. |
| 1042 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1043 | |
| 1044 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1045 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1046 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1047 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1048 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1050 | // Does it fit in a unsigned int? |
| 1051 | if (ResultVal.isIntN(IntSize)) { |
| 1052 | // Does it fit in a signed int? |
| 1053 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1054 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1055 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1056 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1057 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1058 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1059 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1061 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1062 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1063 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1064 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1065 | // Does it fit in a unsigned long? |
| 1066 | if (ResultVal.isIntN(LongSize)) { |
| 1067 | // Does it fit in a signed long? |
| 1068 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1069 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1070 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1071 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1072 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1073 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1076 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1077 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1078 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1080 | // Does it fit in a unsigned long long? |
| 1081 | if (ResultVal.isIntN(LongLongSize)) { |
| 1082 | // Does it fit in a signed long long? |
| 1083 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1084 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1085 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1086 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1087 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1091 | // If we still couldn't decide a type, we probably have something that |
| 1092 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1093 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1094 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1095 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1096 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1097 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1098 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1099 | if (ResultVal.getBitWidth() != Width) |
| 1100 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1101 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1102 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1103 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1105 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1106 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1107 | Res = new (Context) ImaginaryLiteral(Res, |
| 1108 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1109 | |
| 1110 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1113 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1114 | SourceLocation R, ExprArg Val) { |
| 1115 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1116 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1117 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1121 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1122 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 1123 | SourceLocation OpLoc, |
| 1124 | const SourceRange &ExprRange, |
| 1125 | bool isSizeof) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1126 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1127 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1128 | // alignof(function) is allowed. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1129 | if (isSizeof) |
| 1130 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1135 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1136 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1140 | return DiagnoseIncompleteType(OpLoc, exprType, |
| 1141 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1142 | diag::err_alignof_incomplete_type, |
| 1143 | ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1146 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1147 | const SourceRange &ExprRange) { |
| 1148 | E = E->IgnoreParens(); |
| 1149 | |
| 1150 | // alignof decl is always ok. |
| 1151 | if (isa<DeclRefExpr>(E)) |
| 1152 | return false; |
| 1153 | |
| 1154 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1155 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1156 | if (FD->isBitField()) { |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1157 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1158 | return true; |
| 1159 | } |
| 1160 | // Other fields are ok. |
| 1161 | return false; |
| 1162 | } |
| 1163 | } |
| 1164 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1165 | } |
| 1166 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1167 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1168 | /// the same for @c alignof and @c __alignof |
| 1169 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1170 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1171 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1172 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1173 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1174 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1175 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1176 | QualType ArgTy; |
| 1177 | SourceRange Range; |
| 1178 | if (isType) { |
| 1179 | ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1180 | Range = ArgRange; |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Verify that the operand is valid. |
| 1183 | if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) |
| 1184 | return ExprError(); |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1185 | } else { |
| 1186 | // Get the end location. |
| 1187 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1188 | Range = ArgEx->getSourceRange(); |
| 1189 | ArgTy = ArgEx->getType(); |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1190 | |
| 1191 | // Verify that the operand is valid. |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1192 | bool isInvalid; |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1193 | if (!isSizeof) { |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1194 | isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range); |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1195 | } else if (ArgEx->isBitField()) { // C99 6.5.3.4p1. |
| 1196 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1197 | isInvalid = true; |
| 1198 | } else { |
| 1199 | isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true); |
| 1200 | } |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (isInvalid) { |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1203 | DeleteExpr(ArgEx); |
| 1204 | return ExprError(); |
| 1205 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1208 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1209 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeof, isType, TyOrEx, |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1210 | Context.getSizeType(), OpLoc, |
| 1211 | Range.getEnd())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1214 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1215 | DefaultFunctionArrayConversion(V); |
| 1216 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1217 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1218 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1219 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Otherwise they pass through real integer and floating point types here. |
| 1222 | if (V->getType()->isArithmeticType()) |
| 1223 | return V->getType(); |
| 1224 | |
| 1225 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1226 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1227 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1228 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1232 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1233 | Action::OwningExprResult |
| 1234 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1235 | tok::TokenKind Kind, ExprArg Input) { |
| 1236 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1238 | UnaryOperator::Opcode Opc; |
| 1239 | switch (Kind) { |
| 1240 | default: assert(0 && "Unknown unary op!"); |
| 1241 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1242 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1243 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1245 | if (getLangOptions().CPlusPlus && |
| 1246 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1247 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1248 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1249 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1250 | |
| 1251 | // C++ [over.inc]p1: |
| 1252 | // |
| 1253 | // [...] If the function is a member function with one |
| 1254 | // parameter (which shall be of type int) or a non-member |
| 1255 | // function with two parameters (the second of which shall be |
| 1256 | // of type int), it defines the postfix increment operator ++ |
| 1257 | // for objects of that type. When the postfix increment is |
| 1258 | // called as a result of using the ++ operator, the int |
| 1259 | // argument will have value zero. |
| 1260 | Expr *Args[2] = { |
| 1261 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1262 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1263 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1264 | }; |
| 1265 | |
| 1266 | // Build the candidate set for overloading |
| 1267 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1268 | if (AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet)) |
| 1269 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1270 | |
| 1271 | // Perform overload resolution. |
| 1272 | OverloadCandidateSet::iterator Best; |
| 1273 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1274 | case OR_Success: { |
| 1275 | // We found a built-in operator or an overloaded operator. |
| 1276 | FunctionDecl *FnDecl = Best->Function; |
| 1277 | |
| 1278 | if (FnDecl) { |
| 1279 | // We matched an overloaded operator. Build a call to that |
| 1280 | // operator. |
| 1281 | |
| 1282 | // Convert the arguments. |
| 1283 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1284 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1285 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1286 | } else { |
| 1287 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1288 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1289 | FnDecl->getParamDecl(0)->getType(), |
| 1290 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1291 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1295 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1296 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1297 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1299 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1300 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1301 | SourceLocation()); |
| 1302 | UsualUnaryConversions(FnExpr); |
| 1303 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1304 | Input.release(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1305 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
| 1306 | ResultTy, OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1307 | } else { |
| 1308 | // We matched a built-in operator. Convert the arguments, then |
| 1309 | // break out so that we will build the appropriate built-in |
| 1310 | // operator node. |
| 1311 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1312 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1313 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1314 | |
| 1315 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1316 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | case OR_No_Viable_Function: |
| 1320 | // No viable function; fall through to handling this as a |
| 1321 | // built-in operator, which will produce an error message for us. |
| 1322 | break; |
| 1323 | |
| 1324 | case OR_Ambiguous: |
| 1325 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1326 | << UnaryOperator::getOpcodeStr(Opc) |
| 1327 | << Arg->getSourceRange(); |
| 1328 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1329 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1330 | |
| 1331 | case OR_Deleted: |
| 1332 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1333 | << Best->Function->isDeleted() |
| 1334 | << UnaryOperator::getOpcodeStr(Opc) |
| 1335 | << Arg->getSourceRange(); |
| 1336 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1337 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // Either we found no viable overloaded operator or we matched a |
| 1341 | // built-in operator. In either case, fall through to trying to |
| 1342 | // build a built-in operation. |
| 1343 | } |
| 1344 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1345 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1346 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1347 | if (result.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1348 | return ExprError(); |
| 1349 | Input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1350 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1353 | Action::OwningExprResult |
| 1354 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1355 | ExprArg Idx, SourceLocation RLoc) { |
| 1356 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1357 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1358 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1359 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1360 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1361 | LHSExp->getType()->isEnumeralType() || |
| 1362 | RHSExp->getType()->isRecordType() || |
| 1363 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1364 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1365 | // to the candidate set. |
| 1366 | OverloadCandidateSet CandidateSet; |
| 1367 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1368 | if (AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1369 | SourceRange(LLoc, RLoc))) |
| 1370 | return ExprError(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1372 | // Perform overload resolution. |
| 1373 | OverloadCandidateSet::iterator Best; |
| 1374 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1375 | case OR_Success: { |
| 1376 | // We found a built-in operator or an overloaded operator. |
| 1377 | FunctionDecl *FnDecl = Best->Function; |
| 1378 | |
| 1379 | if (FnDecl) { |
| 1380 | // We matched an overloaded operator. Build a call to that |
| 1381 | // operator. |
| 1382 | |
| 1383 | // Convert the arguments. |
| 1384 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1385 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1386 | PerformCopyInitialization(RHSExp, |
| 1387 | FnDecl->getParamDecl(0)->getType(), |
| 1388 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1389 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1390 | } else { |
| 1391 | // Convert the arguments. |
| 1392 | if (PerformCopyInitialization(LHSExp, |
| 1393 | FnDecl->getParamDecl(0)->getType(), |
| 1394 | "passing") || |
| 1395 | PerformCopyInitialization(RHSExp, |
| 1396 | FnDecl->getParamDecl(1)->getType(), |
| 1397 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1398 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1402 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1403 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1404 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1406 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1407 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1408 | SourceLocation()); |
| 1409 | UsualUnaryConversions(FnExpr); |
| 1410 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1411 | Base.release(); |
| 1412 | Idx.release(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1413 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1414 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1415 | } else { |
| 1416 | // We matched a built-in operator. Convert the arguments, then |
| 1417 | // break out so that we will build the appropriate built-in |
| 1418 | // operator node. |
| 1419 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1420 | "passing") || |
| 1421 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1422 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1423 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1424 | |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | case OR_No_Viable_Function: |
| 1430 | // No viable function; fall through to handling this as a |
| 1431 | // built-in operator, which will produce an error message for us. |
| 1432 | break; |
| 1433 | |
| 1434 | case OR_Ambiguous: |
| 1435 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1436 | << "[]" |
| 1437 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1438 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1439 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1440 | |
| 1441 | case OR_Deleted: |
| 1442 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1443 | << Best->Function->isDeleted() |
| 1444 | << "[]" |
| 1445 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1446 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1447 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | // Either we found no viable overloaded operator or we matched a |
| 1451 | // built-in operator. In either case, fall through to trying to |
| 1452 | // build a built-in operation. |
| 1453 | } |
| 1454 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1455 | // Perform default conversions. |
| 1456 | DefaultFunctionArrayConversion(LHSExp); |
| 1457 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1459 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1460 | |
| 1461 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1462 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1463 | // in the subscript position. As a result, we need to derive the array base |
| 1464 | // and index from the expression types. |
| 1465 | Expr *BaseExpr, *IndexExpr; |
| 1466 | QualType ResultType; |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1467 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1468 | BaseExpr = LHSExp; |
| 1469 | IndexExpr = RHSExp; |
| 1470 | // FIXME: need to deal with const... |
| 1471 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1472 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1473 | // Handle the uncommon case of "123[Ptr]". |
| 1474 | BaseExpr = RHSExp; |
| 1475 | IndexExpr = LHSExp; |
| 1476 | // FIXME: need to deal with const... |
| 1477 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1478 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1479 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1480 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1482 | // FIXME: need to deal with const... |
| 1483 | ResultType = VTy->getElementType(); |
| 1484 | } else { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1485 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1486 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1487 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1488 | // C99 6.5.2.1p1 |
| 1489 | if (!IndexExpr->getType()->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1490 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1491 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1492 | |
| 1493 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 1494 | // the following check catches trying to index a pointer to a function (e.g. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1495 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 1496 | // objects in C99. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1497 | if (!ResultType->isObjectType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1498 | return ExprError(Diag(BaseExpr->getLocStart(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1499 | diag::err_typecheck_subscript_not_object) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1500 | << BaseExpr->getType() << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1501 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1502 | Base.release(); |
| 1503 | Idx.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1504 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1505 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1508 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1509 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1510 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1511 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1512 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1513 | // The vector accessor can't exceed the number of elements. |
| 1514 | const char *compStr = CompName.getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1515 | |
| 1516 | // This flag determines whether or not the component is one of the four |
| 1517 | // special names that indicate a subset of exactly half the elements are |
| 1518 | // to be selected. |
| 1519 | bool HalvingSwizzle = false; |
| 1520 | |
| 1521 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1522 | // indicating that it is a string of hex values to be used as vector indices. |
| 1523 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Check that we've found one of the special components, or that the component |
| 1526 | // names must come from the same set. |
| 1527 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1528 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1529 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1530 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1531 | do |
| 1532 | compStr++; |
| 1533 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1534 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1535 | do |
| 1536 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1537 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1538 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1539 | |
| 1540 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1541 | // We didn't get to the end of the string. This means the component names |
| 1542 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1543 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1544 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1545 | return QualType(); |
| 1546 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Ensure no component accessor exceeds the width of the vector type it |
| 1549 | // operates on. |
| 1550 | if (!HalvingSwizzle) { |
| 1551 | compStr = CompName.getName(); |
| 1552 | |
| 1553 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1554 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1555 | |
| 1556 | while (*compStr) { |
| 1557 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1558 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1559 | << baseType << SourceRange(CompLoc); |
| 1560 | return QualType(); |
| 1561 | } |
| 1562 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1563 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1564 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1565 | // If this is a halving swizzle, verify that the base type has an even |
| 1566 | // number of elements. |
| 1567 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1568 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1569 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1570 | return QualType(); |
| 1571 | } |
| 1572 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1573 | // The component accessor looks fine - now we need to compute the actual type. |
| 1574 | // The vector type is implied by the component accessor. For example, |
| 1575 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1576 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1577 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1578 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1579 | : CompName.getLength(); |
| 1580 | if (HexSwizzle) |
| 1581 | CompSize--; |
| 1582 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1583 | if (CompSize == 1) |
| 1584 | return vecType->getElementType(); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1585 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1586 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1587 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1588 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1589 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1590 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1591 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1592 | } |
| 1593 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1596 | |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1597 | /// constructSetterName - Return the setter name for the given |
| 1598 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 1599 | /// has been capitalized. |
| 1600 | // FIXME: Merge with same routine in Parser. But where should this |
| 1601 | // live? |
| 1602 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 1603 | const IdentifierInfo *Name) { |
| 1604 | llvm::SmallString<100> SelectorName; |
| 1605 | SelectorName = "set"; |
| 1606 | SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); |
| 1607 | SelectorName[3] = toupper(SelectorName[3]); |
| 1608 | return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); |
| 1609 | } |
| 1610 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1611 | Action::OwningExprResult |
| 1612 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1613 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 1614 | IdentifierInfo &Member) { |
| 1615 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1616 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Perform default conversions. |
| 1619 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1620 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1621 | QualType BaseType = BaseExpr->getType(); |
| 1622 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1624 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1625 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1626 | if (OpKind == tok::arrow) { |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1627 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1628 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1629 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1630 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1631 | MemberLoc, Member)); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1632 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1633 | return ExprError(Diag(MemberLoc, |
| 1634 | diag::err_typecheck_member_reference_arrow) |
| 1635 | << BaseType << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1636 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1638 | // Handle field access to simple records. This also handles access to fields |
| 1639 | // of the ObjC 'id' struct. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1640 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1641 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1642 | if (DiagnoseIncompleteType(OpLoc, BaseType, |
| 1643 | diag::err_typecheck_incomplete_tag, |
| 1644 | BaseExpr->getSourceRange())) |
| 1645 | return ExprError(); |
| 1646 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1647 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1648 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1649 | // than this. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1650 | LookupResult Result |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1651 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1652 | LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1653 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1654 | NamedDecl *MemberDecl = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1655 | if (!Result) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1656 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1657 | << &Member << BaseExpr->getSourceRange()); |
| 1658 | else if (Result.isAmbiguous()) { |
| 1659 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1660 | MemberLoc, BaseExpr->getSourceRange()); |
| 1661 | return ExprError(); |
| 1662 | } else |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1663 | MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1664 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1665 | // If the decl being referenced had an error, return an error for this |
| 1666 | // sub-expr without emitting another error, in order to avoid cascading |
| 1667 | // error cases. |
| 1668 | if (MemberDecl->isInvalidDecl()) |
| 1669 | return ExprError(); |
Chris Lattner | b63f813 | 2009-02-16 17:07:21 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1671 | // Check the use of this field |
| 1672 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 1673 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1675 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1676 | // We may have found a field within an anonymous union or struct |
| 1677 | // (C++ [class.union]). |
| 1678 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1679 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1680 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1681 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1682 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1683 | // FIXME: Handle address space modifiers |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1684 | QualType MemberType = FD->getType(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1685 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1686 | MemberType = Ref->getPointeeType(); |
| 1687 | else { |
| 1688 | unsigned combinedQualifiers = |
| 1689 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1690 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1691 | combinedQualifiers &= ~QualType::Const; |
| 1692 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1693 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1694 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1695 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1696 | MemberLoc, MemberType)); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1697 | } else if (CXXClassVarDecl *Var = dyn_cast<CXXClassVarDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1698 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1699 | Var, MemberLoc, |
| 1700 | Var->getType().getNonReferenceType())); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1701 | else if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1702 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1703 | MemberFn, MemberLoc, MemberFn->getType())); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1704 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1705 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1706 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1707 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1708 | else if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1709 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Enum, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1710 | MemberLoc, Enum->getType())); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1711 | else if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1712 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1713 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1715 | // We found a declaration kind that we didn't expect. This is a |
| 1716 | // generic error message that tells the user that she can't refer |
| 1717 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1718 | return ExprError(Diag(MemberLoc, |
| 1719 | diag::err_typecheck_member_reference_unknown) |
| 1720 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1721 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1723 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1724 | // (*Obj).ivar. |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1725 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1726 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member)) { |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1727 | // If the decl being referenced had an error, return an error for this |
| 1728 | // sub-expr without emitting another error, in order to avoid cascading |
| 1729 | // error cases. |
| 1730 | if (IV->isInvalidDecl()) |
| 1731 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1732 | |
| 1733 | // Check whether we can reference this field. |
| 1734 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1735 | return ExprError(); |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 1736 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1737 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 1738 | MemberLoc, BaseExpr, |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1739 | OpKind == tok::arrow); |
| 1740 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1741 | return Owned(MRef); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1742 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1743 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1744 | << IFTy->getDecl()->getDeclName() << &Member |
| 1745 | << BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1746 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1748 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1749 | // pointer to a (potentially qualified) interface type. |
| 1750 | const PointerType *PTy; |
| 1751 | const ObjCInterfaceType *IFTy; |
| 1752 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1753 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1754 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | dd85128 | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1755 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1756 | // Search for a declared property first. |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1757 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1758 | // Check whether we can reference this property. |
| 1759 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1760 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1761 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1762 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1763 | MemberLoc, BaseExpr)); |
| 1764 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1765 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1766 | // Check protocols on qualified interfaces. |
Chris Lattner | d5f8179 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1767 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1768 | E = IFTy->qual_end(); I != E; ++I) |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1769 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1770 | // Check whether we can reference this property. |
| 1771 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1772 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1773 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1774 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1775 | MemberLoc, BaseExpr)); |
| 1776 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1777 | |
| 1778 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1779 | // selector is implemented. |
| 1780 | |
| 1781 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1782 | // shared with the code in ActOnInstanceMessage. |
| 1783 | |
| 1784 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1785 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1786 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1787 | // If this reference is in an @implementation, check for 'private' methods. |
| 1788 | if (!Getter) |
| 1789 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1790 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1791 | if (ObjCImplementationDecl *ImpDecl = |
| 1792 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1793 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 1794 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1795 | // Look through local category implementations associated with the class. |
| 1796 | if (!Getter) { |
| 1797 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1798 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1799 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1800 | } |
| 1801 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1802 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1803 | // Check if we can reference this property. |
| 1804 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1805 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1806 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1807 | // If we found a getter then this may be a valid dot-reference, we |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1808 | // will look for the matching setter, in case it is needed. |
| 1809 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 1810 | &Member); |
| 1811 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 1812 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 1813 | if (!Setter) { |
| 1814 | // If this reference is in an @implementation, also check for 'private' |
| 1815 | // methods. |
| 1816 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1817 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1818 | if (ObjCImplementationDecl *ImpDecl = |
| 1819 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1820 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 1821 | } |
| 1822 | // Look through local category implementations associated with the class. |
| 1823 | if (!Setter) { |
| 1824 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 1825 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1826 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
| 1827 | } |
| 1828 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1829 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1830 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1831 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1832 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1833 | // FIXME: we must check that the setter has property type. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1834 | return Owned(new (Context) ObjCKVCRefExpr(Getter, Getter->getResultType(), |
| 1835 | Setter, MemberLoc, BaseExpr)); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1836 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1837 | |
| 1838 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1839 | << &Member << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1840 | } |
Steve Naroff | d1d4440 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1841 | // Handle properties on qualified "id" protocols. |
| 1842 | const ObjCQualifiedIdType *QIdTy; |
| 1843 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 1844 | // Check protocols on qualified interfaces. |
| 1845 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1846 | E = QIdTy->qual_end(); I != E; ++I) { |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1847 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1848 | // Check the use of this declaration |
| 1849 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1850 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1851 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1852 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1853 | MemberLoc, BaseExpr)); |
| 1854 | } |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1855 | // Also must look for a getter name which uses property syntax. |
| 1856 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1857 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 1858 | // Check the use of this method. |
| 1859 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 1860 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1861 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1862 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
| 1863 | OMD->getResultType(), OMD, OpLoc, MemberLoc, NULL, 0)); |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1866 | |
| 1867 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1868 | << &Member << BaseType); |
| 1869 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1870 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 1871 | if (BaseType->isExtVectorType()) { |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1872 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1873 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1874 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1875 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
| 1876 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1877 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1878 | |
| 1879 | return ExprError(Diag(MemberLoc, |
| 1880 | diag::err_typecheck_member_reference_struct_union) |
| 1881 | << BaseType << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1884 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 1885 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 1886 | /// function prototype Proto. Call is the call expression itself, and |
| 1887 | /// Fn is the function expression. For a C++ member function, this |
| 1888 | /// routine does not attempt to convert the object argument. Returns |
| 1889 | /// true if the call is ill-formed. |
| 1890 | bool |
| 1891 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
| 1892 | FunctionDecl *FDecl, |
| 1893 | const FunctionTypeProto *Proto, |
| 1894 | Expr **Args, unsigned NumArgs, |
| 1895 | SourceLocation RParenLoc) { |
| 1896 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 1897 | // assignment, to the types of the corresponding parameter, ... |
| 1898 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1899 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1900 | bool Invalid = false; |
| 1901 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1902 | // If too few arguments are available (and we don't have default |
| 1903 | // arguments for the remaining parameters), don't make the call. |
| 1904 | if (NumArgs < NumArgsInProto) { |
| 1905 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 1906 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 1907 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 1908 | // Use default arguments for missing arguments |
| 1909 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1910 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | // If too many are passed and not variadic, error on the extras and drop |
| 1914 | // them. |
| 1915 | if (NumArgs > NumArgsInProto) { |
| 1916 | if (!Proto->isVariadic()) { |
| 1917 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 1918 | diag::err_typecheck_call_too_many_args) |
| 1919 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 1920 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 1921 | Args[NumArgs-1]->getLocEnd()); |
| 1922 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1923 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1924 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1925 | } |
| 1926 | NumArgsToCheck = NumArgsInProto; |
| 1927 | } |
| 1928 | |
| 1929 | // Continue to check argument types (even if we have too few/many args). |
| 1930 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 1931 | QualType ProtoArgType = Proto->getArgType(i); |
| 1932 | |
| 1933 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1934 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1935 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1936 | |
| 1937 | // Pass the argument. |
| 1938 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 1939 | return true; |
| 1940 | } else |
| 1941 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1942 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1943 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1945 | Call->setArg(i, Arg); |
| 1946 | } |
| 1947 | |
| 1948 | // If this is a variadic call, handle args passed through "...". |
| 1949 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1950 | VariadicCallType CallType = VariadicFunction; |
| 1951 | if (Fn->getType()->isBlockPointerType()) |
| 1952 | CallType = VariadicBlock; // Block |
| 1953 | else if (isa<MemberExpr>(Fn)) |
| 1954 | CallType = VariadicMethod; |
| 1955 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1956 | // Promote the arguments (C99 6.5.2.2p7). |
| 1957 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 1958 | Expr *Arg = Args[i]; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1959 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1960 | Call->setArg(i, Arg); |
| 1961 | } |
| 1962 | } |
| 1963 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1964 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1967 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1968 | /// This provides the location of the left/right parens and a list of comma |
| 1969 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1970 | Action::OwningExprResult |
| 1971 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 1972 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1973 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1974 | unsigned NumArgs = args.size(); |
| 1975 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 1976 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1977 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1978 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1979 | DeclarationName UnqualifiedName; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1981 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1982 | // Determine whether this is a dependent call inside a C++ template, |
| 1983 | // in which case we won't do any semantic analysis now. |
| 1984 | // FIXME: Will need to cache the results of name lookup (including ADL) in Fn. |
| 1985 | bool Dependent = false; |
| 1986 | if (Fn->isTypeDependent()) |
| 1987 | Dependent = true; |
| 1988 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 1989 | Dependent = true; |
| 1990 | |
| 1991 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1992 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1993 | Context.DependentTy, RParenLoc)); |
| 1994 | |
| 1995 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 1996 | if (Fn->getType()->isRecordType()) |
| 1997 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 1998 | CommaLocs, RParenLoc)); |
| 1999 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2000 | // Determine whether this is a call to a member function. |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2001 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2002 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2003 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2004 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2005 | CommaLocs, RParenLoc)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2008 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2009 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2010 | Expr *FnExpr = Fn; |
| 2011 | bool ADL = true; |
| 2012 | while (true) { |
| 2013 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2014 | FnExpr = IcExpr->getSubExpr(); |
| 2015 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2016 | // Parentheses around a function disable ADL |
| 2017 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2018 | ADL = false; |
| 2019 | FnExpr = PExpr->getSubExpr(); |
| 2020 | } else if (isa<UnaryOperator>(FnExpr) && |
| 2021 | cast<UnaryOperator>(FnExpr)->getOpcode() |
| 2022 | == UnaryOperator::AddrOf) { |
| 2023 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2024 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2025 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2026 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2027 | break; |
| 2028 | } else if (UnresolvedFunctionNameExpr *DepName |
| 2029 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2030 | UnqualifiedName = DepName->getName(); |
| 2031 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2032 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2033 | // Any kind of name that does not refer to a declaration (or |
| 2034 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2035 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2036 | break; |
| 2037 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2038 | } |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2040 | OverloadedFunctionDecl *Ovl = 0; |
| 2041 | if (DRExpr) { |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2042 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2043 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
| 2044 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2046 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2047 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2048 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2049 | ADL = false; |
| 2050 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2051 | // We don't perform ADL in C. |
| 2052 | if (!getLangOptions().CPlusPlus) |
| 2053 | ADL = false; |
| 2054 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2055 | if (Ovl || ADL) { |
| 2056 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2057 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2058 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2059 | if (!FDecl) |
| 2060 | return ExprError(); |
| 2061 | |
| 2062 | // Update Fn to refer to the actual function selected. |
| 2063 | Expr *NewFn = 0; |
| 2064 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2065 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2066 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2067 | QDRExpr->getLocation(), |
| 2068 | false, false, |
| 2069 | QDRExpr->getSourceRange().getBegin()); |
| 2070 | else |
| 2071 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
| 2072 | Fn->getSourceRange().getBegin()); |
| 2073 | Fn->Destroy(Context); |
| 2074 | Fn = NewFn; |
| 2075 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2076 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2077 | |
| 2078 | // Promote the function operand. |
| 2079 | UsualUnaryConversions(Fn); |
| 2080 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2081 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2082 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2083 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2084 | Args, NumArgs, |
| 2085 | Context.BoolTy, |
| 2086 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2087 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2088 | const FunctionType *FuncT; |
| 2089 | if (!Fn->getType()->isBlockPointerType()) { |
| 2090 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2091 | // have type pointer to function". |
| 2092 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2093 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2094 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2095 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2096 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2097 | } else { // This is a block call. |
| 2098 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2099 | getAsFunctionType(); |
| 2100 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2101 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2102 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2103 | << Fn->getType() << Fn->getSourceRange()); |
| 2104 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2105 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2106 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2107 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2108 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2109 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
| 2110 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2111 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2112 | } else { |
| 2113 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2114 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2115 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2116 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2117 | Expr *Arg = Args[i]; |
| 2118 | DefaultArgumentPromotion(Arg); |
| 2119 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2120 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2121 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2123 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2124 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2125 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2126 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2127 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2128 | // Do special checking on direct calls to functions. |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2129 | if (FDecl) |
| 2130 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2131 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2132 | return Owned(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2135 | Action::OwningExprResult |
| 2136 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2137 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2138 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2139 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 2140 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2141 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2142 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2143 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2144 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2145 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2146 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2147 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2148 | } else if (DiagnoseIncompleteType(LParenLoc, literalType, |
| 2149 | diag::err_typecheck_decl_incomplete_type, |
| 2150 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2151 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2152 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2153 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2154 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2155 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2156 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2157 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2158 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2159 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2160 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2161 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2162 | InitExpr.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2163 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
| 2164 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2167 | Action::OwningExprResult |
| 2168 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| 2169 | InitListDesignations &Designators, |
| 2170 | SourceLocation RBraceLoc) { |
| 2171 | unsigned NumInit = initlist.size(); |
| 2172 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2173 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2174 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | 1c9de71 | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 2175 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2176 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2177 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2178 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2179 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2180 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2183 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 5ad49de | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2184 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2185 | UsualUnaryConversions(castExpr); |
| 2186 | |
| 2187 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2188 | // type needs to be scalar. |
| 2189 | if (castType->isVoidType()) { |
| 2190 | // Cast to void allows any expr type. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2191 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2192 | // We can't check any more until template instantiation time. |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2193 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2194 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2195 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2196 | (castType->isStructureType() || castType->isUnionType())) { |
| 2197 | // GCC struct/union extension: allow cast to self. |
| 2198 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2199 | << castType << castExpr->getSourceRange(); |
| 2200 | } else if (castType->isUnionType()) { |
| 2201 | // GCC cast to union extension |
| 2202 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2203 | RecordDecl::field_iterator Field, FieldEnd; |
| 2204 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2205 | Field != FieldEnd; ++Field) { |
| 2206 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2207 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2208 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2209 | << castExpr->getSourceRange(); |
| 2210 | break; |
| 2211 | } |
| 2212 | } |
| 2213 | if (Field == FieldEnd) |
| 2214 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2215 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2216 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2217 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2218 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2219 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2220 | } |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2221 | } else if (!castExpr->getType()->isScalarType() && |
| 2222 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2223 | return Diag(castExpr->getLocStart(), |
| 2224 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2225 | << castExpr->getType() << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2226 | } else if (castExpr->getType()->isVectorType()) { |
| 2227 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2228 | return true; |
| 2229 | } else if (castType->isVectorType()) { |
| 2230 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2231 | return true; |
| 2232 | } |
| 2233 | return false; |
| 2234 | } |
| 2235 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2236 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2237 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 2238 | |
| 2239 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2240 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2241 | return Diag(R.getBegin(), |
| 2242 | Ty->isVectorType() ? |
| 2243 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2244 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2245 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2246 | } else |
| 2247 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2248 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2249 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2250 | |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2254 | Action::OwningExprResult |
| 2255 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2256 | SourceLocation RParenLoc, ExprArg Op) { |
| 2257 | assert((Ty != 0) && (Op.get() != 0) && |
| 2258 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2259 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2260 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2261 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2262 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2263 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2264 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2265 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2266 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2269 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 2270 | /// In that case, lex = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2271 | /// C99 6.5.15 |
| 2272 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2273 | SourceLocation QuestionLoc) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2274 | UsualUnaryConversions(Cond); |
| 2275 | UsualUnaryConversions(LHS); |
| 2276 | UsualUnaryConversions(RHS); |
| 2277 | QualType CondTy = Cond->getType(); |
| 2278 | QualType LHSTy = LHS->getType(); |
| 2279 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2280 | |
| 2281 | // first, check the condition. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2282 | if (!Cond->isTypeDependent()) { |
| 2283 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2284 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2285 | << CondTy; |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2286 | return QualType(); |
| 2287 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2288 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2289 | |
| 2290 | // Now check the two expressions. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2291 | if ((LHS && LHS->isTypeDependent()) || (RHS && RHS->isTypeDependent())) |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2292 | return Context.DependentTy; |
| 2293 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2294 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2295 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2296 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2297 | UsualArithmeticConversions(LHS, RHS); |
| 2298 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2299 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2300 | |
| 2301 | // If both operands are the same structure or union type, the result is that |
| 2302 | // type. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2303 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2304 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2305 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2306 | // "If both the operands have structure or union type, the result has |
| 2307 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2308 | return LHSTy.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2309 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2310 | |
| 2311 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2312 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2313 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2314 | if (!LHSTy->isVoidType()) |
| 2315 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2316 | << RHS->getSourceRange(); |
| 2317 | if (!RHSTy->isVoidType()) |
| 2318 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2319 | << LHS->getSourceRange(); |
| 2320 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2321 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2322 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2323 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2324 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2325 | // the type of the other operand." |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2326 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2327 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2328 | RHS->isNullPointerConstant(Context)) { |
| 2329 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2330 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2331 | } |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2332 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2333 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2334 | LHS->isNullPointerConstant(Context)) { |
| 2335 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2336 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2337 | } |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2339 | // Handle the case where both operands are pointers before we handle null |
| 2340 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2341 | if (const PointerType *LHSPT = LHSTy->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2342 | if (const PointerType *RHSPT = RHSTy->getAsPointerType()) { |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2343 | // get the "pointed to" types |
| 2344 | QualType lhptee = LHSPT->getPointeeType(); |
| 2345 | QualType rhptee = RHSPT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2346 | |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2347 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2348 | if (lhptee->isVoidType() && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2349 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2350 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2351 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2352 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2353 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2354 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2355 | return destType; |
| 2356 | } |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2357 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2358 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2359 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2360 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2361 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2362 | return destType; |
| 2363 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2365 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 2366 | // Two identical pointers types are always compatible. |
| 2367 | return LHSTy; |
| 2368 | } |
| 2369 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2370 | QualType compositeType = LHSTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2371 | |
| 2372 | // If either type is an Objective-C object type then check |
| 2373 | // compatibility according to Objective-C. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2374 | if (Context.isObjCObjectPointerType(LHSTy) || |
| 2375 | Context.isObjCObjectPointerType(RHSTy)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2376 | // If both operands are interfaces and either operand can be |
| 2377 | // assigned to the other, use that type as the composite |
| 2378 | // type. This allows |
| 2379 | // xxx ? (A*) a : (B*) b |
| 2380 | // where B is a subclass of A. |
| 2381 | // |
| 2382 | // Additionally, as for assignment, if either type is 'id' |
| 2383 | // allow silent coercion. Finally, if the types are |
| 2384 | // incompatible then make sure to use 'id' as the composite |
| 2385 | // type so the result is acceptable for sending messages to. |
| 2386 | |
Steve Naroff | 9fc9cb5 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2387 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 2388 | // It could return the composite type. |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2389 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2390 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2391 | if (LHSIface && RHSIface && |
| 2392 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2393 | compositeType = LHSTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2394 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 5183f9e | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2395 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2396 | compositeType = RHSTy; |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2397 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2398 | Context.isObjCIdStructType(rhptee)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2399 | compositeType = Context.getObjCIdType(); |
| 2400 | } else { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2401 | Diag(QuestionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 2402 | << LHSTy << RHSTy |
| 2403 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2404 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2405 | ImpCastExprToType(LHS, incompatTy); |
| 2406 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2407 | return incompatTy; |
| 2408 | } |
| 2409 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2410 | rhptee.getUnqualifiedType())) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2411 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 2412 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2413 | // In this situation, we assume void* type. No especially good |
| 2414 | // reason, but this is what gcc does, and we do have to pick |
| 2415 | // to get a consistent AST. |
| 2416 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2417 | ImpCastExprToType(LHS, incompatTy); |
| 2418 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | cd23bb2 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2419 | return incompatTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2420 | } |
| 2421 | // The pointer types are compatible. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2422 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2423 | // differently qualified versions of compatible types, the result type is |
| 2424 | // a pointer to an appropriately qualified version of the *composite* |
| 2425 | // type. |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2426 | // FIXME: Need to calculate the composite type. |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2427 | // FIXME: Need to add qualifiers |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2428 | ImpCastExprToType(LHS, compositeType); |
| 2429 | ImpCastExprToType(RHS, compositeType); |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2430 | return compositeType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2431 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2432 | } |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Selection between block pointer types is ok as long as they are the same. |
| 2435 | if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() && |
| 2436 | Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) |
| 2437 | return LHSTy; |
| 2438 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2439 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2440 | // evaluates to "struct objc_object *" (and is handled above when comparing |
| 2441 | // id with statically typed objects). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2442 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2443 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2444 | // id. Currently localizing to here until clear this should be |
| 2445 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2446 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
| 2447 | (LHSTy->isObjCQualifiedIdType() && |
| 2448 | Context.isObjCObjectPointerType(RHSTy)) || |
| 2449 | (RHSTy->isObjCQualifiedIdType() && |
| 2450 | Context.isObjCObjectPointerType(LHSTy))) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2451 | // FIXME: This is not the correct composite type. This only |
| 2452 | // happens to work because id can more or less be used anywhere, |
| 2453 | // however this may change the type of method sends. |
| 2454 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2455 | // (confusing) incompatible comparison warnings in some |
| 2456 | // cases. Investigate. |
| 2457 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2458 | ImpCastExprToType(LHS, compositeType); |
| 2459 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2460 | return compositeType; |
| 2461 | } |
| 2462 | } |
| 2463 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2464 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2465 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2466 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2467 | return QualType(); |
| 2468 | } |
| 2469 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2470 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2471 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2472 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2473 | SourceLocation ColonLoc, |
| 2474 | ExprArg Cond, ExprArg LHS, |
| 2475 | ExprArg RHS) { |
| 2476 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2477 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2478 | |
| 2479 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2480 | // was the condition. |
| 2481 | bool isLHSNull = LHSExpr == 0; |
| 2482 | if (isLHSNull) |
| 2483 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2484 | |
| 2485 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2486 | RHSExpr, QuestionLoc); |
| 2487 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2488 | return ExprError(); |
| 2489 | |
| 2490 | Cond.release(); |
| 2491 | LHS.release(); |
| 2492 | RHS.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2493 | return Owned(new (Context) ConditionalOperator(CondExpr, |
| 2494 | isLHSNull ? 0 : LHSExpr, |
| 2495 | RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2498 | |
| 2499 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 2500 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 2501 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2502 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2503 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2504 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2505 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2506 | QualType lhptee, rhptee; |
| 2507 | |
| 2508 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2509 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2510 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2511 | |
| 2512 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2513 | lhptee = Context.getCanonicalType(lhptee); |
| 2514 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2516 | AssignConvertType ConvTy = Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2517 | |
| 2518 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2519 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2520 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2521 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2522 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2523 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2524 | |
| 2525 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2526 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 2527 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2528 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2529 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2530 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2531 | |
| 2532 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2533 | assert(rhptee->isFunctionType()); |
| 2534 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2538 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2539 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2540 | |
| 2541 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2542 | assert(lhptee->isFunctionType()); |
| 2543 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2544 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2545 | |
| 2546 | // Check for ObjC interfaces |
| 2547 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2548 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2549 | if (LHSIface && RHSIface && |
| 2550 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 2551 | return ConvTy; |
| 2552 | |
| 2553 | // ID acts sort of like void* for ObjC interfaces |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2554 | if (LHSIface && Context.isObjCIdStructType(rhptee)) |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2555 | return ConvTy; |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2556 | if (RHSIface && Context.isObjCIdStructType(lhptee)) |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2557 | return ConvTy; |
| 2558 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2559 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 2560 | // unqualified versions of compatible types, ... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2561 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2562 | rhptee.getUnqualifiedType())) |
| 2563 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2564 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2567 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2568 | /// block pointer types are compatible or whether a block and normal pointer |
| 2569 | /// are compatible. It is more restrict than comparing two function pointer |
| 2570 | // types. |
| 2571 | Sema::AssignConvertType |
| 2572 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
| 2573 | QualType rhsType) { |
| 2574 | QualType lhptee, rhptee; |
| 2575 | |
| 2576 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2577 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
| 2578 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2579 | |
| 2580 | // make sure we operate on the canonical type |
| 2581 | lhptee = Context.getCanonicalType(lhptee); |
| 2582 | rhptee = Context.getCanonicalType(rhptee); |
| 2583 | |
| 2584 | AssignConvertType ConvTy = Compatible; |
| 2585 | |
| 2586 | // For blocks we enforce that qualifiers are identical. |
| 2587 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2588 | ConvTy = CompatiblePointerDiscardsQualifiers; |
| 2589 | |
| 2590 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
| 2591 | return IncompatibleBlockPointer; |
| 2592 | return ConvTy; |
| 2593 | } |
| 2594 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2595 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2596 | /// has code to accommodate several GCC extensions when type checking |
| 2597 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2598 | /// |
| 2599 | /// int a, *pint; |
| 2600 | /// short *pshort; |
| 2601 | /// struct foo *pfoo; |
| 2602 | /// |
| 2603 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2604 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2605 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2606 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2607 | /// |
| 2608 | /// As a result, the code for dealing with pointers is more complex than the |
| 2609 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2610 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2611 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2612 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2613 | // Get canonical types. We're not formatting these types, just comparing |
| 2614 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2615 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2616 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2619 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2621 | // If the left-hand side is a reference type, then we are in a |
| 2622 | // (rare!) case where we've allowed the use of references in C, |
| 2623 | // e.g., as a parameter type in a built-in function. In this case, |
| 2624 | // just make sure that the type referenced is compatible with the |
| 2625 | // right-hand side type. The caller is responsible for adjusting |
| 2626 | // lhsType so that the resulting expression does not have reference |
| 2627 | // type. |
| 2628 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2629 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2630 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2631 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2632 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2634 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2635 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2636 | return Compatible; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2637 | // Relax integer conversions like we do for pointers below. |
| 2638 | if (rhsType->isIntegerType()) |
| 2639 | return IntToPointer; |
| 2640 | if (lhsType->isIntegerType()) |
| 2641 | return PointerToInt; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2642 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2643 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2644 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2645 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2646 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2647 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2648 | if (LV->getElementType() == rhsType) |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2649 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2650 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2651 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 2652 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 2653 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2654 | if (getLangOptions().LaxVectorConversions && |
| 2655 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2656 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2657 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2658 | } |
| 2659 | return Incompatible; |
| 2660 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2662 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2663 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2664 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2665 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2666 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2667 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2669 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2670 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2671 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2672 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2673 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2674 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Treat block pointers as objects. |
| 2677 | if (getLangOptions().ObjC1 && |
| 2678 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2679 | return Compatible; |
| 2680 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2681 | return Incompatible; |
| 2682 | } |
| 2683 | |
| 2684 | if (isa<BlockPointerType>(lhsType)) { |
| 2685 | if (rhsType->isIntegerType()) |
| 2686 | return IntToPointer; |
| 2687 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2688 | // Treat block pointers as objects. |
| 2689 | if (getLangOptions().ObjC1 && |
| 2690 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2691 | return Compatible; |
| 2692 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2693 | if (rhsType->isBlockPointerType()) |
| 2694 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
| 2695 | |
| 2696 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 2697 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2698 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2699 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2700 | return Incompatible; |
| 2701 | } |
| 2702 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2703 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2704 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2705 | if (lhsType == Context.BoolTy) |
| 2706 | return Compatible; |
| 2707 | |
| 2708 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2709 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2711 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2712 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2713 | |
| 2714 | if (isa<BlockPointerType>(lhsType) && |
| 2715 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2716 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2717 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2718 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2719 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2720 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2721 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2722 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2723 | } |
| 2724 | return Incompatible; |
| 2725 | } |
| 2726 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2727 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2728 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2729 | if (getLangOptions().CPlusPlus) { |
| 2730 | if (!lhsType->isRecordType()) { |
| 2731 | // C++ 5.17p3: If the left operand is not of class type, the |
| 2732 | // expression is implicitly converted (C++ 4) to the |
| 2733 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2734 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 2735 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2736 | return Incompatible; |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2737 | else |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2738 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 2742 | // structures. |
| 2743 | } |
| 2744 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2745 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 2746 | // a null pointer constant. |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2747 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType() || |
| 2748 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 2749 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2750 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2751 | return Compatible; |
| 2752 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2753 | |
| 2754 | // We don't allow conversion of non-null-pointer constants to integers. |
| 2755 | if (lhsType->isBlockPointerType() && rExpr->getType()->isIntegerType()) |
| 2756 | return IntToBlockPointer; |
| 2757 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2758 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2759 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2760 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2761 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2762 | // |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2763 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2764 | if (!lhsType->isReferenceType()) |
| 2765 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2766 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2767 | Sema::AssignConvertType result = |
| 2768 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2769 | |
| 2770 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 2771 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2772 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 2773 | // so that we can use references in built-in functions even in C. |
| 2774 | // The getNonReferenceType() call makes sure that the resulting expression |
| 2775 | // does not have reference type. |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2776 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2777 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2778 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2781 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2782 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 2783 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 2784 | } |
| 2785 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2786 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2787 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 2788 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2789 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2790 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2793 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2794 | Expr *&rex) { |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2795 | // For conversion purposes, we ignore any qualifiers. |
| 2796 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2797 | QualType lhsType = |
| 2798 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 2799 | QualType rhsType = |
| 2800 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2801 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2802 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2803 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2804 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2805 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2806 | // Handle the case of a vector & extvector type of the same size and element |
| 2807 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2808 | if (getLangOptions().LaxVectorConversions) { |
| 2809 | // FIXME: Should we warn here? |
| 2810 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2811 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 2812 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2813 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2814 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2819 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 2820 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2821 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2822 | QualType eltType = V->getElementType(); |
| 2823 | |
| 2824 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
| 2825 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 2826 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2827 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2828 | return lhsType; |
| 2829 | } |
| 2830 | } |
| 2831 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2832 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2833 | // promote the lhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2834 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2835 | QualType eltType = V->getElementType(); |
| 2836 | |
| 2837 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
| 2838 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 2839 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2840 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2841 | return rhsType; |
| 2842 | } |
| 2843 | } |
| 2844 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2845 | // You cannot convert between vector values of different size. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2846 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2847 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2848 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2849 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2852 | inline QualType Sema::CheckMultiplyDivideOperands( |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2853 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2854 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 2855 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2856 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2857 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2858 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2860 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2861 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2862 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | inline QualType Sema::CheckRemainderOperands( |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2866 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2867 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 2868 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 2869 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 2870 | return CheckVectorOperands(Loc, lex, rex); |
| 2871 | return InvalidOperands(Loc, lex, rex); |
| 2872 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2873 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2874 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2875 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2876 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2877 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2878 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2882 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2883 | { |
| 2884 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2885 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2886 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2887 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2888 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2889 | // handle the common case first (both operands are arithmetic). |
| 2890 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2891 | return compType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2892 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2893 | // Put any potential pointer into PExp |
| 2894 | Expr* PExp = lex, *IExp = rex; |
| 2895 | if (IExp->getType()->isPointerType()) |
| 2896 | std::swap(PExp, IExp); |
| 2897 | |
| 2898 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 2899 | if (IExp->getType()->isIntegerType()) { |
| 2900 | // Check for arithmetic on pointers to incomplete types |
| 2901 | if (!PTy->getPointeeType()->isObjectType()) { |
| 2902 | if (PTy->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2903 | if (getLangOptions().CPlusPlus) { |
| 2904 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 2905 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2906 | return QualType(); |
| 2907 | } |
| 2908 | |
| 2909 | // GNU extension: arithmetic on pointer to void |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2910 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2911 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2912 | } else if (PTy->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2913 | if (getLangOptions().CPlusPlus) { |
| 2914 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2915 | << lex->getType() << lex->getSourceRange(); |
| 2916 | return QualType(); |
| 2917 | } |
| 2918 | |
| 2919 | // GNU extension: arithmetic on pointer to function |
| 2920 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2921 | << lex->getType() << lex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2922 | } else { |
| 2923 | DiagnoseIncompleteType(Loc, PTy->getPointeeType(), |
| 2924 | diag::err_typecheck_arithmetic_incomplete_type, |
| 2925 | lex->getSourceRange(), SourceRange(), |
| 2926 | lex->getType()); |
| 2927 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | return PExp->getType(); |
| 2931 | } |
| 2932 | } |
| 2933 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2934 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2937 | // C99 6.5.6 |
| 2938 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2939 | SourceLocation Loc, bool isCompAssign) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2940 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2941 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2942 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2943 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2944 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2945 | // Enforce type constraints: C99 6.5.6p3. |
| 2946 | |
| 2947 | // Handle the common case first (both operands are arithmetic). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2948 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2949 | return compType; |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Either ptr - int or ptr - ptr. |
| 2952 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2953 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2955 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2956 | if (!lpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2957 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2958 | if (lpointee->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2959 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2960 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2961 | } else if (lpointee->isFunctionType()) { |
| 2962 | if (getLangOptions().CPlusPlus) { |
| 2963 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2964 | << lex->getType() << lex->getSourceRange(); |
| 2965 | return QualType(); |
| 2966 | } |
| 2967 | |
| 2968 | // GNU extension: arithmetic on pointer to function |
| 2969 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 2970 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2971 | } else { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2972 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2973 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2974 | return QualType(); |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | // The result type of a pointer-int computation is the pointer type. |
| 2979 | if (rex->getType()->isIntegerType()) |
| 2980 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2981 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2982 | // Handle pointer-pointer subtractions. |
| 2983 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2984 | QualType rpointee = RHSPTy->getPointeeType(); |
| 2985 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2986 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2987 | if (!rpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2988 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2989 | if (rpointee->isVoidType()) { |
| 2990 | if (!lpointee->isVoidType()) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2991 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2992 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f93eda1 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 2993 | } else if (rpointee->isFunctionType()) { |
| 2994 | if (getLangOptions().CPlusPlus) { |
| 2995 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2996 | << rex->getType() << rex->getSourceRange(); |
| 2997 | return QualType(); |
| 2998 | } |
| 2999 | |
| 3000 | // GNU extension: arithmetic on pointer to function |
| 3001 | if (!lpointee->isFunctionType()) |
| 3002 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3003 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3004 | } else { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3005 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3006 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3007 | return QualType(); |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | // Pointee types must be compatible. |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3012 | if (!Context.typesAreCompatible( |
| 3013 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 3014 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3015 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3016 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3017 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3018 | return QualType(); |
| 3019 | } |
| 3020 | |
| 3021 | return Context.getPointerDiffType(); |
| 3022 | } |
| 3023 | } |
| 3024 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3025 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3028 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3029 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3030 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3031 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3032 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3033 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3034 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3035 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3036 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3037 | if (!isCompAssign) |
| 3038 | UsualUnaryConversions(lex); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3039 | UsualUnaryConversions(rex); |
| 3040 | |
| 3041 | // "The type of the result is that of the promoted left operand." |
| 3042 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3045 | // C99 6.5.8 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3046 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3047 | bool isRelational) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3048 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3049 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3050 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3051 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3052 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3053 | UsualArithmeticConversions(lex, rex); |
| 3054 | else { |
| 3055 | UsualUnaryConversions(lex); |
| 3056 | UsualUnaryConversions(rex); |
| 3057 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3058 | QualType lType = lex->getType(); |
| 3059 | QualType rType = rex->getType(); |
| 3060 | |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3061 | // For non-floating point types, check for self-comparisons of the form |
| 3062 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3063 | // often indicate logic errors in the program. |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3064 | if (!lType->isFloatingType()) { |
Ted Kremenek | 87e30c5 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 3065 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3066 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3067 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3068 | Diag(Loc, diag::warn_selfcomparison); |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3071 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| 3072 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy : Context.IntTy; |
| 3073 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3074 | if (isRelational) { |
| 3075 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3076 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3077 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3078 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3079 | if (lType->isFloatingType()) { |
| 3080 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3081 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3084 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3085 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3086 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3088 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3089 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 3090 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3091 | // All of the following pointer related warnings are GCC extensions, except |
| 3092 | // when handling null pointer constants. One day, we can consider making them |
| 3093 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3094 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3095 | QualType LCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3096 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3097 | QualType RCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3098 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3099 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3100 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3101 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3102 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3103 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3104 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3105 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3106 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3107 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3108 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3109 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3110 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3111 | // Handle block pointer types. |
| 3112 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 3113 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3114 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
| 3115 | |
| 3116 | if (!LHSIsNull && !RHSIsNull && |
| 3117 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3118 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3119 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3120 | } |
| 3121 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3122 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3123 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3124 | // Allow block pointers to be compared with null pointer constants. |
| 3125 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 3126 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 3127 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3128 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3129 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3130 | } |
| 3131 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3132 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3133 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3134 | |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3135 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3136 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3137 | const PointerType *LPT = lType->getAsPointerType(); |
| 3138 | const PointerType *RPT = rType->getAsPointerType(); |
| 3139 | bool LPtrToVoid = LPT ? |
| 3140 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
| 3141 | bool RPtrToVoid = RPT ? |
| 3142 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
| 3143 | |
| 3144 | if (!LPtrToVoid && !RPtrToVoid && |
| 3145 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3146 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3147 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3148 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3149 | return ResultTy; |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3150 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3151 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3152 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 3153 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3154 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 3155 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3156 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3157 | } else { |
| 3158 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3159 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3160 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3161 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3162 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3163 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3164 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 3165 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3166 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
| 3167 | rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3168 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3169 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3170 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3171 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3172 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3173 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3174 | if (lType->isIntegerType() && |
| 3175 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3176 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3177 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3178 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3179 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3180 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3181 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3182 | // Handle block pointers. |
| 3183 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 3184 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3185 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3186 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3187 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3188 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3189 | } |
| 3190 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 3191 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3192 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3193 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3194 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3195 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3196 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3197 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3200 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| 3201 | /// operates on extended vector types. Instead of producing an IntTy result, |
| 3202 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3203 | /// types. |
| 3204 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3205 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3206 | bool isRelational) { |
| 3207 | // Check to make sure we're operating on vectors of the same type and width, |
| 3208 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3209 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3210 | if (vType.isNull()) |
| 3211 | return vType; |
| 3212 | |
| 3213 | QualType lType = lex->getType(); |
| 3214 | QualType rType = rex->getType(); |
| 3215 | |
| 3216 | // For non-floating point types, check for self-comparisons of the form |
| 3217 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3218 | // often indicate logic errors in the program. |
| 3219 | if (!lType->isFloatingType()) { |
| 3220 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3221 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3222 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3223 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | // Check for comparisons of floating point operands using != and ==. |
| 3227 | if (!isRelational && lType->isFloatingType()) { |
| 3228 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3229 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | // Return the type for the comparison, which is the same as vector type for |
| 3233 | // integer vectors, or an integer type of identical size and number of |
| 3234 | // elements for floating point vectors. |
| 3235 | if (lType->isIntegerType()) |
| 3236 | return lType; |
| 3237 | |
| 3238 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3239 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3240 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3241 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3242 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 3243 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3244 | |
| 3245 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 3246 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3247 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3248 | } |
| 3249 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3250 | inline QualType Sema::CheckBitwiseOperands( |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3251 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3252 | { |
| 3253 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3254 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3255 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3256 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3257 | |
| 3258 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3259 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3260 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3264 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3265 | { |
| 3266 | UsualUnaryConversions(lex); |
| 3267 | UsualUnaryConversions(rex); |
| 3268 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3269 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3270 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3271 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3274 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3275 | /// is a read-only property; return true if so. A readonly property expression |
| 3276 | /// depends on various declarations and thus must be treated specially. |
| 3277 | /// |
| 3278 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
| 3279 | { |
| 3280 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3281 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3282 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3283 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3284 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
| 3285 | if (const ObjCInterfaceType *IFTy = |
| 3286 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3287 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3288 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3289 | return true; |
| 3290 | } |
| 3291 | } |
| 3292 | return false; |
| 3293 | } |
| 3294 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3295 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3296 | /// emit an error and return true. If so, return false. |
| 3297 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3298 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3299 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3300 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3301 | if (IsLV == Expr::MLV_Valid) |
| 3302 | return false; |
| 3303 | |
| 3304 | unsigned Diag = 0; |
| 3305 | bool NeedType = false; |
| 3306 | switch (IsLV) { // C99 6.5.16p2 |
| 3307 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3308 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3309 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3310 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3311 | NeedType = true; |
| 3312 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3313 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3314 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3315 | NeedType = true; |
| 3316 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3317 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3318 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3319 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3320 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3321 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3322 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3323 | case Expr::MLV_IncompleteType: |
| 3324 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3325 | return S.DiagnoseIncompleteType(Loc, E->getType(), |
| 3326 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3327 | E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3328 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3329 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3330 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3331 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3332 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3333 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3334 | case Expr::MLV_ReadonlyProperty: |
| 3335 | Diag = diag::error_readonly_property_assignment; |
| 3336 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3337 | case Expr::MLV_NoSetterProperty: |
| 3338 | Diag = diag::error_nosetter_property_assignment; |
| 3339 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3340 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3341 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3342 | if (NeedType) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3343 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3344 | else |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3345 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3346 | return true; |
| 3347 | } |
| 3348 | |
| 3349 | |
| 3350 | |
| 3351 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3352 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3353 | SourceLocation Loc, |
| 3354 | QualType CompoundType) { |
| 3355 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3356 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3357 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3358 | |
| 3359 | QualType LHSType = LHS->getType(); |
| 3360 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3361 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3362 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3363 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3364 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3365 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3366 | // Special case of NSObject attributes on c-style pointer types. |
| 3367 | if (ConvTy == IncompatiblePointer && |
| 3368 | ((Context.isObjCNSObjectType(LHSType) && |
| 3369 | Context.isObjCObjectPointerType(RHSType)) || |
| 3370 | (Context.isObjCNSObjectType(RHSType) && |
| 3371 | Context.isObjCObjectPointerType(LHSType)))) |
| 3372 | ConvTy = Compatible; |
| 3373 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3374 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3375 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3376 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3377 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3378 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3379 | RHSCheck = ICE->getSubExpr(); |
| 3380 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3381 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3382 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3383 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3384 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3385 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3386 | Diag(Loc, diag::warn_not_compound_assign) |
| 3387 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3388 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3389 | } |
| 3390 | } else { |
| 3391 | // Compound assignment "x += y" |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3392 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3393 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3394 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3395 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3396 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3397 | return QualType(); |
| 3398 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3399 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3400 | // left operand unless the left operand has qualified type, in which case |
| 3401 | // it is the unqualified version of the type of the left operand. |
| 3402 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3403 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3404 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3405 | // oprdu. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3406 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3409 | // C99 6.5.17 |
| 3410 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
| 3411 | // FIXME: what is required for LHS? |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3412 | |
| 3413 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3414 | DefaultFunctionArrayConversion(RHS); |
| 3415 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3419 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3420 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3421 | bool isInc) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3422 | QualType ResType = Op->getType(); |
| 3423 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3424 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3425 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3426 | // Decrement of bool is not allowed. |
| 3427 | if (!isInc) { |
| 3428 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3429 | return QualType(); |
| 3430 | } |
| 3431 | // Increment of bool sets it to true, but is deprecated. |
| 3432 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3433 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3434 | // OK! |
| 3435 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3436 | // C99 6.5.2.4p2, 6.5.6p2 |
| 3437 | if (PT->getPointeeType()->isObjectType()) { |
| 3438 | // Pointer to object is ok! |
| 3439 | } else if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3440 | if (getLangOptions().CPlusPlus) { |
| 3441 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3442 | << Op->getSourceRange(); |
| 3443 | return QualType(); |
| 3444 | } |
| 3445 | |
| 3446 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3447 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3448 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3449 | if (getLangOptions().CPlusPlus) { |
| 3450 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3451 | << Op->getType() << Op->getSourceRange(); |
| 3452 | return QualType(); |
| 3453 | } |
| 3454 | |
| 3455 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3456 | << ResType << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3457 | return QualType(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3458 | } else { |
| 3459 | DiagnoseIncompleteType(OpLoc, PT->getPointeeType(), |
| 3460 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3461 | Op->getSourceRange(), SourceRange(), |
| 3462 | ResType); |
| 3463 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3464 | } |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3465 | } else if (ResType->isComplexType()) { |
| 3466 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3467 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3468 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3469 | } else { |
| 3470 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3471 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3472 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3473 | } |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 3474 | // At this point, we know we have a real, complex or pointer type. |
| 3475 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3476 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3477 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3478 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3481 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3482 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3483 | /// where the declaration is needed for type checking. We only need to |
| 3484 | /// handle cases when the expression references a function designator |
| 3485 | /// or is an lvalue. Here are some examples: |
| 3486 | /// - &(x) => x |
| 3487 | /// - &*****f => f for f a function designator. |
| 3488 | /// - &s.xx => s |
| 3489 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3490 | /// - *(x + 1) -> x, if x is an array |
| 3491 | /// - &"123"[2] -> 0 |
| 3492 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3493 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3494 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3495 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3496 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3497 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3498 | case Stmt::MemberExprClass: |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3499 | // Fields cannot be declared with a 'register' storage class. |
| 3500 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3501 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3502 | return 0; |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3503 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3504 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3505 | // &X[4] and &4[X] refers to X if X is not a pointer. |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3507 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 612720d | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3508 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | 655694e | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3509 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3510 | return 0; |
| 3511 | else |
| 3512 | return VD; |
| 3513 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3514 | case Stmt::UnaryOperatorClass: { |
| 3515 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| 3516 | |
| 3517 | switch(UO->getOpcode()) { |
| 3518 | case UnaryOperator::Deref: { |
| 3519 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3520 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3521 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3522 | if (!VD || VD->getType()->isPointerType()) |
| 3523 | return 0; |
| 3524 | return VD; |
| 3525 | } |
| 3526 | return 0; |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3527 | } |
| 3528 | case UnaryOperator::Real: |
| 3529 | case UnaryOperator::Imag: |
| 3530 | case UnaryOperator::Extension: |
| 3531 | return getPrimaryDecl(UO->getSubExpr()); |
| 3532 | default: |
| 3533 | return 0; |
| 3534 | } |
| 3535 | } |
| 3536 | case Stmt::BinaryOperatorClass: { |
| 3537 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3538 | |
| 3539 | // Handle cases involving pointer arithmetic. The result of an |
| 3540 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3541 | |
| 3542 | // (x + n) or (n + x) => x |
| 3543 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3544 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3545 | return getPrimaryDecl(BO->getLHS()); |
| 3546 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3547 | return getPrimaryDecl(BO->getRHS()); |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | return 0; |
| 3552 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3553 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3554 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3555 | case Stmt::ImplicitCastExprClass: |
| 3556 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3557 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3558 | default: |
| 3559 | return 0; |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 3564 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 3565 | /// object cannot be declared with storage class register or be a bit field. |
| 3566 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 3567 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3568 | /// In C++, the operand might be an overloaded function name, in which case |
| 3569 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3570 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3571 | if (op->isTypeDependent()) |
| 3572 | return Context.DependentTy; |
| 3573 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3574 | if (getLangOptions().C99) { |
| 3575 | // Implement C99-only parts of addressof rules. |
| 3576 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 3577 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 3578 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 3579 | // (assuming the deref expression is valid). |
| 3580 | return uOp->getSubExpr()->getType(); |
| 3581 | } |
| 3582 | // Technically, there should be a check for array subscript |
| 3583 | // expressions here, but the result of one is always an lvalue anyway. |
| 3584 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3585 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 3586 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 3587 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3588 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3589 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 3590 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3591 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 3592 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3593 | return QualType(); |
| 3594 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3595 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 3596 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 3597 | if (Field->isBitField()) { |
| 3598 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3599 | << "bit-field" << op->getSourceRange(); |
| 3600 | return QualType(); |
| 3601 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3602 | } |
| 3603 | // Check for Apple extension for accessing vector components. |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3604 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 3605 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3606 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3607 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3608 | return QualType(); |
| 3609 | } else if (dcl) { // C99 6.5.3.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3610 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 3611 | // with the register storage-class specifier. |
| 3612 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 3613 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3614 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3615 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3616 | return QualType(); |
| 3617 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3618 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3619 | return Context.OverloadTy; |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3620 | } else if (isa<FieldDecl>(dcl)) { |
| 3621 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 3622 | // Could be a pointer to member, though, if there is an explicit |
| 3623 | // scope qualifier for the class. |
| 3624 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3625 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3626 | if (Ctx && Ctx->isRecord()) |
| 3627 | return Context.getMemberPointerType(op->getType(), |
| 3628 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3629 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3630 | } else if (isa<FunctionDecl>(dcl)) { |
| 3631 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3632 | // As above. |
| 3633 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3634 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3635 | if (Ctx && Ctx->isRecord()) |
| 3636 | return Context.getMemberPointerType(op->getType(), |
| 3637 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3638 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3639 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3640 | else |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3641 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3642 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3643 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3644 | // If the operand has type "type", the result has type "pointer to type". |
| 3645 | return Context.getPointerType(op->getType()); |
| 3646 | } |
| 3647 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3648 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
| 3649 | UsualUnaryConversions(Op); |
| 3650 | QualType Ty = Op->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3651 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3652 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 3653 | // incomplete type or void. It would be possible to warn about dereferencing |
| 3654 | // a void pointer, but it's completely well-defined, and such a warning is |
| 3655 | // unlikely to catch any mistakes. |
| 3656 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3657 | return PT->getPointeeType(); |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3658 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3659 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3660 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3661 | return QualType(); |
| 3662 | } |
| 3663 | |
| 3664 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 3665 | tok::TokenKind Kind) { |
| 3666 | BinaryOperator::Opcode Opc; |
| 3667 | switch (Kind) { |
| 3668 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3669 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 3670 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3671 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 3672 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 3673 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 3674 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 3675 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 3676 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 3677 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 3678 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 3679 | case tok::less: Opc = BinaryOperator::LT; break; |
| 3680 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 3681 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 3682 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 3683 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 3684 | case tok::amp: Opc = BinaryOperator::And; break; |
| 3685 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 3686 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 3687 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 3688 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 3689 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 3690 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 3691 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 3692 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 3693 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 3694 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 3695 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 3696 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 3697 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 3698 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 3699 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 3700 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 3701 | } |
| 3702 | return Opc; |
| 3703 | } |
| 3704 | |
| 3705 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 3706 | tok::TokenKind Kind) { |
| 3707 | UnaryOperator::Opcode Opc; |
| 3708 | switch (Kind) { |
| 3709 | default: assert(0 && "Unknown unary op!"); |
| 3710 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 3711 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 3712 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 3713 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 3714 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 3715 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 3716 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 3717 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3718 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 3719 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 3720 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 3721 | } |
| 3722 | return Opc; |
| 3723 | } |
| 3724 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3725 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 3726 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 3727 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3728 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 3729 | unsigned Op, |
| 3730 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3731 | QualType ResultTy; // Result type of the binary operator. |
| 3732 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 3733 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
| 3734 | |
| 3735 | switch (Opc) { |
| 3736 | default: |
| 3737 | assert(0 && "Unknown binary expr!"); |
| 3738 | case BinaryOperator::Assign: |
| 3739 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 3740 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3741 | case BinaryOperator::PtrMemD: |
| 3742 | case BinaryOperator::PtrMemI: |
| 3743 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 3744 | Opc == BinaryOperator::PtrMemI); |
| 3745 | break; |
| 3746 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3747 | case BinaryOperator::Div: |
| 3748 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 3749 | break; |
| 3750 | case BinaryOperator::Rem: |
| 3751 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 3752 | break; |
| 3753 | case BinaryOperator::Add: |
| 3754 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 3755 | break; |
| 3756 | case BinaryOperator::Sub: |
| 3757 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 3758 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3759 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3760 | case BinaryOperator::Shr: |
| 3761 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 3762 | break; |
| 3763 | case BinaryOperator::LE: |
| 3764 | case BinaryOperator::LT: |
| 3765 | case BinaryOperator::GE: |
| 3766 | case BinaryOperator::GT: |
| 3767 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, true); |
| 3768 | break; |
| 3769 | case BinaryOperator::EQ: |
| 3770 | case BinaryOperator::NE: |
| 3771 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, false); |
| 3772 | break; |
| 3773 | case BinaryOperator::And: |
| 3774 | case BinaryOperator::Xor: |
| 3775 | case BinaryOperator::Or: |
| 3776 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 3777 | break; |
| 3778 | case BinaryOperator::LAnd: |
| 3779 | case BinaryOperator::LOr: |
| 3780 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 3781 | break; |
| 3782 | case BinaryOperator::MulAssign: |
| 3783 | case BinaryOperator::DivAssign: |
| 3784 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 3785 | if (!CompTy.isNull()) |
| 3786 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3787 | break; |
| 3788 | case BinaryOperator::RemAssign: |
| 3789 | CompTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 3790 | if (!CompTy.isNull()) |
| 3791 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3792 | break; |
| 3793 | case BinaryOperator::AddAssign: |
| 3794 | CompTy = CheckAdditionOperands(lhs, rhs, OpLoc, true); |
| 3795 | if (!CompTy.isNull()) |
| 3796 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3797 | break; |
| 3798 | case BinaryOperator::SubAssign: |
| 3799 | CompTy = CheckSubtractionOperands(lhs, rhs, OpLoc, true); |
| 3800 | if (!CompTy.isNull()) |
| 3801 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3802 | break; |
| 3803 | case BinaryOperator::ShlAssign: |
| 3804 | case BinaryOperator::ShrAssign: |
| 3805 | CompTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 3806 | if (!CompTy.isNull()) |
| 3807 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3808 | break; |
| 3809 | case BinaryOperator::AndAssign: |
| 3810 | case BinaryOperator::XorAssign: |
| 3811 | case BinaryOperator::OrAssign: |
| 3812 | CompTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 3813 | if (!CompTy.isNull()) |
| 3814 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3815 | break; |
| 3816 | case BinaryOperator::Comma: |
| 3817 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 3818 | break; |
| 3819 | } |
| 3820 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3821 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3822 | if (CompTy.isNull()) |
| 3823 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 3824 | else |
| 3825 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Steve Naroff | 8b9a98d | 2009-01-20 21:06:31 +0000 | [diff] [blame] | 3826 | CompTy, OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3829 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3830 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 3831 | tok::TokenKind Kind, |
| 3832 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3833 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3834 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3835 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3836 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 3837 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3838 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3839 | // If either expression is type-dependent, just build the AST. |
| 3840 | // FIXME: We'll need to perform some caching of the result of name |
| 3841 | // lookup for operator+. |
| 3842 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3843 | if (Opc > BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) |
| 3844 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3845 | Context.DependentTy, |
| 3846 | Context.DependentTy, TokLoc)); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3847 | else |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3848 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, |
| 3849 | Context.DependentTy, TokLoc)); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3852 | if (getLangOptions().CPlusPlus && Opc != BinaryOperator::PtrMemD && |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3853 | (lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType() || |
| 3854 | rhs->getType()->isRecordType() || rhs->getType()->isEnumeralType())) { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3855 | // If this is one of the assignment operators, we only perform |
| 3856 | // overload resolution if the left-hand side is a class or |
| 3857 | // enumeration type (C++ [expr.ass]p3). |
| 3858 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3859 | !(lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType())) { |
| 3860 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
| 3861 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3862 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3863 | // Determine which overloaded operator we're dealing with. |
| 3864 | static const OverloadedOperatorKind OverOps[] = { |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3865 | // Overloading .* is not possible. |
| 3866 | static_cast<OverloadedOperatorKind>(0), OO_ArrowStar, |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3867 | OO_Star, OO_Slash, OO_Percent, |
| 3868 | OO_Plus, OO_Minus, |
| 3869 | OO_LessLess, OO_GreaterGreater, |
| 3870 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 3871 | OO_EqualEqual, OO_ExclaimEqual, |
| 3872 | OO_Amp, |
| 3873 | OO_Caret, |
| 3874 | OO_Pipe, |
| 3875 | OO_AmpAmp, |
| 3876 | OO_PipePipe, |
| 3877 | OO_Equal, OO_StarEqual, |
| 3878 | OO_SlashEqual, OO_PercentEqual, |
| 3879 | OO_PlusEqual, OO_MinusEqual, |
| 3880 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 3881 | OO_AmpEqual, OO_CaretEqual, |
| 3882 | OO_PipeEqual, |
| 3883 | OO_Comma |
| 3884 | }; |
| 3885 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3886 | |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3887 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 3888 | // to the candidate set. |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3889 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3890 | Expr *Args[2] = { lhs, rhs }; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3891 | if (AddOperatorCandidates(OverOp, S, TokLoc, Args, 2, CandidateSet)) |
| 3892 | return ExprError(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3893 | |
| 3894 | // Perform overload resolution. |
| 3895 | OverloadCandidateSet::iterator Best; |
| 3896 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3897 | case OR_Success: { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3898 | // We found a built-in operator or an overloaded operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3899 | FunctionDecl *FnDecl = Best->Function; |
| 3900 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3901 | if (FnDecl) { |
| 3902 | // We matched an overloaded operator. Build a call to that |
| 3903 | // operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3905 | // Convert the arguments. |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3906 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3907 | if (PerformObjectArgumentInitialization(lhs, Method) || |
| 3908 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(0)->getType(), |
| 3909 | "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3910 | return ExprError(); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3911 | } else { |
| 3912 | // Convert the arguments. |
| 3913 | if (PerformCopyInitialization(lhs, FnDecl->getParamDecl(0)->getType(), |
| 3914 | "passing") || |
| 3915 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(1)->getType(), |
| 3916 | "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3917 | return ExprError(); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3918 | } |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3920 | // Determine the result type |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3921 | QualType ResultTy |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3922 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3923 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3925 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3926 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3927 | SourceLocation()); |
Douglas Gregor | 65fedaf | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 3928 | UsualUnaryConversions(FnExpr); |
| 3929 | |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3930 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3931 | ResultTy, TokLoc)); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3932 | } else { |
| 3933 | // We matched a built-in operator. Convert the arguments, then |
| 3934 | // break out so that we will build the appropriate built-in |
| 3935 | // operator node. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3936 | if (PerformImplicitConversion(lhs, Best->BuiltinTypes.ParamTypes[0], |
| 3937 | Best->Conversions[0], "passing") || |
| 3938 | PerformImplicitConversion(rhs, Best->BuiltinTypes.ParamTypes[1], |
| 3939 | Best->Conversions[1], "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3940 | return ExprError(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3941 | |
| 3942 | break; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3943 | } |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | case OR_No_Viable_Function: |
| 3947 | // No viable function; fall through to handling this as a |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3948 | // built-in operator, which will produce an error message for us. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3949 | break; |
| 3950 | |
| 3951 | case OR_Ambiguous: |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3952 | Diag(TokLoc, diag::err_ovl_ambiguous_oper) |
| 3953 | << BinaryOperator::getOpcodeStr(Opc) |
| 3954 | << lhs->getSourceRange() << rhs->getSourceRange(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3955 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3956 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 3957 | |
| 3958 | case OR_Deleted: |
| 3959 | Diag(TokLoc, diag::err_ovl_deleted_oper) |
| 3960 | << Best->Function->isDeleted() |
| 3961 | << BinaryOperator::getOpcodeStr(Opc) |
| 3962 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 3963 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3964 | return ExprError(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3967 | // Either we found no viable overloaded operator or we matched a |
| 3968 | // built-in operator. In either case, fall through to trying to |
| 3969 | // build a built-in operation. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3972 | // Build a built-in binary operation. |
| 3973 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | // Unary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3977 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 3978 | tok::TokenKind Op, ExprArg input) { |
| 3979 | // FIXME: Input is modified later, but smart pointer not reassigned. |
| 3980 | Expr *Input = (Expr*)input.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3981 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3982 | |
| 3983 | if (getLangOptions().CPlusPlus && |
| 3984 | (Input->getType()->isRecordType() |
| 3985 | || Input->getType()->isEnumeralType())) { |
| 3986 | // Determine which overloaded operator we're dealing with. |
| 3987 | static const OverloadedOperatorKind OverOps[] = { |
| 3988 | OO_None, OO_None, |
| 3989 | OO_PlusPlus, OO_MinusMinus, |
| 3990 | OO_Amp, OO_Star, |
| 3991 | OO_Plus, OO_Minus, |
| 3992 | OO_Tilde, OO_Exclaim, |
| 3993 | OO_None, OO_None, |
| 3994 | OO_None, |
| 3995 | OO_None |
| 3996 | }; |
| 3997 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3998 | |
| 3999 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 4000 | // to the candidate set. |
| 4001 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 4002 | if (OverOp != OO_None && |
| 4003 | AddOperatorCandidates(OverOp, S, OpLoc, &Input, 1, CandidateSet)) |
| 4004 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4005 | |
| 4006 | // Perform overload resolution. |
| 4007 | OverloadCandidateSet::iterator Best; |
| 4008 | switch (BestViableFunction(CandidateSet, Best)) { |
| 4009 | case OR_Success: { |
| 4010 | // We found a built-in operator or an overloaded operator. |
| 4011 | FunctionDecl *FnDecl = Best->Function; |
| 4012 | |
| 4013 | if (FnDecl) { |
| 4014 | // We matched an overloaded operator. Build a call to that |
| 4015 | // operator. |
| 4016 | |
| 4017 | // Convert the arguments. |
| 4018 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4019 | if (PerformObjectArgumentInitialization(Input, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4020 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4021 | } else { |
| 4022 | // Convert the arguments. |
| 4023 | if (PerformCopyInitialization(Input, |
| 4024 | FnDecl->getParamDecl(0)->getType(), |
| 4025 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4026 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4030 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4031 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4032 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4033 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4034 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4035 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4036 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4037 | UsualUnaryConversions(FnExpr); |
| 4038 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4039 | input.release(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4040 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, &Input, 1, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4041 | ResultTy, OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4042 | } else { |
| 4043 | // We matched a built-in operator. Convert the arguments, then |
| 4044 | // break out so that we will build the appropriate built-in |
| 4045 | // operator node. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4046 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4047 | Best->Conversions[0], "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4048 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4049 | |
| 4050 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4051 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | case OR_No_Viable_Function: |
| 4055 | // No viable function; fall through to handling this as a |
| 4056 | // built-in operator, which will produce an error message for us. |
| 4057 | break; |
| 4058 | |
| 4059 | case OR_Ambiguous: |
| 4060 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4061 | << UnaryOperator::getOpcodeStr(Opc) |
| 4062 | << Input->getSourceRange(); |
| 4063 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4064 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame^] | 4065 | |
| 4066 | case OR_Deleted: |
| 4067 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4068 | << Best->Function->isDeleted() |
| 4069 | << UnaryOperator::getOpcodeStr(Opc) |
| 4070 | << Input->getSourceRange(); |
| 4071 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4072 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | // Either we found no viable overloaded operator or we matched a |
| 4076 | // built-in operator. In either case, fall through to trying to |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4077 | // build a built-in operation. |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4080 | QualType resultType; |
| 4081 | switch (Opc) { |
| 4082 | default: |
| 4083 | assert(0 && "Unimplemented unary expr!"); |
| 4084 | case UnaryOperator::PreInc: |
| 4085 | case UnaryOperator::PreDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4086 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4087 | Opc == UnaryOperator::PreInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4088 | break; |
| 4089 | case UnaryOperator::AddrOf: |
| 4090 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4091 | break; |
| 4092 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4093 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4094 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4095 | break; |
| 4096 | case UnaryOperator::Plus: |
| 4097 | case UnaryOperator::Minus: |
| 4098 | UsualUnaryConversions(Input); |
| 4099 | resultType = Input->getType(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4100 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4101 | break; |
| 4102 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4103 | resultType->isEnumeralType()) |
| 4104 | break; |
| 4105 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4106 | Opc == UnaryOperator::Plus && |
| 4107 | resultType->isPointerType()) |
| 4108 | break; |
| 4109 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4110 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4111 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4112 | case UnaryOperator::Not: // bitwise complement |
| 4113 | UsualUnaryConversions(Input); |
| 4114 | resultType = Input->getType(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4115 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4116 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4117 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4118 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4119 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4120 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4121 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4122 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4123 | break; |
| 4124 | case UnaryOperator::LNot: // logical negation |
| 4125 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 4126 | DefaultFunctionArrayConversion(Input); |
| 4127 | resultType = Input->getType(); |
| 4128 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4129 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4130 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4131 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4132 | // In C++, it's bool. C++ 5.3.1p8 |
| 4133 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4134 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4135 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4136 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4137 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4138 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4139 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4140 | resultType = Input->getType(); |
| 4141 | break; |
| 4142 | } |
| 4143 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4144 | return ExprError(); |
| 4145 | input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4146 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4149 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 4150 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4151 | SourceLocation LabLoc, |
| 4152 | IdentifierInfo *LabelII) { |
| 4153 | // Look up the record for this label identifier. |
| 4154 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 4155 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4156 | // If we haven't seen this label yet, create a forward reference. It |
| 4157 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4158 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4159 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4160 | |
| 4161 | // Create the AST node. The address of a label always has type 'void*'. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4162 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4163 | Context.getPointerType(Context.VoidTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4166 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4167 | SourceLocation RPLoc) { // "({..})" |
| 4168 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 4169 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4170 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4171 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4172 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 4173 | if (isFileScope) { |
| 4174 | return Diag(LPLoc, diag::err_stmtexpr_file_scope); |
| 4175 | } |
| 4176 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4177 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4178 | // example, it is not possible to goto into a stmt expression apparently. |
| 4179 | // More semantic analysis is needed. |
| 4180 | |
| 4181 | // FIXME: the last statement in the compount stmt has its value used. We |
| 4182 | // should not warn about it being unused. |
| 4183 | |
| 4184 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4185 | // as the type of the stmtexpr. |
| 4186 | QualType Ty = Context.VoidTy; |
| 4187 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4188 | if (!Compound->body_empty()) { |
| 4189 | Stmt *LastStmt = Compound->body_back(); |
| 4190 | // If LastStmt is a label, skip down through into the body. |
| 4191 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4192 | LastStmt = Label->getSubStmt(); |
| 4193 | |
| 4194 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4195 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4196 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4197 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4198 | return new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4199 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4200 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4201 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4202 | SourceLocation BuiltinLoc, |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4203 | SourceLocation TypeLoc, |
| 4204 | TypeTy *argty, |
| 4205 | OffsetOfComponent *CompPtr, |
| 4206 | unsigned NumComponents, |
| 4207 | SourceLocation RPLoc) { |
| 4208 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4209 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 4210 | |
| 4211 | // We must have at least one component that refers to the type, and the first |
| 4212 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4213 | // a struct/union/class. |
| 4214 | if (!ArgTy->isRecordType()) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4215 | return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4216 | |
| 4217 | // Otherwise, create a compound literal expression as the base, and |
| 4218 | // iteratively process the offsetof designators. |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4219 | InitListExpr *IList = |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 4220 | new (Context) InitListExpr(SourceLocation(), 0, 0, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4221 | IList->setType(ArgTy); |
| 4222 | Expr *Res = |
| 4223 | new (Context) CompoundLiteralExpr(SourceLocation(), ArgTy, IList, false); |
| 4224 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4225 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4226 | // GCC extension, diagnose them. |
| 4227 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4228 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4229 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4230 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4231 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4232 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4233 | if (OC.isBrackets) { |
| 4234 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4235 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4236 | if (!AT) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4237 | Res->Destroy(Context); |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4238 | return Diag(OC.LocEnd, diag::err_offsetof_array_type) << Res->getType(); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4241 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4242 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4243 | // C99 6.5.2.1p1 |
| 4244 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 4245 | if (!Idx->getType()->isIntegerType()) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4246 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript) |
| 4247 | << Idx->getSourceRange(); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4248 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4249 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4250 | OC.LocEnd); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4251 | continue; |
| 4252 | } |
| 4253 | |
| 4254 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4255 | if (!RC) { |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4256 | Res->Destroy(Context); |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4257 | return Diag(OC.LocEnd, diag::err_offsetof_record_type) << Res->getType(); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | // Get the decl corresponding to this. |
| 4261 | RecordDecl *RD = RC->getDecl(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4262 | FieldDecl *MemberDecl |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 4263 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4264 | LookupMemberName) |
| 4265 | .getAsDecl()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4266 | if (!MemberDecl) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 4267 | return Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4268 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd); |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4269 | |
| 4270 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4271 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 4272 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4273 | // matter here. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4274 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 4275 | MemberDecl->getType().getNonReferenceType()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4278 | return new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 4279 | Context.getSizeType(), BuiltinLoc); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4283 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4284 | TypeTy *arg1, TypeTy *arg2, |
| 4285 | SourceLocation RPLoc) { |
| 4286 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4287 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 4288 | |
| 4289 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 4290 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4291 | return new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, |
| 4292 | argT2, RPLoc); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4295 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4296 | ExprTy *expr1, ExprTy *expr2, |
| 4297 | SourceLocation RPLoc) { |
| 4298 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 4299 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 4300 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 4301 | |
| 4302 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4303 | |
| 4304 | // The conditional expression is required to be a constant expression. |
| 4305 | llvm::APSInt condEval(32); |
| 4306 | SourceLocation ExpLoc; |
| 4307 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4308 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant) |
| 4309 | << CondExpr->getSourceRange(); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4310 | |
| 4311 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4312 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 4313 | RHSExpr->getType(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4314 | return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 4315 | resType, RPLoc); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4318 | //===----------------------------------------------------------------------===// |
| 4319 | // Clang Extensions. |
| 4320 | //===----------------------------------------------------------------------===// |
| 4321 | |
| 4322 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4323 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4324 | // Analyze block parameters. |
| 4325 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
| 4326 | |
| 4327 | // Add BSI to CurBlock. |
| 4328 | BSI->PrevBlockInfo = CurBlock; |
| 4329 | CurBlock = BSI; |
| 4330 | |
| 4331 | BSI->ReturnType = 0; |
| 4332 | BSI->TheScope = BlockScope; |
| 4333 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4334 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4335 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4338 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| 4339 | assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); |
| 4340 | |
| 4341 | if (ParamInfo.getNumTypeObjects() == 0 |
| 4342 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
| 4343 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4344 | |
| 4345 | // The type is entirely optional as well, if none, use DependentTy. |
| 4346 | if (T.isNull()) |
| 4347 | T = Context.DependentTy; |
| 4348 | |
| 4349 | // The parameter list is optional, if there was none, assume (). |
| 4350 | if (!T->isFunctionType()) |
| 4351 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 4352 | |
| 4353 | CurBlock->hasPrototype = true; |
| 4354 | CurBlock->isVariadic = false; |
| 4355 | Type *RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4356 | .getTypePtr(); |
| 4357 | |
| 4358 | if (!RetTy->isDependentType()) |
| 4359 | CurBlock->ReturnType = RetTy; |
| 4360 | return; |
| 4361 | } |
| 4362 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4363 | // Analyze arguments to block. |
| 4364 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4365 | "Not a function declarator!"); |
| 4366 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4367 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4368 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4369 | CurBlock->isVariadic = true; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4370 | |
| 4371 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4372 | // no arguments, not a function that takes a single void argument. |
| 4373 | if (FTI.hasPrototype && |
| 4374 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4375 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 4376 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 4377 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4378 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4379 | } else if (FTI.hasPrototype) { |
| 4380 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4381 | CurBlock->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 4382 | CurBlock->isVariadic = FTI.isVariadic; |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4383 | QualType T = GetTypeForDeclarator (ParamInfo, CurScope); |
| 4384 | |
| 4385 | Type* RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4386 | .getTypePtr(); |
| 4387 | |
| 4388 | if (!RetTy->isDependentType()) |
| 4389 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4390 | } |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4391 | CurBlock->TheDecl->setArgs(&CurBlock->Params[0], CurBlock->Params.size()); |
| 4392 | |
| 4393 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4394 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4395 | // If this has an identifier, add it to the scope stack. |
| 4396 | if ((*AI)->getIdentifier()) |
| 4397 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4401 | /// is invoked to pop the information about the block from the action impl. |
| 4402 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4403 | // Ensure that CurBlock is deleted. |
| 4404 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
| 4405 | |
| 4406 | // Pop off CurBlock, handle nested blocks. |
| 4407 | CurBlock = CurBlock->PrevBlockInfo; |
| 4408 | |
| 4409 | // FIXME: Delete the ParmVarDecl objects as well??? |
| 4410 | |
| 4411 | } |
| 4412 | |
| 4413 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4414 | /// literal was successfully completed. ^(int x){...} |
| 4415 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 4416 | Scope *CurScope) { |
| 4417 | // Ensure that CurBlock is deleted. |
| 4418 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4419 | ExprOwningPtr<CompoundStmt> Body(this, static_cast<CompoundStmt*>(body)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4420 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4421 | PopDeclContext(); |
| 4422 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4423 | // Pop off CurBlock, handle nested blocks. |
| 4424 | CurBlock = CurBlock->PrevBlockInfo; |
| 4425 | |
| 4426 | QualType RetTy = Context.VoidTy; |
| 4427 | if (BSI->ReturnType) |
| 4428 | RetTy = QualType(BSI->ReturnType, 0); |
| 4429 | |
| 4430 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4431 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4432 | ArgTypes.push_back(BSI->Params[i]->getType()); |
| 4433 | |
| 4434 | QualType BlockTy; |
| 4435 | if (!BSI->hasPrototype) |
| 4436 | BlockTy = Context.getFunctionTypeNoProto(RetTy); |
| 4437 | else |
| 4438 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4439 | BSI->isVariadic, 0); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4440 | |
| 4441 | BlockTy = Context.getBlockPointerType(BlockTy); |
Steve Naroff | 9ac456d | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4442 | |
Steve Naroff | 95029d9 | 2008-10-08 18:44:00 +0000 | [diff] [blame] | 4443 | BSI->TheDecl->setBody(Body.take()); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4444 | return new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4447 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4448 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4449 | /// The number of arguments has already been validated to match the number of |
| 4450 | /// arguments in FnType. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4451 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType, |
| 4452 | ASTContext &Context) { |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4453 | unsigned NumParams = FnType->getNumArgs(); |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4454 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4455 | QualType ExprTy = Context.getCanonicalType(Args[i]->getType()); |
| 4456 | QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i)); |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4457 | |
| 4458 | if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType()) |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4459 | return false; |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4460 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4461 | return true; |
| 4462 | } |
| 4463 | |
| 4464 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 4465 | SourceLocation *CommaLocs, |
| 4466 | SourceLocation BuiltinLoc, |
| 4467 | SourceLocation RParenLoc) { |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4468 | // __builtin_overload requires at least 2 arguments |
| 4469 | if (NumArgs < 2) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4470 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4471 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4472 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4473 | // The first argument is required to be a constant expression. It tells us |
| 4474 | // the number of arguments to pass to each of the functions to be overloaded. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4475 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4476 | Expr *NParamsExpr = Args[0]; |
| 4477 | llvm::APSInt constEval(32); |
| 4478 | SourceLocation ExpLoc; |
| 4479 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4480 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4481 | << NParamsExpr->getSourceRange(); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4482 | |
| 4483 | // Verify that the number of parameters is > 0 |
| 4484 | unsigned NumParams = constEval.getZExtValue(); |
| 4485 | if (NumParams == 0) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4486 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4487 | << NParamsExpr->getSourceRange(); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4488 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 4489 | if ((NumParams + 1) > NumArgs) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4490 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4491 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4492 | |
| 4493 | // Figure out the return type, by matching the args to one of the functions |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4494 | // listed after the parameters. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4495 | OverloadExpr *OE = 0; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4496 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 4497 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 4498 | // pointer to function. |
| 4499 | Expr *Fn = UsualUnaryConversions(Args[i]); |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4500 | const FunctionTypeProto *FnType = 0; |
| 4501 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) |
| 4502 | FnType = PT->getPointeeType()->getAsFunctionTypeProto(); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4503 | |
| 4504 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 4505 | // parameters, and the number of parameters must match the value passed to |
| 4506 | // the builtin. |
| 4507 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4508 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype) |
| 4509 | << Fn->getSourceRange(); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4510 | |
| 4511 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4512 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4513 | // If they match, return a new OverloadExpr. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4514 | if (ExprsMatchFnType(Args+1, FnType, Context)) { |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4515 | if (OE) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4516 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match) |
| 4517 | << OE->getFn()->getSourceRange(); |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4518 | // Remember our match, and continue processing the remaining arguments |
| 4519 | // to catch any errors. |
Ted Kremenek | f1a6712 | 2009-02-09 17:08:14 +0000 | [diff] [blame] | 4520 | OE = new (Context) OverloadExpr(Context, Args, NumArgs, i, |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4521 | FnType->getResultType().getNonReferenceType(), |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4522 | BuiltinLoc, RParenLoc); |
| 4523 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4524 | } |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4525 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 4526 | // exactly one of the candidate functions. |
| 4527 | if (OE) |
| 4528 | return OE; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4529 | |
| 4530 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 4531 | // the return an error. |
| 4532 | std::string typeNames; |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4533 | for (unsigned i = 0; i != NumParams; ++i) { |
| 4534 | if (i != 0) typeNames += ", "; |
| 4535 | typeNames += Args[i+1]->getType().getAsString(); |
| 4536 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4537 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4538 | return Diag(BuiltinLoc, diag::err_overload_no_match) |
| 4539 | << typeNames << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4542 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4543 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4544 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4545 | Expr *E = static_cast<Expr*>(expr); |
| 4546 | QualType T = QualType::getFromOpaquePtr(type); |
| 4547 | |
| 4548 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4549 | |
| 4550 | // Get the va_list type |
| 4551 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4552 | // Deal with implicit array decay; for example, on x86-64, |
| 4553 | // va_list is an array, but it's supposed to decay to |
| 4554 | // a pointer for va_arg. |
| 4555 | if (VaListType->isArrayType()) |
| 4556 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 8754e5b | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4557 | // Make sure the input expression also decays appropriately. |
| 4558 | UsualUnaryConversions(E); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4559 | |
| 4560 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4561 | return Diag(E->getLocStart(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4562 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4563 | << E->getType() << E->getSourceRange(); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4564 | |
| 4565 | // FIXME: Warn if a non-POD type is passed in. |
| 4566 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4567 | return new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), RPLoc); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4570 | Sema::ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| 4571 | // The type of __null will be int or long, depending on the size of |
| 4572 | // pointers on the target. |
| 4573 | QualType Ty; |
| 4574 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4575 | Ty = Context.IntTy; |
| 4576 | else |
| 4577 | Ty = Context.LongTy; |
| 4578 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4579 | return new (Context) GNUNullExpr(Ty, TokenLoc); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4582 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4583 | SourceLocation Loc, |
| 4584 | QualType DstType, QualType SrcType, |
| 4585 | Expr *SrcExpr, const char *Flavor) { |
| 4586 | // Decode the result (notice that AST's are still created for extensions). |
| 4587 | bool isInvalid = false; |
| 4588 | unsigned DiagKind; |
| 4589 | switch (ConvTy) { |
| 4590 | default: assert(0 && "Unknown conversion type"); |
| 4591 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4592 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4593 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4594 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4595 | case IntToPointer: |
| 4596 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4597 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4598 | case IncompatiblePointer: |
| 4599 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4600 | break; |
| 4601 | case FunctionVoidPointer: |
| 4602 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4603 | break; |
| 4604 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4605 | // If the qualifiers lost were because we were applying the |
| 4606 | // (deprecated) C++ conversion from a string literal to a char* |
| 4607 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4608 | // Ideally, this check would be performed in |
| 4609 | // CheckPointerTypesForAssignment. However, that would require a |
| 4610 | // bit of refactoring (so that the second argument is an |
| 4611 | // expression, rather than a type), which should be done as part |
| 4612 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4613 | // C++ semantics. |
| 4614 | if (getLangOptions().CPlusPlus && |
| 4615 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4616 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4617 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4618 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4619 | case IntToBlockPointer: |
| 4620 | DiagKind = diag::err_int_to_block_pointer; |
| 4621 | break; |
| 4622 | case IncompatibleBlockPointer: |
Steve Naroff | 82324d6 | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4623 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4624 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4625 | case IncompatibleObjCQualifiedId: |
| 4626 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
| 4627 | // it can give a more specific diagnostic. |
| 4628 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4629 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4630 | case IncompatibleVectors: |
| 4631 | DiagKind = diag::warn_incompatible_vectors; |
| 4632 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4633 | case Incompatible: |
| 4634 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4635 | isInvalid = true; |
| 4636 | break; |
| 4637 | } |
| 4638 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4639 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4640 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4641 | return isInvalid; |
| 4642 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4643 | |
| 4644 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4645 | { |
| 4646 | Expr::EvalResult EvalResult; |
| 4647 | |
| 4648 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
| 4649 | EvalResult.HasSideEffects) { |
| 4650 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4651 | |
| 4652 | if (EvalResult.Diag) { |
| 4653 | // We only show the note if it's not the usual "invalid subexpression" |
| 4654 | // or if it's actually in a subexpression. |
| 4655 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4656 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4657 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4658 | } |
| 4659 | |
| 4660 | return true; |
| 4661 | } |
| 4662 | |
| 4663 | if (EvalResult.Diag) { |
| 4664 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 4665 | E->getSourceRange(); |
| 4666 | |
| 4667 | // Print the reason it's not a constant. |
| 4668 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4669 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4670 | } |
| 4671 | |
| 4672 | if (Result) |
| 4673 | *Result = EvalResult.Val.getInt(); |
| 4674 | return false; |
| 4675 | } |