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 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 61 | MD = Impl->getClassInterface()->getMethod(Context, |
| 62 | MD->getSelector(), |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 63 | MD->isInstanceMethod()); |
| 64 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!isSilenced) |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 70 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 71 | } |
| 72 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 73 | // See if this is a deleted function. |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 74 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 75 | if (FD->isDeleted()) { |
| 76 | Diag(Loc, diag::err_deleted_function_use); |
| 77 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 78 | return true; |
| 79 | } |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 80 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 81 | |
| 82 | // See if the decl is unavailable |
| 83 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 84 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 86 | } |
| 87 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 91 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 92 | Expr *Ex = (Expr *)E; |
| 93 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 94 | } |
| 95 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | // Standard Promotions and Conversions |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 100 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 101 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 102 | QualType Ty = E->getType(); |
| 103 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 104 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 105 | if (Ty->isFunctionType()) |
| 106 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 107 | else if (Ty->isArrayType()) { |
| 108 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 109 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 110 | // type 'array of type' is converted to an expression that has type 'pointer |
| 111 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 112 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 113 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 114 | // |
| 115 | // C++ 4.2p1: |
| 116 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 117 | // T" can be converted to an rvalue of type "pointer to T". |
| 118 | // |
| 119 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 120 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 121 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 122 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 126 | /// operators (C99 6.3). The conversions of array and function types are |
| 127 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 128 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 129 | /// In these instances, this routine should *not* be called. |
| 130 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 131 | QualType Ty = Expr->getType(); |
| 132 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 133 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 134 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 135 | ImpCastExprToType(Expr, Context.IntTy); |
| 136 | else |
| 137 | DefaultFunctionArrayConversion(Expr); |
| 138 | |
| 139 | return Expr; |
| 140 | } |
| 141 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 142 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 143 | /// do not have a prototype. Arguments that have type float are promoted to |
| 144 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 145 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 146 | QualType Ty = Expr->getType(); |
| 147 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 148 | |
| 149 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 150 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 151 | if (BT->getKind() == BuiltinType::Float) |
| 152 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 153 | |
| 154 | UsualUnaryConversions(Expr); |
| 155 | } |
| 156 | |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 157 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 158 | // will warn if the resulting type is not a POD type. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 159 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 160 | DefaultArgumentPromotion(Expr); |
| 161 | |
| 162 | if (!Expr->getType()->isPODType()) { |
| 163 | Diag(Expr->getLocStart(), |
| 164 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 165 | Expr->getType() << CT; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 170 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 171 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 172 | /// routine returns the first non-arithmetic type found. The client is |
| 173 | /// responsible for emitting appropriate error diagnostics. |
| 174 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 175 | /// GCC. |
| 176 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 177 | bool isCompAssign) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 178 | if (!isCompAssign) |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 179 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 180 | |
| 181 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 183 | // For conversion purposes, we ignore any qualifiers. |
| 184 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 185 | QualType lhs = |
| 186 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 187 | QualType rhs = |
| 188 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 189 | |
| 190 | // If both types are identical, no conversion is needed. |
| 191 | if (lhs == rhs) |
| 192 | return lhs; |
| 193 | |
| 194 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 195 | // The caller can deal with this (e.g. pointer + int). |
| 196 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 197 | return lhs; |
| 198 | |
| 199 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 200 | if (!isCompAssign) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 201 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 202 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 203 | return destType; |
| 204 | } |
| 205 | |
| 206 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 207 | // Perform the usual unary conversions. We do this early so that |
| 208 | // integral promotions to "int" can allow us to exit early, in the |
| 209 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 210 | // qualifiers. For example, "const float" and "float" are |
| 211 | // equivalent. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 212 | if (lhs->isPromotableIntegerType()) |
| 213 | lhs = Context.IntTy; |
| 214 | else |
| 215 | lhs = lhs.getUnqualifiedType(); |
| 216 | if (rhs->isPromotableIntegerType()) |
| 217 | rhs = Context.IntTy; |
| 218 | else |
| 219 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 221 | // If both types are identical, no conversion is needed. |
| 222 | if (lhs == rhs) |
| 223 | return lhs; |
| 224 | |
| 225 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 226 | // The caller can deal with this (e.g. pointer + int). |
| 227 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 228 | return lhs; |
| 229 | |
| 230 | // At this point, we have two different arithmetic types. |
| 231 | |
| 232 | // Handle complex types first (C99 6.3.1.8p1). |
| 233 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 234 | // if we have an integer operand, the result is the complex type. |
| 235 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 236 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 237 | return lhs; |
| 238 | } |
| 239 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 240 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 241 | return rhs; |
| 242 | } |
| 243 | // This handles complex/complex, complex/float, or float/complex. |
| 244 | // When both operands are complex, the shorter operand is converted to the |
| 245 | // type of the longer, and that is the type of the result. This corresponds |
| 246 | // to what is done when combining two real floating-point operands. |
| 247 | // The fun begins when size promotion occur across type domains. |
| 248 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 249 | // floating-point type, the less precise type is converted, within it's |
| 250 | // real or complex domain, to the precision of the other type. For example, |
| 251 | // when combining a "long double" with a "double _Complex", the |
| 252 | // "double _Complex" is promoted to "long double _Complex". |
| 253 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 254 | |
| 255 | if (result > 0) { // The left side is bigger, convert rhs. |
| 256 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 257 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 258 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | } |
| 260 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 261 | // domains match. This is a requirement for our implementation, C99 |
| 262 | // does not require this promotion. |
| 263 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 264 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 265 | return rhs; |
| 266 | } else { // handle "_Complex double, double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 267 | return lhs; |
| 268 | } |
| 269 | } |
| 270 | return lhs; // The domain/size match exactly. |
| 271 | } |
| 272 | // Now handle "real" floating types (i.e. float, double, long double). |
| 273 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 274 | // if we have an integer operand, the result is the real floating type. |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 275 | if (rhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 276 | // convert rhs to the lhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 277 | return lhs; |
| 278 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 279 | if (rhs->isComplexIntegerType()) { |
| 280 | // convert rhs to the complex floating point type. |
| 281 | return Context.getComplexType(lhs); |
| 282 | } |
| 283 | if (lhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 284 | // convert lhs to the rhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 285 | return rhs; |
| 286 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 287 | if (lhs->isComplexIntegerType()) { |
| 288 | // convert lhs to the complex floating point type. |
| 289 | return Context.getComplexType(rhs); |
| 290 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 291 | // We have two real floating types, float/complex combos were handled above. |
| 292 | // Convert the smaller operand to the bigger result. |
| 293 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 294 | if (result > 0) // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 295 | return lhs; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 296 | assert(result < 0 && "illegal float comparison"); |
| 297 | return rhs; // convert the lhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 298 | } |
| 299 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 300 | // Handle GCC complex int extension. |
| 301 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 302 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 303 | |
| 304 | if (lhsComplexInt && rhsComplexInt) { |
| 305 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 306 | rhsComplexInt->getElementType()) >= 0) |
| 307 | return lhs; // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 308 | return rhs; |
| 309 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 310 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 311 | return lhs; |
| 312 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 313 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 314 | return rhs; |
| 315 | } |
| 316 | } |
| 317 | // Finally, we have two differing integer types. |
| 318 | // The rules for this case are in C99 6.3.1.8 |
| 319 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 320 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 321 | rhsSigned = rhs->isSignedIntegerType(); |
| 322 | QualType destType; |
| 323 | if (lhsSigned == rhsSigned) { |
| 324 | // Same signedness; use the higher-ranked type |
| 325 | destType = compare >= 0 ? lhs : rhs; |
| 326 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 327 | // The unsigned type has greater than or equal rank to the |
| 328 | // signed type, so use the unsigned type |
| 329 | destType = lhsSigned ? rhs : lhs; |
| 330 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 331 | // The two types are different widths; if we are here, that |
| 332 | // means the signed type is larger than the unsigned type, so |
| 333 | // use the signed type. |
| 334 | destType = lhsSigned ? lhs : rhs; |
| 335 | } else { |
| 336 | // The signed type is higher-ranked than the unsigned type, |
| 337 | // but isn't actually any bigger (like unsigned int and long |
| 338 | // on most 32-bit systems). Use the unsigned type corresponding |
| 339 | // to the signed type. |
| 340 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 341 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 342 | return destType; |
| 343 | } |
| 344 | |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | // Semantic Analysis for various Expression Types |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
| 349 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 350 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 351 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 352 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 353 | /// multiple tokens. However, the common case is that StringToks points to one |
| 354 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 355 | /// |
| 356 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 357 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 358 | assert(NumStringToks && "Must have at least one string!"); |
| 359 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 360 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 361 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 362 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 363 | |
| 364 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 365 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 366 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 367 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 368 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 369 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 370 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 371 | |
| 372 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 373 | if (getLangOptions().CPlusPlus) |
| 374 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 375 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 376 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 377 | // the nul terminator character as well as the string length for pascal |
| 378 | // strings. |
| 379 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 380 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 381 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 383 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 384 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 385 | Literal.GetStringLength(), |
| 386 | Literal.AnyWide, StrTy, |
| 387 | &StringTokLocs[0], |
| 388 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 391 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 392 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 393 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 394 | /// for values inside the block or for globals). |
| 395 | /// |
| 396 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 397 | /// function references, etc which is suboptimal :) and breaks |
| 398 | /// things like "integer constant expression" tests. |
| 399 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 400 | ValueDecl *VD) { |
| 401 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 402 | // we wanted to. |
| 403 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 404 | return false; |
| 405 | |
| 406 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 407 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 408 | return false; |
| 409 | |
| 410 | // If this is a reference to an extern, static, or global variable, no need to |
| 411 | // snapshot it. |
| 412 | // FIXME: What about 'const' variables in C++? |
| 413 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 414 | return Var->hasLocalStorage(); |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 421 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 422 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 423 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 424 | /// 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] | 425 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 426 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 427 | IdentifierInfo &II, |
| 428 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 429 | const CXXScopeSpec *SS, |
| 430 | bool isAddressOfOperand) { |
| 431 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 432 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 435 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 436 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 437 | /// nested-name-specifier. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 438 | DeclRefExpr * |
| 439 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 440 | bool TypeDependent, bool ValueDependent, |
| 441 | const CXXScopeSpec *SS) { |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 442 | if (SS && !SS->isEmpty()) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 443 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
| 444 | ValueDependent, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 445 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 446 | } else |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 447 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 450 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 451 | /// variable corresponding to the anonymous union or struct whose type |
| 452 | /// is Record. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 453 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 454 | RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 455 | assert(Record->isAnonymousStructOrUnion() && |
| 456 | "Record must be an anonymous struct or union!"); |
| 457 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 458 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 459 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 460 | // vector (which itself will be eliminated). DeclGroups might make |
| 461 | // this even better. |
| 462 | DeclContext *Ctx = Record->getDeclContext(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 463 | for (DeclContext::decl_iterator D = Ctx->decls_begin(Context), |
| 464 | DEnd = Ctx->decls_end(Context); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 465 | D != DEnd; ++D) { |
| 466 | if (*D == Record) { |
| 467 | // The object for the anonymous struct/union directly |
| 468 | // follows its type in the list of declarations. |
| 469 | ++D; |
| 470 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 471 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 472 | return *D; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | assert(false && "Missing object for anonymous record"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 480 | Sema::OwningExprResult |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 481 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 482 | FieldDecl *Field, |
| 483 | Expr *BaseObjectExpr, |
| 484 | SourceLocation OpLoc) { |
| 485 | assert(Field->getDeclContext()->isRecord() && |
| 486 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 487 | && "Field must be stored inside an anonymous struct or union"); |
| 488 | |
| 489 | // Construct the sequence of field member references |
| 490 | // we'll have to perform to get to the field in the anonymous |
| 491 | // union/struct. The list of members is built from the field |
| 492 | // outward, so traverse it backwards to go from an object in |
| 493 | // the current context to the field we found. |
| 494 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 495 | AnonFields.push_back(Field); |
| 496 | VarDecl *BaseObject = 0; |
| 497 | DeclContext *Ctx = Field->getDeclContext(); |
| 498 | do { |
| 499 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 500 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 501 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 502 | AnonFields.push_back(AnonField); |
| 503 | else { |
| 504 | BaseObject = cast<VarDecl>(AnonObject); |
| 505 | break; |
| 506 | } |
| 507 | Ctx = Ctx->getParent(); |
| 508 | } while (Ctx->isRecord() && |
| 509 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 510 | |
| 511 | // Build the expression that refers to the base object, from |
| 512 | // which we will build a sequence of member references to each |
| 513 | // of the anonymous union objects and, eventually, the field we |
| 514 | // found via name lookup. |
| 515 | bool BaseObjectIsPointer = false; |
| 516 | unsigned ExtraQuals = 0; |
| 517 | if (BaseObject) { |
| 518 | // BaseObject is an anonymous struct/union variable (and is, |
| 519 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 520 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 521 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 522 | SourceLocation()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 523 | ExtraQuals |
| 524 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 525 | } else if (BaseObjectExpr) { |
| 526 | // The caller provided the base object expression. Determine |
| 527 | // whether its a pointer and whether it adds any qualifiers to the |
| 528 | // anonymous struct/union fields we're looking into. |
| 529 | QualType ObjectType = BaseObjectExpr->getType(); |
| 530 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 531 | BaseObjectIsPointer = true; |
| 532 | ObjectType = ObjectPtr->getPointeeType(); |
| 533 | } |
| 534 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 535 | } else { |
| 536 | // We've found a member of an anonymous struct/union that is |
| 537 | // inside a non-anonymous struct/union, so in a well-formed |
| 538 | // program our base object expression is "this". |
| 539 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 540 | if (!MD->isStatic()) { |
| 541 | QualType AnonFieldType |
| 542 | = Context.getTagDeclType( |
| 543 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 544 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 545 | if ((Context.getCanonicalType(AnonFieldType) |
| 546 | == Context.getCanonicalType(ThisType)) || |
| 547 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 548 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 549 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 550 | MD->getThisType(Context)); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 551 | BaseObjectIsPointer = true; |
| 552 | } |
| 553 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 554 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 555 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 556 | } |
| 557 | ExtraQuals = MD->getTypeQualifiers(); |
| 558 | } |
| 559 | |
| 560 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 561 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 562 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Build the implicit member references to the field of the |
| 566 | // anonymous struct/union. |
| 567 | Expr *Result = BaseObjectExpr; |
| 568 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 569 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 570 | FI != FIEnd; ++FI) { |
| 571 | QualType MemberType = (*FI)->getType(); |
| 572 | if (!(*FI)->isMutable()) { |
| 573 | unsigned combinedQualifiers |
| 574 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 575 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 576 | } |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 577 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 578 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 579 | BaseObjectIsPointer = false; |
| 580 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 583 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 586 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 587 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 588 | /// performs lookup on that name and returns an expression that refers |
| 589 | /// to that name. This routine isn't directly called from the parser, |
| 590 | /// because the parser doesn't know about DeclarationName. Rather, |
| 591 | /// this routine is called by ActOnIdentifierExpr, |
| 592 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 593 | /// which form the DeclarationName from the corresponding syntactic |
| 594 | /// forms. |
| 595 | /// |
| 596 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 597 | /// function call context. LookupCtx is only used for a C++ |
| 598 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 599 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 600 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 601 | /// isAddressOfOperand means that this expression is the direct operand |
| 602 | /// of an address-of operator. This matters because this is the only |
| 603 | /// situation where a qualified name referencing a non-static member may |
| 604 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 605 | Sema::OwningExprResult |
| 606 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 607 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 608 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 609 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 610 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 611 | if (SS && SS->isInvalid()) |
| 612 | return ExprError(); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 613 | |
| 614 | // C++ [temp.dep.expr]p3: |
| 615 | // An id-expression is type-dependent if it contains: |
| 616 | // -- a nested-name-specifier that contains a class-name that |
| 617 | // names a dependent type. |
| 618 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 619 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
| 620 | Loc, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 621 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()))); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 624 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 625 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 627 | NamedDecl *D = 0; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 628 | if (Lookup.isAmbiguous()) { |
| 629 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 630 | SS && SS->isSet() ? SS->getRange() |
| 631 | : SourceRange()); |
| 632 | return ExprError(); |
| 633 | } else |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 634 | D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 635 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 636 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 637 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 638 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 639 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 640 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 641 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 642 | // found a decl, but that decl is outside the current instance method (i.e. |
| 643 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 644 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 645 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 646 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 647 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 648 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 649 | ClassDeclared)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 650 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 651 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 652 | return ExprError(); |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 653 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 654 | // If a class method attemps to use a free standing ivar, this is |
| 655 | // an error. |
| 656 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 657 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 658 | << IV->getDeclName()); |
| 659 | // If a class method uses a global variable, even if an ivar with |
| 660 | // same name exists, use the global. |
| 661 | if (!IsClsMethod) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 662 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 663 | ClassDeclared != IFace) |
| 664 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 665 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 666 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 667 | IdentifierInfo &II = Context.Idents.get("self"); |
| 668 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
| 669 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 670 | Loc, static_cast<Expr*>(SelfExpr.release()), |
| 671 | true, true); |
| 672 | Context.setFieldDecl(IFace, IV, MRef); |
| 673 | return Owned(MRef); |
| 674 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 677 | else if (getCurMethodDecl()->isInstanceMethod()) { |
| 678 | // We should warn if a local variable hides an ivar. |
| 679 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 680 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 681 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 682 | ClassDeclared)) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 683 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 684 | IFace == ClassDeclared) |
| 685 | Diag(Loc, diag::warn_ivar_use_hidden)<<IV->getDeclName(); |
| 686 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 687 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 688 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 689 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 690 | QualType T; |
| 691 | |
| 692 | if (getCurMethodDecl()->isInstanceMethod()) |
| 693 | T = Context.getPointerType(Context.getObjCInterfaceType( |
| 694 | getCurMethodDecl()->getClassInterface())); |
| 695 | else |
| 696 | T = Context.getObjCClassType(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 697 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 698 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 699 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 701 | // Determine whether this name might be a candidate for |
| 702 | // argument-dependent lookup. |
| 703 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 704 | HasTrailingLParen; |
| 705 | |
| 706 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 707 | // We've seen something of the form |
| 708 | // |
| 709 | // identifier( |
| 710 | // |
| 711 | // and we did not find any entity by the name |
| 712 | // "identifier". However, this identifier is still subject to |
| 713 | // argument-dependent lookup, so keep track of the name. |
| 714 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 715 | Context.OverloadTy, |
| 716 | Loc)); |
| 717 | } |
| 718 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 719 | if (D == 0) { |
| 720 | // Otherwise, this could be an implicitly declared function reference (legal |
| 721 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 722 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 723 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 724 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 725 | else { |
| 726 | // If this name wasn't predeclared and if this is not a function call, |
| 727 | // diagnose the problem. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 728 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 729 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 730 | << Name << SS->getRange()); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 731 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 732 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 733 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 734 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 735 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 736 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 739 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 740 | // If this is an expression of the form &Class::member, don't build an |
| 741 | // implicit member ref, because we want a pointer to the member in general, |
| 742 | // not any specific instance's member. |
| 743 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 744 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 745 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 746 | QualType DType; |
| 747 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 748 | DType = FD->getType().getNonReferenceType(); |
| 749 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 750 | DType = Method->getType(); |
| 751 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 752 | DType = Context.OverloadTy; |
| 753 | } |
| 754 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 755 | if (!DType.isNull()) { |
| 756 | // The pointer is type- and value-dependent if it points into something |
| 757 | // dependent. |
| 758 | bool Dependent = false; |
| 759 | for (; DC; DC = DC->getParent()) { |
| 760 | // FIXME: could stop early at namespace scope. |
| 761 | if (DC->isRecord()) { |
| 762 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 763 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 764 | Dependent = true; |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 769 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 774 | // We may have found a field within an anonymous union or struct |
| 775 | // (C++ [class.union]). |
| 776 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 777 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 778 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 780 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 781 | if (!MD->isStatic()) { |
| 782 | // C++ [class.mfct.nonstatic]p2: |
| 783 | // [...] if name lookup (3.4.1) resolves the name in the |
| 784 | // id-expression to a nonstatic nontype member of class X or of |
| 785 | // a base class of X, the id-expression is transformed into a |
| 786 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 787 | // as the postfix-expression to the left of the '.' operator. |
| 788 | DeclContext *Ctx = 0; |
| 789 | QualType MemberType; |
| 790 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 791 | Ctx = FD->getDeclContext(); |
| 792 | MemberType = FD->getType(); |
| 793 | |
| 794 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 795 | MemberType = RefType->getPointeeType(); |
| 796 | else if (!FD->isMutable()) { |
| 797 | unsigned combinedQualifiers |
| 798 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 799 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 800 | } |
| 801 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 802 | if (!Method->isStatic()) { |
| 803 | Ctx = Method->getParent(); |
| 804 | MemberType = Method->getType(); |
| 805 | } |
| 806 | } else if (OverloadedFunctionDecl *Ovl |
| 807 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 808 | for (OverloadedFunctionDecl::function_iterator |
| 809 | Func = Ovl->function_begin(), |
| 810 | FuncEnd = Ovl->function_end(); |
| 811 | Func != FuncEnd; ++Func) { |
| 812 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 813 | if (!DMethod->isStatic()) { |
| 814 | Ctx = Ovl->getDeclContext(); |
| 815 | MemberType = Context.OverloadTy; |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 820 | |
| 821 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 822 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 823 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 824 | if ((Context.getCanonicalType(CtxType) |
| 825 | == Context.getCanonicalType(ThisType)) || |
| 826 | IsDerivedFrom(ThisType, CtxType)) { |
| 827 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 828 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 829 | MD->getThisType(Context)); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 830 | return Owned(new (Context) MemberExpr(This, true, D, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 831 | SourceLocation(), MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 837 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 838 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 839 | if (MD->isStatic()) |
| 840 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 841 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 842 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 845 | // Any other ways we could have found the field in a well-formed |
| 846 | // program would have been turned into implicit member expressions |
| 847 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 848 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 849 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 850 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 851 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 852 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 853 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 854 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 855 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 856 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 857 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 858 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 859 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 860 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 861 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 862 | false, false, SS)); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 863 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 864 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 865 | false, false, SS)); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 866 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 868 | // Check whether this declaration can be used. Note that we suppress |
| 869 | // this check when we're going to perform argument-dependent lookup |
| 870 | // on this function name, because this might not be the function |
| 871 | // that overload resolution actually selects. |
| 872 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 873 | return ExprError(); |
| 874 | |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 875 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 876 | // Warn about constructs like: |
| 877 | // if (void *X = foo()) { ... } else { X }. |
| 878 | // In the else block, the pointer is always false. |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 879 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 880 | Scope *CheckS = S; |
| 881 | while (CheckS) { |
| 882 | if (CheckS->isWithinElse() && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 883 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 884 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 885 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 886 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 887 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 888 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 889 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
| 892 | |
| 893 | // Move up one more control parent to check again. |
| 894 | CheckS = CheckS->getControlParent(); |
| 895 | if (CheckS) |
| 896 | CheckS = CheckS->getParent(); |
| 897 | } |
| 898 | } |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 899 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) { |
| 900 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 901 | // C99 DR 316 says that, if a function type comes from a |
| 902 | // function definition (without a prototype), that type is only |
| 903 | // used for checking compatibility. Therefore, when referencing |
| 904 | // the function, we pretend that we don't have the full function |
| 905 | // type. |
| 906 | QualType T = Func->getType(); |
| 907 | QualType NoProtoType = T; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 908 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 909 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 910 | return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS)); |
| 911 | } |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 912 | } |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 913 | |
| 914 | // Only create DeclRefExpr's for valid Decl's. |
| 915 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 916 | return ExprError(); |
| 917 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 918 | // If the identifier reference is inside a block, and it refers to a value |
| 919 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 920 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 921 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 922 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 923 | // We do not do this for things like enum constants, global variables, etc, |
| 924 | // as they do not get snapshotted. |
| 925 | // |
| 926 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 927 | // Blocks that have these can't be constant. |
| 928 | CurBlock->hasBlockDeclRefExprs = true; |
| 929 | |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 930 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 931 | // The BlocksAttr indicates the variable is bound by-reference. |
| 932 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 933 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 934 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 935 | // Variable will be bound by-copy, make it const within the closure. |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 936 | ExprTy.addConst(); |
| 937 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 938 | } |
| 939 | // If this reference is not in a block or if the referenced variable is |
| 940 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 942 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 943 | bool ValueDependent = false; |
| 944 | if (getLangOptions().CPlusPlus) { |
| 945 | // C++ [temp.dep.expr]p3: |
| 946 | // An id-expression is type-dependent if it contains: |
| 947 | // - an identifier that was declared with a dependent type, |
| 948 | if (VD->getType()->isDependentType()) |
| 949 | TypeDependent = true; |
| 950 | // - FIXME: a template-id that is dependent, |
| 951 | // - a conversion-function-id that specifies a dependent type, |
| 952 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 953 | Name.getCXXNameType()->isDependentType()) |
| 954 | TypeDependent = true; |
| 955 | // - a nested-name-specifier that contains a class-name that |
| 956 | // names a dependent type. |
| 957 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 958 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 959 | DC; DC = DC->getParent()) { |
| 960 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 961 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 962 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 963 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 964 | TypeDependent = true; |
| 965 | break; |
| 966 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 971 | // C++ [temp.dep.constexpr]p2: |
| 972 | // |
| 973 | // An identifier is value-dependent if it is: |
| 974 | // - a name declared with a dependent type, |
| 975 | if (TypeDependent) |
| 976 | ValueDependent = true; |
| 977 | // - the name of a non-type template parameter, |
| 978 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 979 | ValueDependent = true; |
| 980 | // - a constant with integral or enumeration type and is |
| 981 | // initialized with an expression that is value-dependent |
| 982 | // (FIXME!). |
| 983 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 984 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 985 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 986 | TypeDependent, ValueDependent, SS)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 989 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 990 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 991 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 993 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 994 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 995 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 996 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 997 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 998 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 999 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1000 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1001 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1002 | unsigned Length; |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1003 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1004 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1005 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1006 | Length = MD->getSynthesizedMethodSize(); |
| 1007 | else { |
| 1008 | Diag(Loc, diag::ext_predef_outside_function); |
| 1009 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 1010 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 1011 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1012 | |
| 1013 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1014 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1015 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1016 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1017 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1020 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1021 | llvm::SmallString<16> CharBuffer; |
| 1022 | CharBuffer.resize(Tok.getLength()); |
| 1023 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1024 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1026 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1027 | Tok.getLocation(), PP); |
| 1028 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1029 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1030 | |
| 1031 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1032 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1033 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1034 | Literal.isWide(), |
| 1035 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1038 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1039 | // 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] | 1040 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1041 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1042 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1043 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1044 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1045 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1046 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1048 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1049 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1050 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1051 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1053 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1054 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1056 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1057 | Tok.getLocation(), PP); |
| 1058 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1059 | return ExprError(); |
| 1060 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1061 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1062 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1063 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1064 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1065 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1066 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1067 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1068 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1069 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1070 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1071 | |
| 1072 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1073 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1074 | // isExact will be set by GetFloatValue(). |
| 1075 | bool isExact = false; |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1076 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1077 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1078 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1079 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1080 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1081 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1082 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1083 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1084 | // long long is a C99 feature. |
| 1085 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1086 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1087 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1088 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1089 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1090 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1092 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1093 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1094 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1095 | Ty = Context.UnsignedLongLongTy; |
| 1096 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1097 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1100 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1102 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1103 | // be an unsigned int. |
| 1104 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1105 | |
| 1106 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1107 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1108 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1109 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1110 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1112 | // Does it fit in a unsigned int? |
| 1113 | if (ResultVal.isIntN(IntSize)) { |
| 1114 | // Does it fit in a signed int? |
| 1115 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1116 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1117 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1118 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1119 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1121 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1123 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1124 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1125 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1127 | // Does it fit in a unsigned long? |
| 1128 | if (ResultVal.isIntN(LongSize)) { |
| 1129 | // Does it fit in a signed long? |
| 1130 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1131 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1132 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1133 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1134 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1135 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1138 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1139 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1140 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1142 | // Does it fit in a unsigned long long? |
| 1143 | if (ResultVal.isIntN(LongLongSize)) { |
| 1144 | // Does it fit in a signed long long? |
| 1145 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1146 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1147 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1148 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1149 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1153 | // If we still couldn't decide a type, we probably have something that |
| 1154 | // 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] | 1155 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1156 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1157 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1158 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1159 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1161 | if (ResultVal.getBitWidth() != Width) |
| 1162 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1163 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1164 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1165 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1167 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1168 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1169 | Res = new (Context) ImaginaryLiteral(Res, |
| 1170 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1171 | |
| 1172 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1175 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1176 | SourceLocation R, ExprArg Val) { |
| 1177 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1178 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1179 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1183 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1184 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1185 | SourceLocation OpLoc, |
| 1186 | const SourceRange &ExprRange, |
| 1187 | bool isSizeof) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1188 | if (exprType->isDependentType()) |
| 1189 | return false; |
| 1190 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1191 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1192 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1193 | // alignof(function) is allowed. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1194 | if (isSizeof) |
| 1195 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1200 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1201 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1202 | return false; |
| 1203 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1205 | return RequireCompleteType(OpLoc, exprType, |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1206 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1207 | diag::err_alignof_incomplete_type, |
| 1208 | ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1211 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1212 | const SourceRange &ExprRange) { |
| 1213 | E = E->IgnoreParens(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1215 | // alignof decl is always ok. |
| 1216 | if (isa<DeclRefExpr>(E)) |
| 1217 | return false; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1218 | |
| 1219 | // Cannot know anything else if the expression is dependent. |
| 1220 | if (E->isTypeDependent()) |
| 1221 | return false; |
| 1222 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1223 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1224 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1225 | if (FD->isBitField()) { |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1226 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1227 | return true; |
| 1228 | } |
| 1229 | // Other fields are ok. |
| 1230 | return false; |
| 1231 | } |
| 1232 | } |
| 1233 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1234 | } |
| 1235 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1236 | /// \brief Build a sizeof or alignof expression given a type operand. |
| 1237 | Action::OwningExprResult |
| 1238 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
| 1239 | bool isSizeOf, SourceRange R) { |
| 1240 | if (T.isNull()) |
| 1241 | return ExprError(); |
| 1242 | |
| 1243 | if (!T->isDependentType() && |
| 1244 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1245 | return ExprError(); |
| 1246 | |
| 1247 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1248 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1249 | Context.getSizeType(), OpLoc, |
| 1250 | R.getEnd())); |
| 1251 | } |
| 1252 | |
| 1253 | /// \brief Build a sizeof or alignof expression given an expression |
| 1254 | /// operand. |
| 1255 | Action::OwningExprResult |
| 1256 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1257 | bool isSizeOf, SourceRange R) { |
| 1258 | // Verify that the operand is valid. |
| 1259 | bool isInvalid = false; |
| 1260 | if (E->isTypeDependent()) { |
| 1261 | // Delay type-checking for type-dependent expressions. |
| 1262 | } else if (!isSizeOf) { |
| 1263 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
| 1264 | } else if (E->isBitField()) { // C99 6.5.3.4p1. |
| 1265 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1266 | isInvalid = true; |
| 1267 | } else { |
| 1268 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1269 | } |
| 1270 | |
| 1271 | if (isInvalid) |
| 1272 | return ExprError(); |
| 1273 | |
| 1274 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1275 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1276 | Context.getSizeType(), OpLoc, |
| 1277 | R.getEnd())); |
| 1278 | } |
| 1279 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1280 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1281 | /// the same for @c alignof and @c __alignof |
| 1282 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1283 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1284 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1285 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1286 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1287 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1288 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1289 | if (isType) { |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1290 | QualType ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1291 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
| 1292 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1294 | // Get the end location. |
| 1295 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1296 | Action::OwningExprResult Result |
| 1297 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1298 | |
| 1299 | if (Result.isInvalid()) |
| 1300 | DeleteExpr(ArgEx); |
| 1301 | |
| 1302 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1305 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1306 | if (V->isTypeDependent()) |
| 1307 | return Context.DependentTy; |
| 1308 | |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1309 | DefaultFunctionArrayConversion(V); |
| 1310 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1311 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1312 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1313 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1314 | |
| 1315 | // Otherwise they pass through real integer and floating point types here. |
| 1316 | if (V->getType()->isArithmeticType()) |
| 1317 | return V->getType(); |
| 1318 | |
| 1319 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1320 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1321 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1322 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1326 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1327 | Action::OwningExprResult |
| 1328 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1329 | tok::TokenKind Kind, ExprArg Input) { |
| 1330 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1332 | UnaryOperator::Opcode Opc; |
| 1333 | switch (Kind) { |
| 1334 | default: assert(0 && "Unknown unary op!"); |
| 1335 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1336 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1337 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1339 | if (getLangOptions().CPlusPlus && |
| 1340 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1341 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1342 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1343 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1344 | |
| 1345 | // C++ [over.inc]p1: |
| 1346 | // |
| 1347 | // [...] If the function is a member function with one |
| 1348 | // parameter (which shall be of type int) or a non-member |
| 1349 | // function with two parameters (the second of which shall be |
| 1350 | // of type int), it defines the postfix increment operator ++ |
| 1351 | // for objects of that type. When the postfix increment is |
| 1352 | // called as a result of using the ++ operator, the int |
| 1353 | // argument will have value zero. |
| 1354 | Expr *Args[2] = { |
| 1355 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1356 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1357 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1358 | }; |
| 1359 | |
| 1360 | // Build the candidate set for overloading |
| 1361 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1362 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Perform overload resolution. |
| 1365 | OverloadCandidateSet::iterator Best; |
| 1366 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1367 | case OR_Success: { |
| 1368 | // We found a built-in operator or an overloaded operator. |
| 1369 | FunctionDecl *FnDecl = Best->Function; |
| 1370 | |
| 1371 | if (FnDecl) { |
| 1372 | // We matched an overloaded operator. Build a call to that |
| 1373 | // operator. |
| 1374 | |
| 1375 | // Convert the arguments. |
| 1376 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1377 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1378 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1379 | } else { |
| 1380 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1381 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1382 | FnDecl->getParamDecl(0)->getType(), |
| 1383 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1384 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1388 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1389 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1390 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1391 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1392 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1393 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 6d8e573 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1394 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1395 | UsualUnaryConversions(FnExpr); |
| 1396 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1397 | Input.release(); |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1398 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
| 1399 | Args, 2, ResultTy, |
| 1400 | OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1401 | } else { |
| 1402 | // We matched a built-in operator. Convert the arguments, then |
| 1403 | // break out so that we will build the appropriate built-in |
| 1404 | // operator node. |
| 1405 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1406 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1407 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1408 | |
| 1409 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | case OR_No_Viable_Function: |
| 1414 | // No viable function; fall through to handling this as a |
| 1415 | // built-in operator, which will produce an error message for us. |
| 1416 | break; |
| 1417 | |
| 1418 | case OR_Ambiguous: |
| 1419 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1420 | << UnaryOperator::getOpcodeStr(Opc) |
| 1421 | << Arg->getSourceRange(); |
| 1422 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1423 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1424 | |
| 1425 | case OR_Deleted: |
| 1426 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1427 | << Best->Function->isDeleted() |
| 1428 | << UnaryOperator::getOpcodeStr(Opc) |
| 1429 | << Arg->getSourceRange(); |
| 1430 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1431 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | // Either we found no viable overloaded operator or we matched a |
| 1435 | // built-in operator. In either case, fall through to trying to |
| 1436 | // build a built-in operation. |
| 1437 | } |
| 1438 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1439 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1440 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1441 | if (result.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1442 | return ExprError(); |
| 1443 | Input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1444 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1447 | Action::OwningExprResult |
| 1448 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1449 | ExprArg Idx, SourceLocation RLoc) { |
| 1450 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1451 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1452 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1453 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1454 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1455 | LHSExp->getType()->isEnumeralType() || |
| 1456 | RHSExp->getType()->isRecordType() || |
| 1457 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1458 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1459 | // to the candidate set. |
| 1460 | OverloadCandidateSet CandidateSet; |
| 1461 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1462 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1463 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1464 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1465 | // Perform overload resolution. |
| 1466 | OverloadCandidateSet::iterator Best; |
| 1467 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1468 | case OR_Success: { |
| 1469 | // We found a built-in operator or an overloaded operator. |
| 1470 | FunctionDecl *FnDecl = Best->Function; |
| 1471 | |
| 1472 | if (FnDecl) { |
| 1473 | // We matched an overloaded operator. Build a call to that |
| 1474 | // operator. |
| 1475 | |
| 1476 | // Convert the arguments. |
| 1477 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1478 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1479 | PerformCopyInitialization(RHSExp, |
| 1480 | FnDecl->getParamDecl(0)->getType(), |
| 1481 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1482 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1483 | } else { |
| 1484 | // Convert the arguments. |
| 1485 | if (PerformCopyInitialization(LHSExp, |
| 1486 | FnDecl->getParamDecl(0)->getType(), |
| 1487 | "passing") || |
| 1488 | PerformCopyInitialization(RHSExp, |
| 1489 | FnDecl->getParamDecl(1)->getType(), |
| 1490 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1491 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1495 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1496 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1497 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1499 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1500 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1501 | SourceLocation()); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1502 | UsualUnaryConversions(FnExpr); |
| 1503 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1504 | Base.release(); |
| 1505 | Idx.release(); |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1506 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 1507 | FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1508 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1509 | } else { |
| 1510 | // We matched a built-in operator. Convert the arguments, then |
| 1511 | // break out so that we will build the appropriate built-in |
| 1512 | // operator node. |
| 1513 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1514 | "passing") || |
| 1515 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1516 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1517 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1518 | |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | case OR_No_Viable_Function: |
| 1524 | // No viable function; fall through to handling this as a |
| 1525 | // built-in operator, which will produce an error message for us. |
| 1526 | break; |
| 1527 | |
| 1528 | case OR_Ambiguous: |
| 1529 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1530 | << "[]" |
| 1531 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1532 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1533 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1534 | |
| 1535 | case OR_Deleted: |
| 1536 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1537 | << Best->Function->isDeleted() |
| 1538 | << "[]" |
| 1539 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1540 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1541 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | // Either we found no viable overloaded operator or we matched a |
| 1545 | // built-in operator. In either case, fall through to trying to |
| 1546 | // build a built-in operation. |
| 1547 | } |
| 1548 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1549 | // Perform default conversions. |
| 1550 | DefaultFunctionArrayConversion(LHSExp); |
| 1551 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1553 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1554 | |
| 1555 | // 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] | 1556 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1557 | // in the subscript position. As a result, we need to derive the array base |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1558 | // and index from the expression types. |
| 1559 | Expr *BaseExpr, *IndexExpr; |
| 1560 | QualType ResultType; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1561 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1562 | BaseExpr = LHSExp; |
| 1563 | IndexExpr = RHSExp; |
| 1564 | ResultType = Context.DependentTy; |
| 1565 | } else if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1566 | BaseExpr = LHSExp; |
| 1567 | IndexExpr = RHSExp; |
| 1568 | // FIXME: need to deal with const... |
| 1569 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1570 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1571 | // Handle the uncommon case of "123[Ptr]". |
| 1572 | BaseExpr = RHSExp; |
| 1573 | IndexExpr = LHSExp; |
| 1574 | // FIXME: need to deal with const... |
| 1575 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1576 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1577 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1578 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1580 | // FIXME: need to deal with const... |
| 1581 | ResultType = VTy->getElementType(); |
| 1582 | } else { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1583 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1584 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1585 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1586 | // C99 6.5.2.1p1 |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1587 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1588 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1589 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1590 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1591 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| 1592 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1593 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| 1594 | // incomplete types are not object types. |
| 1595 | if (ResultType->isFunctionType()) { |
| 1596 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1597 | << ResultType << BaseExpr->getSourceRange(); |
| 1598 | return ExprError(); |
| 1599 | } |
| 1600 | if (!ResultType->isDependentType() && |
| 1601 | RequireCompleteType(BaseExpr->getLocStart(), ResultType, |
| 1602 | diag::err_subscript_incomplete_type, |
| 1603 | BaseExpr->getSourceRange())) |
| 1604 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1605 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1606 | Base.release(); |
| 1607 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1608 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1609 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1612 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1613 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1614 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1615 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1616 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1617 | // The vector accessor can't exceed the number of elements. |
| 1618 | const char *compStr = CompName.getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1619 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1620 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1621 | // special names that indicate a subset of exactly half the elements are |
| 1622 | // to be selected. |
| 1623 | bool HalvingSwizzle = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1624 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1625 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1626 | // indicating that it is a string of hex values to be used as vector indices. |
| 1627 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Check that we've found one of the special components, or that the component |
| 1630 | // names must come from the same set. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1631 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1632 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1633 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1634 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1635 | do |
| 1636 | compStr++; |
| 1637 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1638 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1639 | do |
| 1640 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1641 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1642 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1643 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1644 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1645 | // We didn't get to the end of the string. This means the component names |
| 1646 | // 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] | 1647 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1648 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1649 | return QualType(); |
| 1650 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1651 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1652 | // Ensure no component accessor exceeds the width of the vector type it |
| 1653 | // operates on. |
| 1654 | if (!HalvingSwizzle) { |
| 1655 | compStr = CompName.getName(); |
| 1656 | |
| 1657 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1658 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1659 | |
| 1660 | while (*compStr) { |
| 1661 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1662 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1663 | << baseType << SourceRange(CompLoc); |
| 1664 | return QualType(); |
| 1665 | } |
| 1666 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1667 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1668 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1669 | // If this is a halving swizzle, verify that the base type has an even |
| 1670 | // number of elements. |
| 1671 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1672 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1673 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1674 | return QualType(); |
| 1675 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1676 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1677 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1678 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1679 | // 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] | 1680 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1681 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1682 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1683 | : CompName.getLength(); |
| 1684 | if (HexSwizzle) |
| 1685 | CompSize--; |
| 1686 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1687 | if (CompSize == 1) |
| 1688 | return vecType->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1689 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1690 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1691 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1692 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1693 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1694 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1695 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1696 | } |
| 1697 | 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] | 1698 | } |
| 1699 | |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1700 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 1701 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1702 | const Selector &Sel, |
| 1703 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1705 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Context, &Member)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1706 | return PD; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1707 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Context, Sel)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1708 | return OMD; |
| 1709 | |
| 1710 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1711 | E = PDecl->protocol_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1712 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
| 1713 | Context)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1714 | return D; |
| 1715 | } |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | static Decl *FindGetterNameDecl(const ObjCQualifiedIdType *QIdTy, |
| 1720 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1721 | const Selector &Sel, |
| 1722 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1723 | // Check protocols on qualified interfaces. |
| 1724 | Decl *GDecl = 0; |
| 1725 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
| 1726 | E = QIdTy->qual_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1727 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, &Member)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1728 | GDecl = PD; |
| 1729 | break; |
| 1730 | } |
| 1731 | // Also must look for a getter name which uses property syntax. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1732 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Context, Sel)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1733 | GDecl = OMD; |
| 1734 | break; |
| 1735 | } |
| 1736 | } |
| 1737 | if (!GDecl) { |
| 1738 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
| 1739 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1740 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1741 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1742 | if (GDecl) |
| 1743 | return GDecl; |
| 1744 | } |
| 1745 | } |
| 1746 | return GDecl; |
| 1747 | } |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1748 | |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1749 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 1750 | /// all base class implementations. |
| 1751 | /// |
| 1752 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 1753 | const ObjCInterfaceDecl *IFace, |
| 1754 | const Selector &Sel) { |
| 1755 | ObjCMethodDecl *Method = 0; |
| 1756 | if (ObjCImplementationDecl *ImpDecl = |
| 1757 | Sema::ObjCImplementations[IFace->getIdentifier()]) |
| 1758 | Method = ImpDecl->getInstanceMethod(Sel); |
| 1759 | |
| 1760 | if (!Method && IFace->getSuperClass()) |
| 1761 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 1762 | return Method; |
| 1763 | } |
| 1764 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1765 | Action::OwningExprResult |
| 1766 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1767 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1768 | IdentifierInfo &Member, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1769 | DeclPtrTy ObjCImpDecl) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1770 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1771 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Perform default conversions. |
| 1774 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1775 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1776 | QualType BaseType = BaseExpr->getType(); |
| 1777 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1779 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1780 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1781 | if (OpKind == tok::arrow) { |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1782 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1783 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1784 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1785 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1786 | MemberLoc, Member)); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1787 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1788 | return ExprError(Diag(MemberLoc, |
| 1789 | diag::err_typecheck_member_reference_arrow) |
| 1790 | << BaseType << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1791 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1792 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1793 | // Handle field access to simple records. This also handles access to fields |
| 1794 | // of the ObjC 'id' struct. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1795 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1796 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1797 | if (RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1798 | diag::err_typecheck_incomplete_tag, |
| 1799 | BaseExpr->getSourceRange())) |
| 1800 | return ExprError(); |
| 1801 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1802 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1803 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1804 | // than this. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1805 | LookupResult Result |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1806 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1807 | LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1809 | if (!Result) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1810 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1811 | << &Member << BaseExpr->getSourceRange()); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1812 | if (Result.isAmbiguous()) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1813 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1814 | MemberLoc, BaseExpr->getSourceRange()); |
| 1815 | return ExprError(); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1820 | // If the decl being referenced had an error, return an error for this |
| 1821 | // sub-expr without emitting another error, in order to avoid cascading |
| 1822 | // error cases. |
| 1823 | if (MemberDecl->isInvalidDecl()) |
| 1824 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1825 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1826 | // Check the use of this field |
| 1827 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 1828 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1829 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1830 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1831 | // We may have found a field within an anonymous union or struct |
| 1832 | // (C++ [class.union]). |
| 1833 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1834 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1835 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1837 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1838 | // FIXME: Handle address space modifiers |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1839 | QualType MemberType = FD->getType(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1840 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1841 | MemberType = Ref->getPointeeType(); |
| 1842 | else { |
| 1843 | unsigned combinedQualifiers = |
| 1844 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1845 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1846 | combinedQualifiers &= ~QualType::Const; |
| 1847 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1848 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1849 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1850 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1851 | MemberLoc, MemberType)); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1855 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1856 | Var, MemberLoc, |
| 1857 | Var->getType().getNonReferenceType())); |
| 1858 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1859 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1860 | MemberFn, MemberLoc, |
| 1861 | MemberFn->getType())); |
| 1862 | if (OverloadedFunctionDecl *Ovl |
| 1863 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1864 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1865 | MemberLoc, Context.OverloadTy)); |
| 1866 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1867 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1868 | Enum, MemberLoc, Enum->getType())); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1869 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1870 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1871 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1873 | // We found a declaration kind that we didn't expect. This is a |
| 1874 | // generic error message that tells the user that she can't refer |
| 1875 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1876 | return ExprError(Diag(MemberLoc, |
| 1877 | diag::err_typecheck_member_reference_unknown) |
| 1878 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1879 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1881 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1882 | // (*Obj).ivar. |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1883 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1884 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1885 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(Context, |
| 1886 | &Member, |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1887 | ClassDeclared)) { |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1888 | // If the decl being referenced had an error, return an error for this |
| 1889 | // sub-expr without emitting another error, in order to avoid cascading |
| 1890 | // error cases. |
| 1891 | if (IV->isInvalidDecl()) |
| 1892 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1893 | |
| 1894 | // Check whether we can reference this field. |
| 1895 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1896 | return ExprError(); |
Steve Naroff | 8c56ee0 | 2009-03-26 16:01:08 +0000 | [diff] [blame] | 1897 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 1898 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1899 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 1900 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1901 | ClassOfMethodDecl = MD->getClassInterface(); |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1902 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 1903 | // Case of a c-function declared inside an objc implementation. |
| 1904 | // FIXME: For a c-style function nested inside an objc implementation |
| 1905 | // class, there is no implementation context available, so we pass down |
| 1906 | // the context as argument to this routine. Ideally, this context need |
| 1907 | // be passed down in the AST node and somehow calculated from the AST |
| 1908 | // for a function decl. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1909 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1910 | if (ObjCImplementationDecl *IMPD = |
| 1911 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 1912 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 1913 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 1914 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 1915 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
Steve Naroff | f960657 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 1916 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 1917 | |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1918 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1919 | if (ClassDeclared != IFTy->getDecl() || |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1920 | ClassOfMethodDecl != ClassDeclared) |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 1921 | Diag(MemberLoc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1922 | } |
| 1923 | // @protected |
| 1924 | else if (!IFTy->getDecl()->isSuperClassOf(ClassOfMethodDecl)) |
| 1925 | Diag(MemberLoc, diag::error_protected_ivar_access) << IV->getDeclName(); |
| 1926 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1927 | |
| 1928 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1929 | MemberLoc, BaseExpr, |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1930 | OpKind == tok::arrow); |
| 1931 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1932 | return Owned(MRef); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1933 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1934 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1935 | << IFTy->getDecl()->getDeclName() << &Member |
| 1936 | << BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1937 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1939 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1940 | // pointer to a (potentially qualified) interface type. |
| 1941 | const PointerType *PTy; |
| 1942 | const ObjCInterfaceType *IFTy; |
| 1943 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1944 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1945 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | dd85128 | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1946 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1947 | // Search for a declared property first. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1948 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Context, |
| 1949 | &Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1950 | // Check whether we can reference this property. |
| 1951 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1952 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1953 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1954 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1955 | MemberLoc, BaseExpr)); |
| 1956 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1957 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1958 | // Check protocols on qualified interfaces. |
Chris Lattner | d5f8179 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1959 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1960 | E = IFTy->qual_end(); I != E; ++I) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1961 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, |
| 1962 | &Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1963 | // Check whether we can reference this property. |
| 1964 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1965 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1966 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1967 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1968 | MemberLoc, BaseExpr)); |
| 1969 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1970 | |
| 1971 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1972 | // selector is implemented. |
| 1973 | |
| 1974 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1975 | // shared with the code in ActOnInstanceMessage. |
| 1976 | |
| 1977 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1978 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1979 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1980 | // If this reference is in an @implementation, check for 'private' methods. |
| 1981 | if (!Getter) |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1982 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1983 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1984 | // Look through local category implementations associated with the class. |
| 1985 | if (!Getter) { |
| 1986 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1987 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1988 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1989 | } |
| 1990 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1991 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1992 | // Check if we can reference this property. |
| 1993 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1994 | return ExprError(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 1995 | } |
| 1996 | // If we found a getter then this may be a valid dot-reference, we |
| 1997 | // will look for the matching setter, in case it is needed. |
| 1998 | Selector SetterSel = |
| 1999 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2000 | PP.getSelectorTable(), &Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2001 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(Context, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2002 | if (!Setter) { |
| 2003 | // If this reference is in an @implementation, also check for 'private' |
| 2004 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2005 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2006 | } |
| 2007 | // Look through local category implementations associated with the class. |
| 2008 | if (!Setter) { |
| 2009 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2010 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 2011 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 2012 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2013 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2014 | |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2015 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2016 | return ExprError(); |
| 2017 | |
| 2018 | if (Getter || Setter) { |
| 2019 | QualType PType; |
| 2020 | |
| 2021 | if (Getter) |
| 2022 | PType = Getter->getResultType(); |
| 2023 | else { |
| 2024 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2025 | E = Setter->param_end(); PI != E; ++PI) |
| 2026 | PType = (*PI)->getType(); |
| 2027 | } |
| 2028 | // FIXME: we must check that the setter has property type. |
| 2029 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2030 | Setter, MemberLoc, BaseExpr)); |
| 2031 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2032 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2033 | << &Member << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2034 | } |
Steve Naroff | d1d4440 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 2035 | // Handle properties on qualified "id" protocols. |
| 2036 | const ObjCQualifiedIdType *QIdTy; |
| 2037 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 2038 | // Check protocols on qualified interfaces. |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2039 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2040 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2041 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2042 | // Check the use of this declaration |
| 2043 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2044 | return ExprError(); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2045 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2046 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2047 | MemberLoc, BaseExpr)); |
| 2048 | } |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2049 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2050 | // Check the use of this method. |
| 2051 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2052 | return ExprError(); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2053 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2054 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2055 | OMD->getResultType(), |
| 2056 | OMD, OpLoc, MemberLoc, |
| 2057 | NULL, 0)); |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 2058 | } |
| 2059 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2060 | |
| 2061 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2062 | << &Member << BaseType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2063 | } |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2064 | // Handle properties on ObjC 'Class' types. |
| 2065 | if (OpKind == tok::period && (BaseType == Context.getObjCClassType())) { |
| 2066 | // Also must look for a getter name which uses property syntax. |
| 2067 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2068 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2069 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2070 | ObjCMethodDecl *Getter; |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2071 | // FIXME: need to also look locally in the implementation. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2072 | if ((Getter = IFace->lookupClassMethod(Context, Sel))) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2073 | // Check the use of this method. |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2074 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2075 | return ExprError(); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2076 | } |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2077 | // If we found a getter then this may be a valid dot-reference, we |
| 2078 | // will look for the matching setter, in case it is needed. |
| 2079 | Selector SetterSel = |
| 2080 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2081 | PP.getSelectorTable(), &Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2082 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel); |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2083 | if (!Setter) { |
| 2084 | // If this reference is in an @implementation, also check for 'private' |
| 2085 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2086 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2087 | } |
| 2088 | // Look through local category implementations associated with the class. |
| 2089 | if (!Setter) { |
| 2090 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2091 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 2092 | Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2097 | return ExprError(); |
| 2098 | |
| 2099 | if (Getter || Setter) { |
| 2100 | QualType PType; |
| 2101 | |
| 2102 | if (Getter) |
| 2103 | PType = Getter->getResultType(); |
| 2104 | else { |
| 2105 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2106 | E = Setter->param_end(); PI != E; ++PI) |
| 2107 | PType = (*PI)->getType(); |
| 2108 | } |
| 2109 | // FIXME: we must check that the setter has property type. |
| 2110 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2111 | Setter, MemberLoc, BaseExpr)); |
| 2112 | } |
| 2113 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2114 | << &Member << BaseType); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2118 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2119 | if (BaseType->isExtVectorType()) { |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2120 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2121 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2122 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2123 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2124 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2125 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2127 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2128 | << BaseType << BaseExpr->getSourceRange(); |
| 2129 | |
| 2130 | // If the user is trying to apply -> or . to a function or function |
| 2131 | // pointer, it's probably because they forgot parentheses to call |
| 2132 | // the function. Suggest the addition of those parentheses. |
| 2133 | if (BaseType == Context.OverloadTy || |
| 2134 | BaseType->isFunctionType() || |
| 2135 | (BaseType->isPointerType() && |
| 2136 | BaseType->getAsPointerType()->isFunctionType())) { |
| 2137 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2138 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2139 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2140 | } |
| 2141 | |
| 2142 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2145 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2146 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2147 | /// function prototype Proto. Call is the call expression itself, and |
| 2148 | /// Fn is the function expression. For a C++ member function, this |
| 2149 | /// routine does not attempt to convert the object argument. Returns |
| 2150 | /// true if the call is ill-formed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2151 | bool |
| 2152 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2153 | FunctionDecl *FDecl, |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2154 | const FunctionProtoType *Proto, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2155 | Expr **Args, unsigned NumArgs, |
| 2156 | SourceLocation RParenLoc) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2157 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2158 | // assignment, to the types of the corresponding parameter, ... |
| 2159 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2160 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2161 | bool Invalid = false; |
| 2162 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2163 | // If too few arguments are available (and we don't have default |
| 2164 | // arguments for the remaining parameters), don't make the call. |
| 2165 | if (NumArgs < NumArgsInProto) { |
| 2166 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2167 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2168 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2169 | // Use default arguments for missing arguments |
| 2170 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2171 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | // If too many are passed and not variadic, error on the extras and drop |
| 2175 | // them. |
| 2176 | if (NumArgs > NumArgsInProto) { |
| 2177 | if (!Proto->isVariadic()) { |
| 2178 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2179 | diag::err_typecheck_call_too_many_args) |
| 2180 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2181 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2182 | Args[NumArgs-1]->getLocEnd()); |
| 2183 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2184 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2185 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2186 | } |
| 2187 | NumArgsToCheck = NumArgsInProto; |
| 2188 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2190 | // Continue to check argument types (even if we have too few/many args). |
| 2191 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2192 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2194 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2195 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2196 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2197 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2198 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2199 | ProtoArgType, |
| 2200 | diag::err_call_incomplete_argument, |
| 2201 | Arg->getSourceRange())) |
| 2202 | return true; |
| 2203 | |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2204 | // Pass the argument. |
| 2205 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2206 | return true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2207 | } else |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2208 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2209 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2210 | QualType ArgType = Arg->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2211 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2212 | Call->setArg(i, Arg); |
| 2213 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2215 | // If this is a variadic call, handle args passed through "...". |
| 2216 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2217 | VariadicCallType CallType = VariadicFunction; |
| 2218 | if (Fn->getType()->isBlockPointerType()) |
| 2219 | CallType = VariadicBlock; // Block |
| 2220 | else if (isa<MemberExpr>(Fn)) |
| 2221 | CallType = VariadicMethod; |
| 2222 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2223 | // Promote the arguments (C99 6.5.2.2p7). |
| 2224 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2225 | Expr *Arg = Args[i]; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2226 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2227 | Call->setArg(i, Arg); |
| 2228 | } |
| 2229 | } |
| 2230 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2231 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2234 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2235 | /// This provides the location of the left/right parens and a list of comma |
| 2236 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2237 | Action::OwningExprResult |
| 2238 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2239 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2240 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2241 | unsigned NumArgs = args.size(); |
| 2242 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 2243 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2244 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2245 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2246 | DeclarationName UnqualifiedName; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2248 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2249 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2250 | // in which case we won't do any semantic analysis now. |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2251 | // FIXME: Will need to cache the results of name lookup (including ADL) in Fn. |
| 2252 | bool Dependent = false; |
| 2253 | if (Fn->isTypeDependent()) |
| 2254 | Dependent = true; |
| 2255 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2256 | Dependent = true; |
| 2257 | |
| 2258 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2259 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2260 | Context.DependentTy, RParenLoc)); |
| 2261 | |
| 2262 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2263 | if (Fn->getType()->isRecordType()) |
| 2264 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2265 | CommaLocs, RParenLoc)); |
| 2266 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2267 | // Determine whether this is a call to a member function. |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2268 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2269 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2270 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2271 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2272 | CommaLocs, RParenLoc)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2275 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2276 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2277 | Expr *FnExpr = Fn; |
| 2278 | bool ADL = true; |
| 2279 | while (true) { |
| 2280 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2281 | FnExpr = IcExpr->getSubExpr(); |
| 2282 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2283 | // Parentheses around a function disable ADL |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2284 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2285 | ADL = false; |
| 2286 | FnExpr = PExpr->getSubExpr(); |
| 2287 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2288 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2289 | == UnaryOperator::AddrOf) { |
| 2290 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2291 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2292 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2293 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2294 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2295 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2296 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2297 | UnqualifiedName = DepName->getName(); |
| 2298 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2299 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2300 | // Any kind of name that does not refer to a declaration (or |
| 2301 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2302 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2305 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2307 | OverloadedFunctionDecl *Ovl = 0; |
| 2308 | if (DRExpr) { |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2309 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2310 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
| 2311 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2313 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2314 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2315 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2316 | ADL = false; |
| 2317 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2318 | // We don't perform ADL in C. |
| 2319 | if (!getLangOptions().CPlusPlus) |
| 2320 | ADL = false; |
| 2321 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2322 | if (Ovl || ADL) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2323 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2324 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2325 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2326 | if (!FDecl) |
| 2327 | return ExprError(); |
| 2328 | |
| 2329 | // Update Fn to refer to the actual function selected. |
| 2330 | Expr *NewFn = 0; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2331 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2332 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2333 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2334 | QDRExpr->getLocation(), |
| 2335 | false, false, |
| 2336 | QDRExpr->getQualifierRange(), |
| 2337 | QDRExpr->getQualifier()); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2338 | else |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2339 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2340 | Fn->getSourceRange().getBegin()); |
| 2341 | Fn->Destroy(Context); |
| 2342 | Fn = NewFn; |
| 2343 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2344 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2345 | |
| 2346 | // Promote the function operand. |
| 2347 | UsualUnaryConversions(Fn); |
| 2348 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2349 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2350 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2351 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2352 | Args, NumArgs, |
| 2353 | Context.BoolTy, |
| 2354 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2355 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2356 | const FunctionType *FuncT; |
| 2357 | if (!Fn->getType()->isBlockPointerType()) { |
| 2358 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2359 | // have type pointer to function". |
| 2360 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2361 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2362 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2363 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2364 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2365 | } else { // This is a block call. |
| 2366 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2367 | getAsFunctionType(); |
| 2368 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2369 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2370 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2371 | << Fn->getType() << Fn->getSourceRange()); |
| 2372 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2373 | // Check for a valid return type |
| 2374 | if (!FuncT->getResultType()->isVoidType() && |
| 2375 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2376 | FuncT->getResultType(), |
| 2377 | diag::err_call_incomplete_return, |
| 2378 | TheCall->getSourceRange())) |
| 2379 | return ExprError(); |
| 2380 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2381 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2382 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2384 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2385 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2386 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2387 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2388 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2389 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2390 | |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2391 | if (FDecl) { |
| 2392 | // Check if we have too few/too many template arguments, based |
| 2393 | // on our knowledge of the function definition. |
| 2394 | const FunctionDecl *Def = 0; |
| 2395 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) |
| 2396 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2397 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2398 | } |
| 2399 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2400 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2401 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2402 | Expr *Arg = Args[i]; |
| 2403 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2404 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2405 | Arg->getType(), |
| 2406 | diag::err_call_incomplete_argument, |
| 2407 | Arg->getSourceRange())) |
| 2408 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2409 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2410 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2411 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2412 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2413 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2414 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2415 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2416 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2418 | // Do special checking on direct calls to functions. |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2419 | if (FDecl) |
| 2420 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2421 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2422 | return Owned(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2425 | Action::OwningExprResult |
| 2426 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2427 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2428 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2429 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 2430 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2431 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2432 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2433 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2434 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2435 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2436 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2437 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2438 | } else if (RequireCompleteType(LParenLoc, literalType, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2439 | diag::err_typecheck_decl_incomplete_type, |
| 2440 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2441 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2442 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2443 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2444 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2445 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2446 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2447 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2448 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2449 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2450 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2451 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2452 | InitExpr.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2453 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2454 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2457 | Action::OwningExprResult |
| 2458 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2459 | SourceLocation RBraceLoc) { |
| 2460 | unsigned NumInit = initlist.size(); |
| 2461 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2462 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2463 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2464 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2465 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2466 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2467 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2468 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2469 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2472 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 5ad49de | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2473 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2474 | UsualUnaryConversions(castExpr); |
| 2475 | |
| 2476 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2477 | // type needs to be scalar. |
| 2478 | if (castType->isVoidType()) { |
| 2479 | // Cast to void allows any expr type. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2480 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2481 | // We can't check any more until template instantiation time. |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2482 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2483 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2484 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2485 | (castType->isStructureType() || castType->isUnionType())) { |
| 2486 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2487 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2488 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2489 | << castType << castExpr->getSourceRange(); |
| 2490 | } else if (castType->isUnionType()) { |
| 2491 | // GCC cast to union extension |
| 2492 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2493 | RecordDecl::field_iterator Field, FieldEnd; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2494 | for (Field = RD->field_begin(Context), FieldEnd = RD->field_end(Context); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2495 | Field != FieldEnd; ++Field) { |
| 2496 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2497 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2498 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2499 | << castExpr->getSourceRange(); |
| 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | if (Field == FieldEnd) |
| 2504 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2505 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2506 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2507 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2508 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2509 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2510 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2511 | } else if (!castExpr->getType()->isScalarType() && |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2512 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2513 | return Diag(castExpr->getLocStart(), |
| 2514 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2515 | << castExpr->getType() << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2516 | } else if (castExpr->getType()->isVectorType()) { |
| 2517 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2518 | return true; |
| 2519 | } else if (castType->isVectorType()) { |
| 2520 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2521 | return true; |
Steve Naroff | ff6c802 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2522 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | 49fd7ad | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 2523 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2524 | } |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2528 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2529 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2530 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2531 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2532 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2533 | return Diag(R.getBegin(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2534 | Ty->isVectorType() ? |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2535 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2536 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2537 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2538 | } else |
| 2539 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2540 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2541 | << VectorTy << Ty << R; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2542 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2543 | return false; |
| 2544 | } |
| 2545 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2546 | Action::OwningExprResult |
| 2547 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2548 | SourceLocation RParenLoc, ExprArg Op) { |
| 2549 | assert((Ty != 0) && (Op.get() != 0) && |
| 2550 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2551 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2552 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2553 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2554 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2555 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2556 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2557 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2558 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2561 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 2562 | /// In that case, lhs = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2563 | /// C99 6.5.15 |
| 2564 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2565 | SourceLocation QuestionLoc) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2566 | UsualUnaryConversions(Cond); |
| 2567 | UsualUnaryConversions(LHS); |
| 2568 | UsualUnaryConversions(RHS); |
| 2569 | QualType CondTy = Cond->getType(); |
| 2570 | QualType LHSTy = LHS->getType(); |
| 2571 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2572 | |
| 2573 | // first, check the condition. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2574 | if (!Cond->isTypeDependent()) { |
| 2575 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2576 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2577 | << CondTy; |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2578 | return QualType(); |
| 2579 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2580 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2582 | // Now check the two expressions. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2583 | if ((LHS && LHS->isTypeDependent()) || (RHS && RHS->isTypeDependent())) |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2584 | return Context.DependentTy; |
| 2585 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2586 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2587 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2588 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2589 | UsualArithmeticConversions(LHS, RHS); |
| 2590 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2591 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2593 | // If both operands are the same structure or union type, the result is that |
| 2594 | // type. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2595 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2596 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2597 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2598 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2599 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2600 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2601 | // FIXME: Type of conditional expression must be complete in C mode. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2602 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2603 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2604 | // 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] | 2605 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2606 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2607 | if (!LHSTy->isVoidType()) |
| 2608 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2609 | << RHS->getSourceRange(); |
| 2610 | if (!RHSTy->isVoidType()) |
| 2611 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2612 | << LHS->getSourceRange(); |
| 2613 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2614 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2615 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2616 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2617 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2618 | // the type of the other operand." |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2619 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2620 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2621 | RHS->isNullPointerConstant(Context)) { |
| 2622 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2623 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2624 | } |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2625 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2626 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2627 | LHS->isNullPointerConstant(Context)) { |
| 2628 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2629 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2630 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2632 | // Handle the case where both operands are pointers before we handle null |
| 2633 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2634 | if (const PointerType *LHSPT = LHSTy->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2635 | if (const PointerType *RHSPT = RHSTy->getAsPointerType()) { |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2636 | // get the "pointed to" types |
| 2637 | QualType lhptee = LHSPT->getPointeeType(); |
| 2638 | QualType rhptee = RHSPT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2640 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2641 | if (lhptee->isVoidType() && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2642 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2643 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2644 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2645 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2646 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2647 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2648 | return destType; |
| 2649 | } |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2650 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2651 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2652 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2653 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2654 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2655 | return destType; |
| 2656 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2657 | |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2658 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
Chris Lattner | 676c86a | 2009-02-19 04:44:58 +0000 | [diff] [blame] | 2659 | // Two identical pointer types are always compatible. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2660 | return LHSTy; |
| 2661 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2663 | QualType compositeType = LHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2664 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2665 | // If either type is an Objective-C object type then check |
| 2666 | // compatibility according to Objective-C. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2667 | if (Context.isObjCObjectPointerType(LHSTy) || |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2668 | Context.isObjCObjectPointerType(RHSTy)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2669 | // If both operands are interfaces and either operand can be |
| 2670 | // assigned to the other, use that type as the composite |
| 2671 | // type. This allows |
| 2672 | // xxx ? (A*) a : (B*) b |
| 2673 | // where B is a subclass of A. |
| 2674 | // |
| 2675 | // Additionally, as for assignment, if either type is 'id' |
| 2676 | // allow silent coercion. Finally, if the types are |
| 2677 | // incompatible then make sure to use 'id' as the composite |
| 2678 | // type so the result is acceptable for sending messages to. |
| 2679 | |
Steve Naroff | 9fc9cb5 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2680 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2681 | // It could return the composite type. |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2682 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2683 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2684 | if (LHSIface && RHSIface && |
| 2685 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2686 | compositeType = LHSTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2687 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 5183f9e | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2688 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2689 | compositeType = RHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2690 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2691 | Context.isObjCIdStructType(rhptee)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2692 | compositeType = Context.getObjCIdType(); |
| 2693 | } else { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2694 | Diag(QuestionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2695 | << LHSTy << RHSTy |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2696 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2697 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2698 | ImpCastExprToType(LHS, incompatTy); |
| 2699 | ImpCastExprToType(RHS, incompatTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2700 | return incompatTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2701 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2702 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2703 | rhptee.getUnqualifiedType())) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2704 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 2705 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2706 | // In this situation, we assume void* type. No especially good |
| 2707 | // reason, but this is what gcc does, and we do have to pick |
| 2708 | // to get a consistent AST. |
| 2709 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2710 | ImpCastExprToType(LHS, incompatTy); |
| 2711 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | cd23bb2 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2712 | return incompatTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2713 | } |
| 2714 | // The pointer types are compatible. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2715 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2716 | // differently qualified versions of compatible types, the result type is |
| 2717 | // a pointer to an appropriately qualified version of the *composite* |
| 2718 | // type. |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2719 | // FIXME: Need to calculate the composite type. |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2720 | // FIXME: Need to add qualifiers |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2721 | ImpCastExprToType(LHS, compositeType); |
| 2722 | ImpCastExprToType(RHS, compositeType); |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2723 | return compositeType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2724 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2725 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2726 | |
Steve Naroff | 6ba2268 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 2727 | // GCC compatibility: soften pointer/integer mismatch. |
| 2728 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 2729 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 2730 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2731 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 2732 | return RHSTy; |
| 2733 | } |
| 2734 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 2735 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 2736 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2737 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 2738 | return LHSTy; |
| 2739 | } |
| 2740 | |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2741 | // Selection between block pointer types is ok as long as they are the same. |
| 2742 | if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() && |
| 2743 | Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) |
| 2744 | return LHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2745 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2746 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2747 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2748 | // id with statically typed objects). |
| 2749 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2750 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2751 | // id. Currently localizing to here until clear this should be |
| 2752 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2753 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2754 | (LHSTy->isObjCQualifiedIdType() && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2755 | Context.isObjCObjectPointerType(RHSTy)) || |
| 2756 | (RHSTy->isObjCQualifiedIdType() && |
| 2757 | Context.isObjCObjectPointerType(LHSTy))) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2758 | // FIXME: This is not the correct composite type. This only |
| 2759 | // happens to work because id can more or less be used anywhere, |
| 2760 | // however this may change the type of method sends. |
| 2761 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2762 | // (confusing) incompatible comparison warnings in some |
| 2763 | // cases. Investigate. |
| 2764 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2765 | ImpCastExprToType(LHS, compositeType); |
| 2766 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2767 | return compositeType; |
| 2768 | } |
| 2769 | } |
| 2770 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2771 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2772 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2773 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2774 | return QualType(); |
| 2775 | } |
| 2776 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2777 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2778 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2779 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2780 | SourceLocation ColonLoc, |
| 2781 | ExprArg Cond, ExprArg LHS, |
| 2782 | ExprArg RHS) { |
| 2783 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2784 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2785 | |
| 2786 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2787 | // was the condition. |
| 2788 | bool isLHSNull = LHSExpr == 0; |
| 2789 | if (isLHSNull) |
| 2790 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2791 | |
| 2792 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2793 | RHSExpr, QuestionLoc); |
| 2794 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2795 | return ExprError(); |
| 2796 | |
| 2797 | Cond.release(); |
| 2798 | LHS.release(); |
| 2799 | RHS.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2800 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2801 | isLHSNull ? 0 : LHSExpr, |
| 2802 | RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2805 | |
| 2806 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2807 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2808 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2809 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2810 | // FIXME: add a couple examples in this comment. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2811 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2812 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2813 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2814 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2815 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2816 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2817 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2818 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2819 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2820 | lhptee = Context.getCanonicalType(lhptee); |
| 2821 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2822 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2823 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2824 | |
| 2825 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2826 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2827 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2828 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2829 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2830 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2831 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2832 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2833 | // incomplete type and the other is a pointer to a qualified or unqualified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2834 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2835 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2836 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2837 | return ConvTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2839 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2840 | assert(rhptee->isFunctionType()); |
| 2841 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2842 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2843 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2844 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2845 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2846 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2847 | |
| 2848 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2849 | assert(lhptee->isFunctionType()); |
| 2850 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2851 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2852 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2853 | // unqualified versions of compatible types, ... |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 2854 | lhptee = lhptee.getUnqualifiedType(); |
| 2855 | rhptee = rhptee.getUnqualifiedType(); |
| 2856 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 2857 | // Check if the pointee types are compatible ignoring the sign. |
| 2858 | // We explicitly check for char so that we catch "char" vs |
| 2859 | // "unsigned char" on systems where "char" is unsigned. |
| 2860 | if (lhptee->isCharType()) { |
| 2861 | lhptee = Context.UnsignedCharTy; |
| 2862 | } else if (lhptee->isSignedIntegerType()) { |
| 2863 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 2864 | } |
| 2865 | if (rhptee->isCharType()) { |
| 2866 | rhptee = Context.UnsignedCharTy; |
| 2867 | } else if (rhptee->isSignedIntegerType()) { |
| 2868 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 2869 | } |
| 2870 | if (lhptee == rhptee) { |
| 2871 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 2872 | // takes priority over sign incompatibility because the sign |
| 2873 | // warning can be disabled. |
| 2874 | if (ConvTy != Compatible) |
| 2875 | return ConvTy; |
| 2876 | return IncompatiblePointerSign; |
| 2877 | } |
| 2878 | // General pointer incompatibility takes priority over qualifiers. |
| 2879 | return IncompatiblePointer; |
| 2880 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2881 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2884 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2885 | /// block pointer types are compatible or whether a block and normal pointer |
| 2886 | /// are compatible. It is more restrict than comparing two function pointer |
| 2887 | // types. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2888 | Sema::AssignConvertType |
| 2889 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2890 | QualType rhsType) { |
| 2891 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2892 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2893 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2894 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2895 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2896 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2897 | // make sure we operate on the canonical type |
| 2898 | lhptee = Context.getCanonicalType(lhptee); |
| 2899 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2900 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2901 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2902 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2903 | // For blocks we enforce that qualifiers are identical. |
| 2904 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2905 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2906 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2907 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2908 | return IncompatibleBlockPointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2909 | return ConvTy; |
| 2910 | } |
| 2911 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2912 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2913 | /// has code to accommodate several GCC extensions when type checking |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2914 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2915 | /// |
| 2916 | /// int a, *pint; |
| 2917 | /// short *pshort; |
| 2918 | /// struct foo *pfoo; |
| 2919 | /// |
| 2920 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2921 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2922 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2923 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2924 | /// |
| 2925 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2926 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2927 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2928 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2929 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2930 | // Get canonical types. We're not formatting these types, just comparing |
| 2931 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2932 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2933 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2934 | |
| 2935 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2936 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2937 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2938 | // If the left-hand side is a reference type, then we are in a |
| 2939 | // (rare!) case where we've allowed the use of references in C, |
| 2940 | // e.g., as a parameter type in a built-in function. In this case, |
| 2941 | // just make sure that the type referenced is compatible with the |
| 2942 | // right-hand side type. The caller is responsible for adjusting |
| 2943 | // lhsType so that the resulting expression does not have reference |
| 2944 | // type. |
| 2945 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2946 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2947 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2948 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2949 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2950 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2951 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2952 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2953 | return Compatible; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2954 | // Relax integer conversions like we do for pointers below. |
| 2955 | if (rhsType->isIntegerType()) |
| 2956 | return IntToPointer; |
| 2957 | if (lhsType->isIntegerType()) |
| 2958 | return PointerToInt; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2959 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2960 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2961 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2962 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2963 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2964 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2965 | if (LV->getElementType() == rhsType) |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2966 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2967 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2968 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2969 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2970 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2971 | if (getLangOptions().LaxVectorConversions && |
| 2972 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2973 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2974 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2975 | } |
| 2976 | return Incompatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2977 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2978 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2979 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2980 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2981 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2982 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2983 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2984 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2985 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2986 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2987 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2988 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2989 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2990 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2991 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2992 | |
| 2993 | // Treat block pointers as objects. |
| 2994 | if (getLangOptions().ObjC1 && |
| 2995 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2996 | return Compatible; |
| 2997 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2998 | return Incompatible; |
| 2999 | } |
| 3000 | |
| 3001 | if (isa<BlockPointerType>(lhsType)) { |
| 3002 | if (rhsType->isIntegerType()) |
Eli Friedman | c589830 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3003 | return IntToBlockPointer; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3004 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3005 | // Treat block pointers as objects. |
| 3006 | if (getLangOptions().ObjC1 && |
| 3007 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 3008 | return Compatible; |
| 3009 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3010 | if (rhsType->isBlockPointerType()) |
| 3011 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3012 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3013 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 3014 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3015 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3016 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3017 | return Incompatible; |
| 3018 | } |
| 3019 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3020 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3021 | // 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] | 3022 | if (lhsType == Context.BoolTy) |
| 3023 | return Compatible; |
| 3024 | |
| 3025 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3026 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3027 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3028 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3029 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3030 | |
| 3031 | if (isa<BlockPointerType>(lhsType) && |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3032 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3033 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3034 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3035 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3036 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3037 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3038 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3039 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3040 | } |
| 3041 | return Incompatible; |
| 3042 | } |
| 3043 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3044 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3045 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3046 | if (getLangOptions().CPlusPlus) { |
| 3047 | if (!lhsType->isRecordType()) { |
| 3048 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3049 | // expression is implicitly converted (C++ 4) to the |
| 3050 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3051 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3052 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3053 | return Incompatible; |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 3054 | else |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3055 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3059 | // structures. |
| 3060 | } |
| 3061 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3062 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3063 | // a null pointer constant. |
Steve Naroff | d305a86 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 3064 | if ((lhsType->isPointerType() || |
| 3065 | lhsType->isObjCQualifiedIdType() || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3066 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3067 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3068 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3069 | return Compatible; |
| 3070 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3072 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3073 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3074 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3075 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3076 | // |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3077 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3078 | if (!lhsType->isReferenceType()) |
| 3079 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3081 | Sema::AssignConvertType result = |
| 3082 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3083 | |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3084 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3085 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3086 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3087 | // so that we can use references in built-in functions even in C. |
| 3088 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3089 | // does not have reference type. |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3090 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3091 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3092 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3095 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3096 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 3097 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 3098 | } |
| 3099 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3100 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3101 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3102 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3103 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3104 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3107 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3108 | Expr *&rex) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3109 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3110 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3111 | QualType lhsType = |
| 3112 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3113 | QualType rhsType = |
| 3114 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3115 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3116 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3117 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3118 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3119 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3120 | // Handle the case of a vector & extvector type of the same size and element |
| 3121 | // 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] | 3122 | if (getLangOptions().LaxVectorConversions) { |
| 3123 | // FIXME: Should we warn here? |
| 3124 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3125 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3126 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3127 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3128 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3132 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3133 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 3134 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3135 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3136 | QualType eltType = V->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3137 | |
| 3138 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3139 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 3140 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3141 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3142 | return lhsType; |
| 3143 | } |
| 3144 | } |
| 3145 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3146 | // 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] | 3147 | // promote the lhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3148 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3149 | QualType eltType = V->getElementType(); |
| 3150 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3151 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3152 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 3153 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3154 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3155 | return rhsType; |
| 3156 | } |
| 3157 | } |
| 3158 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3159 | // You cannot convert between vector values of different size. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3160 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3161 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3162 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3163 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3166 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3167 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3168 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 3169 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3170 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3171 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3172 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3173 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3174 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3175 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3176 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3180 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3181 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 3182 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3183 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 3184 | return CheckVectorOperands(Loc, lex, rex); |
| 3185 | return InvalidOperands(Loc, lex, rex); |
| 3186 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3187 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3188 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3190 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3191 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3192 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3196 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3197 | { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3198 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3199 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3200 | if (CompLHSTy) *CompLHSTy = compType; |
| 3201 | return compType; |
| 3202 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3203 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3204 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3206 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3207 | if (lex->getType()->isArithmeticType() && |
| 3208 | rex->getType()->isArithmeticType()) { |
| 3209 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3210 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3211 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3212 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3213 | // Put any potential pointer into PExp |
| 3214 | Expr* PExp = lex, *IExp = rex; |
| 3215 | if (IExp->getType()->isPointerType()) |
| 3216 | std::swap(PExp, IExp); |
| 3217 | |
| 3218 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 3219 | if (IExp->getType()->isIntegerType()) { |
| 3220 | // Check for arithmetic on pointers to incomplete types |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3221 | if (PTy->getPointeeType()->isVoidType()) { |
| 3222 | if (getLangOptions().CPlusPlus) { |
| 3223 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3224 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3225 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3226 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3227 | |
| 3228 | // GNU extension: arithmetic on pointer to void |
| 3229 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3230 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3231 | } else if (PTy->getPointeeType()->isFunctionType()) { |
| 3232 | if (getLangOptions().CPlusPlus) { |
| 3233 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3234 | << lex->getType() << lex->getSourceRange(); |
| 3235 | return QualType(); |
| 3236 | } |
| 3237 | |
| 3238 | // GNU extension: arithmetic on pointer to function |
| 3239 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3240 | << lex->getType() << lex->getSourceRange(); |
| 3241 | } else if (!PTy->isDependentType() && |
| 3242 | RequireCompleteType(Loc, PTy->getPointeeType(), |
| 3243 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3244 | lex->getSourceRange(), SourceRange(), |
| 3245 | lex->getType())) |
| 3246 | return QualType(); |
| 3247 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3248 | if (CompLHSTy) { |
| 3249 | QualType LHSTy = lex->getType(); |
| 3250 | if (LHSTy->isPromotableIntegerType()) |
| 3251 | LHSTy = Context.IntTy; |
| 3252 | *CompLHSTy = LHSTy; |
| 3253 | } |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3254 | return PExp->getType(); |
| 3255 | } |
| 3256 | } |
| 3257 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3258 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3261 | // C99 6.5.6 |
| 3262 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3263 | SourceLocation Loc, QualType* CompLHSTy) { |
| 3264 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3265 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3266 | if (CompLHSTy) *CompLHSTy = compType; |
| 3267 | return compType; |
| 3268 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3269 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3270 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3272 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3273 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3274 | // Handle the common case first (both operands are arithmetic). |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3275 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) { |
| 3276 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3277 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3278 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3279 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3280 | // Either ptr - int or ptr - ptr. |
| 3281 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3282 | QualType lpointee = LHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3283 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3284 | // The LHS must be an completely-defined object type. |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3285 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3286 | bool ComplainAboutVoid = false; |
| 3287 | Expr *ComplainAboutFunc = 0; |
| 3288 | if (lpointee->isVoidType()) { |
| 3289 | if (getLangOptions().CPlusPlus) { |
| 3290 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3291 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3292 | return QualType(); |
| 3293 | } |
| 3294 | |
| 3295 | // GNU C extension: arithmetic on pointer to void |
| 3296 | ComplainAboutVoid = true; |
| 3297 | } else if (lpointee->isFunctionType()) { |
| 3298 | if (getLangOptions().CPlusPlus) { |
| 3299 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3300 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3301 | return QualType(); |
| 3302 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3303 | |
| 3304 | // GNU C extension: arithmetic on pointer to function |
| 3305 | ComplainAboutFunc = lex; |
| 3306 | } else if (!lpointee->isDependentType() && |
| 3307 | RequireCompleteType(Loc, lpointee, |
| 3308 | diag::err_typecheck_sub_ptr_object, |
| 3309 | lex->getSourceRange(), |
| 3310 | SourceRange(), |
| 3311 | lex->getType())) |
| 3312 | return QualType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3313 | |
| 3314 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3315 | if (rex->getType()->isIntegerType()) { |
| 3316 | if (ComplainAboutVoid) |
| 3317 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3318 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3319 | if (ComplainAboutFunc) |
| 3320 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3321 | << ComplainAboutFunc->getType() |
| 3322 | << ComplainAboutFunc->getSourceRange(); |
| 3323 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3324 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3325 | return lex->getType(); |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3326 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3327 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3328 | // Handle pointer-pointer subtractions. |
| 3329 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3330 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3332 | // RHS must be a completely-type object type. |
| 3333 | // Handle the GNU void* extension. |
| 3334 | if (rpointee->isVoidType()) { |
| 3335 | if (getLangOptions().CPlusPlus) { |
| 3336 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3337 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3338 | return QualType(); |
| 3339 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3341 | ComplainAboutVoid = true; |
| 3342 | } else if (rpointee->isFunctionType()) { |
| 3343 | if (getLangOptions().CPlusPlus) { |
| 3344 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3345 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3346 | return QualType(); |
| 3347 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3348 | |
| 3349 | // GNU extension: arithmetic on pointer to function |
| 3350 | if (!ComplainAboutFunc) |
| 3351 | ComplainAboutFunc = rex; |
| 3352 | } else if (!rpointee->isDependentType() && |
| 3353 | RequireCompleteType(Loc, rpointee, |
| 3354 | diag::err_typecheck_sub_ptr_object, |
| 3355 | rex->getSourceRange(), |
| 3356 | SourceRange(), |
| 3357 | rex->getType())) |
| 3358 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3359 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3360 | // Pointee types must be compatible. |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3361 | if (!Context.typesAreCompatible( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3362 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3363 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3364 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3365 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3366 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3367 | return QualType(); |
| 3368 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3370 | if (ComplainAboutVoid) |
| 3371 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3372 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3373 | if (ComplainAboutFunc) |
| 3374 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3375 | << ComplainAboutFunc->getType() |
| 3376 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3377 | |
| 3378 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3379 | return Context.getPointerDiffType(); |
| 3380 | } |
| 3381 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3382 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3383 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3386 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3387 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3388 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3389 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3390 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3391 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3392 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3393 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3394 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3395 | QualType LHSTy; |
| 3396 | if (lex->getType()->isPromotableIntegerType()) |
| 3397 | LHSTy = Context.IntTy; |
| 3398 | else |
| 3399 | LHSTy = lex->getType(); |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3400 | if (!isCompAssign) |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3401 | ImpCastExprToType(lex, LHSTy); |
| 3402 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3403 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3404 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3405 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3406 | return LHSTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3409 | // C99 6.5.8 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3410 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3411 | unsigned OpaqueOpc, bool isRelational) { |
| 3412 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 3413 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3414 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3415 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3417 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3418 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3419 | UsualArithmeticConversions(lex, rex); |
| 3420 | else { |
| 3421 | UsualUnaryConversions(lex); |
| 3422 | UsualUnaryConversions(rex); |
| 3423 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3424 | QualType lType = lex->getType(); |
| 3425 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3426 | |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3427 | if (!lType->isFloatingType()) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3428 | // For non-floating point types, check for self-comparisons of the form |
| 3429 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3430 | // often indicate logic errors in the program. |
Ted Kremenek | 264b5cb | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 3431 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
| 3432 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3433 | Expr *LHSStripped = lex->IgnoreParens(); |
| 3434 | Expr *RHSStripped = rex->IgnoreParens(); |
| 3435 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 3436 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | f042dc6 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 3437 | if (DRL->getDecl() == DRR->getDecl() && |
| 3438 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3439 | Diag(Loc, diag::warn_selfcomparison); |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3440 | |
| 3441 | if (isa<CastExpr>(LHSStripped)) |
| 3442 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 3443 | if (isa<CastExpr>(RHSStripped)) |
| 3444 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| 3445 | |
| 3446 | // Warn about comparisons against a string constant (unless the other |
| 3447 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3448 | Expr *literalString = 0; |
| 3449 | Expr *literalStringStripped = 0; |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3450 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3451 | !RHSStripped->isNullPointerConstant(Context)) { |
| 3452 | literalString = lex; |
| 3453 | literalStringStripped = LHSStripped; |
| 3454 | } |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3455 | else if ((isa<StringLiteral>(RHSStripped) || |
| 3456 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3457 | !LHSStripped->isNullPointerConstant(Context)) { |
| 3458 | literalString = rex; |
| 3459 | literalStringStripped = RHSStripped; |
| 3460 | } |
| 3461 | |
| 3462 | if (literalString) { |
| 3463 | std::string resultComparison; |
| 3464 | switch (Opc) { |
| 3465 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 3466 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 3467 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 3468 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 3469 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 3470 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 3471 | default: assert(false && "Invalid comparison operator"); |
| 3472 | } |
| 3473 | Diag(Loc, diag::warn_stringcompare) |
| 3474 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 3475 | << literalString->getSourceRange() |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3476 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 3477 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 3478 | "strcmp(") |
| 3479 | << CodeModificationHint::CreateInsertion( |
| 3480 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3481 | resultComparison); |
| 3482 | } |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3483 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3485 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3486 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3487 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3488 | if (isRelational) { |
| 3489 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3490 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3491 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3492 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3493 | if (lType->isFloatingType()) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3494 | assert(rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3495 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3496 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3497 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3498 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3499 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3500 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3501 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3502 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3503 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3504 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3505 | // All of the following pointer related warnings are GCC extensions, except |
| 3506 | // when handling null pointer constants. One day, we can consider making them |
| 3507 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3508 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3509 | QualType LCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3510 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3511 | QualType RCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3512 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3513 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3514 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3515 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3516 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3517 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3518 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3519 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3520 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3521 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3522 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3523 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3524 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3525 | // Handle block pointer types. |
| 3526 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 3527 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3528 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3529 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3530 | if (!LHSIsNull && !RHSIsNull && |
| 3531 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3532 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3533 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3534 | } |
| 3535 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3536 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3537 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3538 | // Allow block pointers to be compared with null pointer constants. |
| 3539 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 3540 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 3541 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3542 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3543 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3544 | } |
| 3545 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3546 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3547 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3548 | |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3549 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3550 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3551 | const PointerType *LPT = lType->getAsPointerType(); |
| 3552 | const PointerType *RPT = rType->getAsPointerType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3553 | bool LPtrToVoid = LPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3554 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3555 | bool RPtrToVoid = RPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3556 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3557 | |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3558 | if (!LPtrToVoid && !RPtrToVoid && |
| 3559 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3560 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3561 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3562 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3563 | return ResultTy; |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3564 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3565 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3566 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 3567 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3568 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 3569 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3570 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3571 | } else { |
| 3572 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3573 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3574 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3575 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3576 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3577 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3578 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 3579 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3580 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3581 | rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3582 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3583 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3584 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3585 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3586 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3587 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3588 | if (lType->isIntegerType() && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3589 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3590 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3591 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3592 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3593 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3594 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3595 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3596 | // Handle block pointers. |
| 3597 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 3598 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3599 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3600 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3601 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3602 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3603 | } |
| 3604 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 3605 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3606 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3607 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3608 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3609 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3610 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3611 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3614 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3615 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3616 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3617 | /// types. |
| 3618 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3619 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3620 | bool isRelational) { |
| 3621 | // Check to make sure we're operating on vectors of the same type and width, |
| 3622 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3623 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3624 | if (vType.isNull()) |
| 3625 | return vType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3626 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3627 | QualType lType = lex->getType(); |
| 3628 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3629 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3630 | // For non-floating point types, check for self-comparisons of the form |
| 3631 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3632 | // often indicate logic errors in the program. |
| 3633 | if (!lType->isFloatingType()) { |
| 3634 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3635 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3636 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3637 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3638 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3639 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3640 | // Check for comparisons of floating point operands using != and ==. |
| 3641 | if (!isRelational && lType->isFloatingType()) { |
| 3642 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3643 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3644 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3645 | |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 3646 | // FIXME: Vector compare support in the LLVM backend is not fully reliable, |
| 3647 | // just reject all vector comparisons for now. |
| 3648 | if (1) { |
| 3649 | Diag(Loc, diag::err_typecheck_vector_comparison) |
| 3650 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 3651 | return QualType(); |
| 3652 | } |
| 3653 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3654 | // Return the type for the comparison, which is the same as vector type for |
| 3655 | // integer vectors, or an integer type of identical size and number of |
| 3656 | // elements for floating point vectors. |
| 3657 | if (lType->isIntegerType()) |
| 3658 | return lType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3659 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3660 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3661 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3662 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3663 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 3664 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3665 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3666 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3667 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3668 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3669 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3670 | } |
| 3671 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3672 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3673 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3674 | { |
| 3675 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3676 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3677 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3678 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3679 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3680 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3681 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3682 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3686 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3687 | { |
| 3688 | UsualUnaryConversions(lex); |
| 3689 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3690 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3691 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3692 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3693 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3696 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3697 | /// is a read-only property; return true if so. A readonly property expression |
| 3698 | /// depends on various declarations and thus must be treated specially. |
| 3699 | /// |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3700 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3701 | { |
| 3702 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3703 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3704 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3705 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3706 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3707 | if (const ObjCInterfaceType *IFTy = |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3708 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3709 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3710 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3711 | return true; |
| 3712 | } |
| 3713 | } |
| 3714 | return false; |
| 3715 | } |
| 3716 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3717 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3718 | /// emit an error and return true. If so, return false. |
| 3719 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3720 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3721 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3722 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3723 | if (IsLV == Expr::MLV_Valid) |
| 3724 | return false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3725 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3726 | unsigned Diag = 0; |
| 3727 | bool NeedType = false; |
| 3728 | switch (IsLV) { // C99 6.5.16p2 |
| 3729 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3730 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3731 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3732 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3733 | NeedType = true; |
| 3734 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3735 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3736 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3737 | NeedType = true; |
| 3738 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3739 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3740 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3741 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3742 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3743 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3744 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3745 | case Expr::MLV_IncompleteType: |
| 3746 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 3747 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3748 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3749 | E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3750 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3751 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3752 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3753 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3754 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3755 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3756 | case Expr::MLV_ReadonlyProperty: |
| 3757 | Diag = diag::error_readonly_property_assignment; |
| 3758 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3759 | case Expr::MLV_NoSetterProperty: |
| 3760 | Diag = diag::error_nosetter_property_assignment; |
| 3761 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3762 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3763 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3764 | if (NeedType) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3765 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3766 | else |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3767 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3768 | return true; |
| 3769 | } |
| 3770 | |
| 3771 | |
| 3772 | |
| 3773 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3774 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3775 | SourceLocation Loc, |
| 3776 | QualType CompoundType) { |
| 3777 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3778 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3779 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3780 | |
| 3781 | QualType LHSType = LHS->getType(); |
| 3782 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3783 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3784 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3785 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3786 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3787 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3788 | // Special case of NSObject attributes on c-style pointer types. |
| 3789 | if (ConvTy == IncompatiblePointer && |
| 3790 | ((Context.isObjCNSObjectType(LHSType) && |
| 3791 | Context.isObjCObjectPointerType(RHSType)) || |
| 3792 | (Context.isObjCNSObjectType(RHSType) && |
| 3793 | Context.isObjCObjectPointerType(LHSType)))) |
| 3794 | ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3795 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3796 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3797 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3798 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3799 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3800 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3801 | RHSCheck = ICE->getSubExpr(); |
| 3802 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3803 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3804 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3805 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3806 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 3807 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 3808 | // And there is a space or other character before the subexpr of the |
| 3809 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | f1e5d4a | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 3810 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 3811 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3812 | Diag(Loc, diag::warn_not_compound_assign) |
| 3813 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3814 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 3815 | } |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3816 | } |
| 3817 | } else { |
| 3818 | // Compound assignment "x += y" |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3819 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3820 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3821 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3822 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3823 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3824 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3825 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3826 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3827 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3828 | // it is the unqualified version of the type of the left operand. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3829 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3830 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3831 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3832 | // oprdu. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3833 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3836 | // C99 6.5.17 |
| 3837 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3838 | // 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] | 3839 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3840 | |
| 3841 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 3842 | // incomplete in C++). |
| 3843 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3844 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3848 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3849 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3850 | bool isInc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3851 | if (Op->isTypeDependent()) |
| 3852 | return Context.DependentTy; |
| 3853 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3854 | QualType ResType = Op->getType(); |
| 3855 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3856 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3857 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3858 | // Decrement of bool is not allowed. |
| 3859 | if (!isInc) { |
| 3860 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3861 | return QualType(); |
| 3862 | } |
| 3863 | // Increment of bool sets it to true, but is deprecated. |
| 3864 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3865 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3866 | // OK! |
| 3867 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3868 | // C99 6.5.2.4p2, 6.5.6p2 |
Douglas Gregor | cde3a2d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 3869 | if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3870 | if (getLangOptions().CPlusPlus) { |
| 3871 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3872 | << Op->getSourceRange(); |
| 3873 | return QualType(); |
| 3874 | } |
| 3875 | |
| 3876 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3877 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3878 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3879 | if (getLangOptions().CPlusPlus) { |
| 3880 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3881 | << Op->getType() << Op->getSourceRange(); |
| 3882 | return QualType(); |
| 3883 | } |
| 3884 | |
| 3885 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3886 | << ResType << Op->getSourceRange(); |
Douglas Gregor | cde3a2d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 3887 | } else if (RequireCompleteType(OpLoc, PT->getPointeeType(), |
| 3888 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3889 | Op->getSourceRange(), SourceRange(), |
| 3890 | ResType)) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3891 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3892 | } else if (ResType->isComplexType()) { |
| 3893 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3894 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3895 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3896 | } else { |
| 3897 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3898 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3899 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3900 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3901 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 3902 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3903 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3904 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3905 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3908 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3909 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3910 | /// where the declaration is needed for type checking. We only need to |
| 3911 | /// handle cases when the expression references a function designator |
| 3912 | /// or is an lvalue. Here are some examples: |
| 3913 | /// - &(x) => x |
| 3914 | /// - &*****f => f for f a function designator. |
| 3915 | /// - &s.xx => s |
| 3916 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3917 | /// - *(x + 1) -> x, if x is an array |
| 3918 | /// - &"123"[2] -> 0 |
| 3919 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3920 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3921 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3922 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3923 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3924 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3925 | case Stmt::MemberExprClass: |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3926 | // Fields cannot be declared with a 'register' storage class. |
| 3927 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3928 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3929 | return 0; |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3930 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3931 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3932 | // &X[4] and &4[X] refers to X if X is not a pointer. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3933 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3934 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 612720d | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3935 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | 655694e | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3936 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3937 | return 0; |
| 3938 | else |
| 3939 | return VD; |
| 3940 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3941 | case Stmt::UnaryOperatorClass: { |
| 3942 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3943 | |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3944 | switch(UO->getOpcode()) { |
| 3945 | case UnaryOperator::Deref: { |
| 3946 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3947 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3948 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3949 | if (!VD || VD->getType()->isPointerType()) |
| 3950 | return 0; |
| 3951 | return VD; |
| 3952 | } |
| 3953 | return 0; |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3954 | } |
| 3955 | case UnaryOperator::Real: |
| 3956 | case UnaryOperator::Imag: |
| 3957 | case UnaryOperator::Extension: |
| 3958 | return getPrimaryDecl(UO->getSubExpr()); |
| 3959 | default: |
| 3960 | return 0; |
| 3961 | } |
| 3962 | } |
| 3963 | case Stmt::BinaryOperatorClass: { |
| 3964 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3965 | |
| 3966 | // Handle cases involving pointer arithmetic. The result of an |
| 3967 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3968 | |
| 3969 | // (x + n) or (n + x) => x |
| 3970 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3971 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3972 | return getPrimaryDecl(BO->getLHS()); |
| 3973 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3974 | return getPrimaryDecl(BO->getRHS()); |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | return 0; |
| 3979 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3980 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3981 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3982 | case Stmt::ImplicitCastExprClass: |
| 3983 | // &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] | 3984 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3985 | default: |
| 3986 | return 0; |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3991 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3992 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3993 | /// Note: The usual conversions are *not* applied to the operand of the & |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3994 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3995 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3996 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3997 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3998 | if (op->isTypeDependent()) |
| 3999 | return Context.DependentTy; |
| 4000 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4001 | if (getLangOptions().C99) { |
| 4002 | // Implement C99-only parts of addressof rules. |
| 4003 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 4004 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 4005 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 4006 | // (assuming the deref expression is valid). |
| 4007 | return uOp->getSubExpr()->getType(); |
| 4008 | } |
| 4009 | // Technically, there should be a check for array subscript |
| 4010 | // expressions here, but the result of one is always an lvalue anyway. |
| 4011 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4012 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 4013 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 4014 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4015 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4016 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 4017 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4018 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 4019 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4020 | return QualType(); |
| 4021 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4022 | } 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] | 4023 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 4024 | if (Field->isBitField()) { |
| 4025 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4026 | << "bit-field" << op->getSourceRange(); |
| 4027 | return QualType(); |
| 4028 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4029 | } |
| 4030 | // Check for Apple extension for accessing vector components. |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4031 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 4032 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4033 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4034 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4035 | return QualType(); |
| 4036 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4037 | // We have an lvalue with a decl. Make sure the decl is not declared |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4038 | // with the register storage-class specifier. |
| 4039 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 4040 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4041 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4042 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4043 | return QualType(); |
| 4044 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4045 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4046 | return Context.OverloadTy; |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4047 | } else if (isa<FieldDecl>(dcl)) { |
| 4048 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4049 | // Could be a pointer to member, though, if there is an explicit |
| 4050 | // scope qualifier for the class. |
| 4051 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 4052 | DeclContext *Ctx = dcl->getDeclContext(); |
| 4053 | if (Ctx && Ctx->isRecord()) |
| 4054 | return Context.getMemberPointerType(op->getType(), |
| 4055 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 4056 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 4057 | } else if (isa<FunctionDecl>(dcl)) { |
| 4058 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4059 | // As above. |
| 4060 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 4061 | DeclContext *Ctx = dcl->getDeclContext(); |
| 4062 | if (Ctx && Ctx->isRecord()) |
| 4063 | return Context.getMemberPointerType(op->getType(), |
| 4064 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 4065 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4066 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 4067 | else |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4068 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4069 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4070 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4071 | // If the operand has type "type", the result has type "pointer to type". |
| 4072 | return Context.getPointerType(op->getType()); |
| 4073 | } |
| 4074 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4075 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4076 | if (Op->isTypeDependent()) |
| 4077 | return Context.DependentTy; |
| 4078 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4079 | UsualUnaryConversions(Op); |
| 4080 | QualType Ty = Op->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4081 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4082 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 4083 | // incomplete type or void. It would be possible to warn about dereferencing |
| 4084 | // a void pointer, but it's completely well-defined, and such a warning is |
| 4085 | // unlikely to catch any mistakes. |
| 4086 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4087 | return PT->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4088 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4089 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4090 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4091 | return QualType(); |
| 4092 | } |
| 4093 | |
| 4094 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 4095 | tok::TokenKind Kind) { |
| 4096 | BinaryOperator::Opcode Opc; |
| 4097 | switch (Kind) { |
| 4098 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4099 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 4100 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4101 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 4102 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 4103 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 4104 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 4105 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 4106 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 4107 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 4108 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 4109 | case tok::less: Opc = BinaryOperator::LT; break; |
| 4110 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 4111 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 4112 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 4113 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 4114 | case tok::amp: Opc = BinaryOperator::And; break; |
| 4115 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 4116 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 4117 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 4118 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 4119 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 4120 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 4121 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 4122 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 4123 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 4124 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 4125 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 4126 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 4127 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 4128 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 4129 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 4130 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 4131 | } |
| 4132 | return Opc; |
| 4133 | } |
| 4134 | |
| 4135 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 4136 | tok::TokenKind Kind) { |
| 4137 | UnaryOperator::Opcode Opc; |
| 4138 | switch (Kind) { |
| 4139 | default: assert(0 && "Unknown unary op!"); |
| 4140 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 4141 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 4142 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 4143 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 4144 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 4145 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 4146 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 4147 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4148 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 4149 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 4150 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 4151 | } |
| 4152 | return Opc; |
| 4153 | } |
| 4154 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4155 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 4156 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 4157 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4158 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 4159 | unsigned Op, |
| 4160 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4161 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4162 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4163 | // The following two variables are used for compound assignment operators |
| 4164 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 4165 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4166 | |
| 4167 | switch (Opc) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4168 | case BinaryOperator::Assign: |
| 4169 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 4170 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4171 | case BinaryOperator::PtrMemD: |
| 4172 | case BinaryOperator::PtrMemI: |
| 4173 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 4174 | Opc == BinaryOperator::PtrMemI); |
| 4175 | break; |
| 4176 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4177 | case BinaryOperator::Div: |
| 4178 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 4179 | break; |
| 4180 | case BinaryOperator::Rem: |
| 4181 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 4182 | break; |
| 4183 | case BinaryOperator::Add: |
| 4184 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 4185 | break; |
| 4186 | case BinaryOperator::Sub: |
| 4187 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 4188 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4189 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4190 | case BinaryOperator::Shr: |
| 4191 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 4192 | break; |
| 4193 | case BinaryOperator::LE: |
| 4194 | case BinaryOperator::LT: |
| 4195 | case BinaryOperator::GE: |
| 4196 | case BinaryOperator::GT: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4197 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4198 | break; |
| 4199 | case BinaryOperator::EQ: |
| 4200 | case BinaryOperator::NE: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4201 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4202 | break; |
| 4203 | case BinaryOperator::And: |
| 4204 | case BinaryOperator::Xor: |
| 4205 | case BinaryOperator::Or: |
| 4206 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 4207 | break; |
| 4208 | case BinaryOperator::LAnd: |
| 4209 | case BinaryOperator::LOr: |
| 4210 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 4211 | break; |
| 4212 | case BinaryOperator::MulAssign: |
| 4213 | case BinaryOperator::DivAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4214 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 4215 | CompLHSTy = CompResultTy; |
| 4216 | if (!CompResultTy.isNull()) |
| 4217 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4218 | break; |
| 4219 | case BinaryOperator::RemAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4220 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 4221 | CompLHSTy = CompResultTy; |
| 4222 | if (!CompResultTy.isNull()) |
| 4223 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4224 | break; |
| 4225 | case BinaryOperator::AddAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4226 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4227 | if (!CompResultTy.isNull()) |
| 4228 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4229 | break; |
| 4230 | case BinaryOperator::SubAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4231 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4232 | if (!CompResultTy.isNull()) |
| 4233 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4234 | break; |
| 4235 | case BinaryOperator::ShlAssign: |
| 4236 | case BinaryOperator::ShrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4237 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 4238 | CompLHSTy = CompResultTy; |
| 4239 | if (!CompResultTy.isNull()) |
| 4240 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4241 | break; |
| 4242 | case BinaryOperator::AndAssign: |
| 4243 | case BinaryOperator::XorAssign: |
| 4244 | case BinaryOperator::OrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4245 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 4246 | CompLHSTy = CompResultTy; |
| 4247 | if (!CompResultTy.isNull()) |
| 4248 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4249 | break; |
| 4250 | case BinaryOperator::Comma: |
| 4251 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 4252 | break; |
| 4253 | } |
| 4254 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4255 | return ExprError(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4256 | if (CompResultTy.isNull()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4257 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 4258 | else |
| 4259 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4260 | CompLHSTy, CompResultTy, |
| 4261 | OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4264 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4265 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 4266 | tok::TokenKind Kind, |
| 4267 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4268 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4269 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4270 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4271 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 4272 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4274 | if (getLangOptions().CPlusPlus && |
| 4275 | (lhs->getType()->isOverloadableType() || |
| 4276 | rhs->getType()->isOverloadableType())) { |
| 4277 | // Find all of the overloaded operators visible from this |
| 4278 | // point. We perform both an operator-name lookup from the local |
| 4279 | // scope and an argument-dependent lookup based on the types of |
| 4280 | // the arguments. |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4281 | FunctionSet Functions; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4282 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 4283 | if (OverOp != OO_None) { |
| 4284 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 4285 | Functions); |
| 4286 | Expr *Args[2] = { lhs, rhs }; |
| 4287 | DeclarationName OpName |
| 4288 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4289 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4290 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4292 | // Build the (potentially-overloaded, potentially-dependent) |
| 4293 | // binary operation. |
| 4294 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4297 | // Build a built-in binary operation. |
| 4298 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4301 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| 4302 | unsigned OpcIn, |
| 4303 | ExprArg InputArg) { |
| 4304 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4305 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4306 | // FIXME: Input is modified below, but InputArg is not updated |
| 4307 | // appropriately. |
| 4308 | Expr *Input = (Expr *)InputArg.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4309 | QualType resultType; |
| 4310 | switch (Opc) { |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4311 | case UnaryOperator::PostInc: |
| 4312 | case UnaryOperator::PostDec: |
| 4313 | case UnaryOperator::OffsetOf: |
| 4314 | assert(false && "Invalid unary operator"); |
| 4315 | break; |
| 4316 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4317 | case UnaryOperator::PreInc: |
| 4318 | case UnaryOperator::PreDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4319 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4320 | Opc == UnaryOperator::PreInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4321 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4322 | case UnaryOperator::AddrOf: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4323 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4324 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4325 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4326 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4327 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4328 | break; |
| 4329 | case UnaryOperator::Plus: |
| 4330 | case UnaryOperator::Minus: |
| 4331 | UsualUnaryConversions(Input); |
| 4332 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4333 | if (resultType->isDependentType()) |
| 4334 | break; |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4335 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4336 | break; |
| 4337 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4338 | resultType->isEnumeralType()) |
| 4339 | break; |
| 4340 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4341 | Opc == UnaryOperator::Plus && |
| 4342 | resultType->isPointerType()) |
| 4343 | break; |
| 4344 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4345 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4346 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4347 | case UnaryOperator::Not: // bitwise complement |
| 4348 | UsualUnaryConversions(Input); |
| 4349 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4350 | if (resultType->isDependentType()) |
| 4351 | break; |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4352 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4353 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4354 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4355 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4356 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4357 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4358 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4359 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4360 | break; |
| 4361 | case UnaryOperator::LNot: // logical negation |
| 4362 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 4363 | DefaultFunctionArrayConversion(Input); |
| 4364 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4365 | if (resultType->isDependentType()) |
| 4366 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4367 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4368 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4369 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4370 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4371 | // In C++, it's bool. C++ 5.3.1p8 |
| 4372 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4373 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4374 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4375 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4376 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4377 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4378 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4379 | resultType = Input->getType(); |
| 4380 | break; |
| 4381 | } |
| 4382 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4383 | return ExprError(); |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4384 | |
| 4385 | InputArg.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4386 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4389 | // Unary Operators. 'Tok' is the token for the operator. |
| 4390 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 4391 | tok::TokenKind Op, ExprArg input) { |
| 4392 | Expr *Input = (Expr*)input.get(); |
| 4393 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 4394 | |
| 4395 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 4396 | // Find all of the overloaded operators visible from this |
| 4397 | // point. We perform both an operator-name lookup from the local |
| 4398 | // scope and an argument-dependent lookup based on the types of |
| 4399 | // the arguments. |
| 4400 | FunctionSet Functions; |
| 4401 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 4402 | if (OverOp != OO_None) { |
| 4403 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 4404 | Functions); |
| 4405 | DeclarationName OpName |
| 4406 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4407 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 4408 | } |
| 4409 | |
| 4410 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 4411 | } |
| 4412 | |
| 4413 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 4414 | } |
| 4415 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4416 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4417 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 4418 | SourceLocation LabLoc, |
| 4419 | IdentifierInfo *LabelII) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4420 | // Look up the record for this label identifier. |
Steve Naroff | ff9ecaf | 2009-03-13 16:03:38 +0000 | [diff] [blame] | 4421 | LabelStmt *&LabelDecl = CurBlock ? CurBlock->LabelMap[LabelII] : |
| 4422 | LabelMap[LabelII]; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4423 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4424 | // If we haven't seen this label yet, create a forward reference. It |
| 4425 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | b88d81c | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 4426 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4427 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4428 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4429 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4430 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4431 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4434 | Sema::OwningExprResult |
| 4435 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 4436 | SourceLocation RPLoc) { // "({..})" |
| 4437 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4438 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4439 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4440 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4441 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 4442 | if (isFileScope) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4443 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4446 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4447 | // example, it is not possible to goto into a stmt expression apparently. |
| 4448 | // More semantic analysis is needed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4449 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4450 | // FIXME: the last statement in the compount stmt has its value used. We |
| 4451 | // should not warn about it being unused. |
| 4452 | |
| 4453 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4454 | // as the type of the stmtexpr. |
| 4455 | QualType Ty = Context.VoidTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4457 | if (!Compound->body_empty()) { |
| 4458 | Stmt *LastStmt = Compound->body_back(); |
| 4459 | // If LastStmt is a label, skip down through into the body. |
| 4460 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4461 | LastStmt = Label->getSubStmt(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4462 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4463 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4464 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4465 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4466 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4467 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 4468 | // expressions are not lvalues. |
| 4469 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4470 | substmt.release(); |
| 4471 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4472 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4473 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4474 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4475 | SourceLocation BuiltinLoc, |
| 4476 | SourceLocation TypeLoc, |
| 4477 | TypeTy *argty, |
| 4478 | OffsetOfComponent *CompPtr, |
| 4479 | unsigned NumComponents, |
| 4480 | SourceLocation RPLoc) { |
| 4481 | // FIXME: This function leaks all expressions in the offset components on |
| 4482 | // error. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4483 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4484 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4485 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4486 | bool Dependent = ArgTy->isDependentType(); |
| 4487 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4488 | // We must have at least one component that refers to the type, and the first |
| 4489 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4490 | // a struct/union/class. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4491 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4492 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4493 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4494 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 4495 | // with an incomplete type would be illegal. |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 4496 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4497 | // Otherwise, create a null pointer as the base, and iteratively process |
| 4498 | // the offsetof designators. |
| 4499 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 4500 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4501 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4502 | ArgTy, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4503 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4504 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4505 | // GCC extension, diagnose them. |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4506 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 4507 | // a system header! |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4508 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4509 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4510 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4511 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4512 | if (!Dependent) { |
| 4513 | // FIXME: Dependent case loses a lot of information here. And probably |
| 4514 | // leaks like a sieve. |
| 4515 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4516 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4517 | if (OC.isBrackets) { |
| 4518 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 4519 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 4520 | if (!AT) { |
| 4521 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4522 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 4523 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4527 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4528 | // Promote the array so it looks more like a normal array subscript |
| 4529 | // expression. |
| 4530 | DefaultFunctionArrayConversion(Res); |
| 4531 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4532 | // C99 6.5.2.1p1 |
| 4533 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4534 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4535 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4536 | return ExprError(Diag(Idx->getLocStart(), |
| 4537 | diag::err_typecheck_subscript) |
| 4538 | << Idx->getSourceRange()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4539 | |
| 4540 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4541 | OC.LocEnd); |
| 4542 | continue; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4543 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4544 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4545 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4546 | if (!RC) { |
| 4547 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4548 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 4549 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4550 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4551 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4552 | // Get the decl corresponding to this. |
| 4553 | RecordDecl *RD = RC->getDecl(); |
| 4554 | FieldDecl *MemberDecl |
| 4555 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4556 | LookupMemberName) |
| 4557 | .getAsDecl()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4558 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4559 | if (!MemberDecl) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4560 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4561 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4562 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4563 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4564 | // FIXME: Verify that MemberDecl isn't a bitfield. |
| 4565 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4566 | // matter here. |
| 4567 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4568 | MemberDecl->getType().getNonReferenceType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4569 | } |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4570 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4571 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4572 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 4573 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4577 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 4578 | TypeTy *arg1,TypeTy *arg2, |
| 4579 | SourceLocation RPLoc) { |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4580 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4581 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4582 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4583 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4584 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4585 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 4586 | argT1, argT2, RPLoc)); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4589 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 4590 | ExprArg cond, |
| 4591 | ExprArg expr1, ExprArg expr2, |
| 4592 | SourceLocation RPLoc) { |
| 4593 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 4594 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 4595 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4596 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4597 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4598 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4599 | QualType resType; |
| 4600 | if (CondExpr->isValueDependent()) { |
| 4601 | resType = Context.DependentTy; |
| 4602 | } else { |
| 4603 | // The conditional expression is required to be a constant expression. |
| 4604 | llvm::APSInt condEval(32); |
| 4605 | SourceLocation ExpLoc; |
| 4606 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4607 | return ExprError(Diag(ExpLoc, |
| 4608 | diag::err_typecheck_choose_expr_requires_constant) |
| 4609 | << CondExpr->getSourceRange()); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4610 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4611 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4612 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 4613 | } |
| 4614 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4615 | cond.release(); expr1.release(); expr2.release(); |
| 4616 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 4617 | resType, RPLoc)); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4620 | //===----------------------------------------------------------------------===// |
| 4621 | // Clang Extensions. |
| 4622 | //===----------------------------------------------------------------------===// |
| 4623 | |
| 4624 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4625 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4626 | // Analyze block parameters. |
| 4627 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4628 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4629 | // Add BSI to CurBlock. |
| 4630 | BSI->PrevBlockInfo = CurBlock; |
| 4631 | CurBlock = BSI; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4632 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4633 | BSI->ReturnType = 0; |
| 4634 | BSI->TheScope = BlockScope; |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4635 | BSI->hasBlockDeclRefExprs = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4636 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4637 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4638 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4641 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| 4642 | assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); |
| 4643 | |
| 4644 | if (ParamInfo.getNumTypeObjects() == 0 |
| 4645 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
| 4646 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4647 | |
| 4648 | // The type is entirely optional as well, if none, use DependentTy. |
| 4649 | if (T.isNull()) |
| 4650 | T = Context.DependentTy; |
| 4651 | |
| 4652 | // The parameter list is optional, if there was none, assume (). |
| 4653 | if (!T->isFunctionType()) |
| 4654 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 4655 | |
| 4656 | CurBlock->hasPrototype = true; |
| 4657 | CurBlock->isVariadic = false; |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 4658 | |
| 4659 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
| 4660 | |
| 4661 | // Do not allow returning a objc interface by-value. |
| 4662 | if (RetTy->isObjCInterfaceType()) { |
| 4663 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 4664 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 4665 | return; |
| 4666 | } |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4667 | |
| 4668 | if (!RetTy->isDependentType()) |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 4669 | CurBlock->ReturnType = RetTy.getTypePtr(); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4670 | return; |
| 4671 | } |
| 4672 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4673 | // Analyze arguments to block. |
| 4674 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4675 | "Not a function declarator!"); |
| 4676 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4677 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4678 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4679 | CurBlock->isVariadic = true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4680 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4681 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4682 | // no arguments, not a function that takes a single void argument. |
| 4683 | if (FTI.hasPrototype && |
| 4684 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4685 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 4686 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4687 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4688 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4689 | } else if (FTI.hasPrototype) { |
| 4690 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4691 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4692 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4693 | } |
Steve Naroff | 494cb0f | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4694 | CurBlock->TheDecl->setParams(Context, &CurBlock->Params[0], |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 4695 | CurBlock->Params.size()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4696 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4697 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4698 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4699 | // If this has an identifier, add it to the scope stack. |
| 4700 | if ((*AI)->getIdentifier()) |
| 4701 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 4702 | |
| 4703 | // Analyze the return type. |
| 4704 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4705 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
| 4706 | |
| 4707 | // Do not allow returning a objc interface by-value. |
| 4708 | if (RetTy->isObjCInterfaceType()) { |
| 4709 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 4710 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 4711 | } else if (!RetTy->isDependentType()) |
| 4712 | CurBlock->ReturnType = RetTy.getTypePtr(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4716 | /// is invoked to pop the information about the block from the action impl. |
| 4717 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4718 | // Ensure that CurBlock is deleted. |
| 4719 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4720 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4721 | // Pop off CurBlock, handle nested blocks. |
| 4722 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4723 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4724 | // FIXME: Delete the ParmVarDecl objects as well??? |
Douglas Gregor | c1408ea | 2009-03-11 23:54:15 +0000 | [diff] [blame] | 4725 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4729 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4730 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 4731 | StmtArg body, Scope *CurScope) { |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4732 | // If blocks are disabled, emit an error. |
| 4733 | if (!LangOpts.Blocks) |
| 4734 | Diag(CaretLoc, diag::err_blocks_disable); |
| 4735 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4736 | // Ensure that CurBlock is deleted. |
| 4737 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4738 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4739 | PopDeclContext(); |
| 4740 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4741 | // Pop off CurBlock, handle nested blocks. |
| 4742 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4743 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4744 | QualType RetTy = Context.VoidTy; |
| 4745 | if (BSI->ReturnType) |
| 4746 | RetTy = QualType(BSI->ReturnType, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4747 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4748 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4749 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4750 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4751 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4752 | QualType BlockTy; |
| 4753 | if (!BSI->hasPrototype) |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4754 | BlockTy = Context.getFunctionNoProtoType(RetTy); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4755 | else |
| 4756 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4757 | BSI->isVariadic, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4758 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4759 | // FIXME: Check that return/parameter types are complete/non-abstract |
| 4760 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4761 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4762 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4763 | BSI->TheDecl->setBody(static_cast<CompoundStmt*>(body.release())); |
| 4764 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 4765 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4768 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4769 | ExprArg expr, TypeTy *type, |
| 4770 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4771 | QualType T = QualType::getFromOpaquePtr(type); |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 4772 | Expr *E = static_cast<Expr*>(expr.get()); |
| 4773 | Expr *OrigExpr = E; |
| 4774 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4775 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4776 | |
| 4777 | // Get the va_list type |
| 4778 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4779 | // Deal with implicit array decay; for example, on x86-64, |
| 4780 | // va_list is an array, but it's supposed to decay to |
| 4781 | // a pointer for va_arg. |
| 4782 | if (VaListType->isArrayType()) |
| 4783 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 8754e5b | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4784 | // Make sure the input expression also decays appropriately. |
| 4785 | UsualUnaryConversions(E); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4786 | |
Chris Lattner | cb9469d | 2009-04-06 17:07:34 +0000 | [diff] [blame] | 4787 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4788 | return ExprError(Diag(E->getLocStart(), |
| 4789 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 4790 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 89a72c5 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 4791 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4792 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4793 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4794 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4795 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4796 | expr.release(); |
| 4797 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 4798 | RPLoc)); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4801 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4802 | // The type of __null will be int or long, depending on the size of |
| 4803 | // pointers on the target. |
| 4804 | QualType Ty; |
| 4805 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4806 | Ty = Context.IntTy; |
| 4807 | else |
| 4808 | Ty = Context.LongTy; |
| 4809 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4810 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4813 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4814 | SourceLocation Loc, |
| 4815 | QualType DstType, QualType SrcType, |
| 4816 | Expr *SrcExpr, const char *Flavor) { |
| 4817 | // Decode the result (notice that AST's are still created for extensions). |
| 4818 | bool isInvalid = false; |
| 4819 | unsigned DiagKind; |
| 4820 | switch (ConvTy) { |
| 4821 | default: assert(0 && "Unknown conversion type"); |
| 4822 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4823 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4824 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4825 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4826 | case IntToPointer: |
| 4827 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4828 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4829 | case IncompatiblePointer: |
| 4830 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4831 | break; |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4832 | case IncompatiblePointerSign: |
| 4833 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 4834 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4835 | case FunctionVoidPointer: |
| 4836 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4837 | break; |
| 4838 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4839 | // If the qualifiers lost were because we were applying the |
| 4840 | // (deprecated) C++ conversion from a string literal to a char* |
| 4841 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4842 | // Ideally, this check would be performed in |
| 4843 | // CheckPointerTypesForAssignment. However, that would require a |
| 4844 | // bit of refactoring (so that the second argument is an |
| 4845 | // expression, rather than a type), which should be done as part |
| 4846 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4847 | // C++ semantics. |
| 4848 | if (getLangOptions().CPlusPlus && |
| 4849 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4850 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4851 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4852 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4853 | case IntToBlockPointer: |
| 4854 | DiagKind = diag::err_int_to_block_pointer; |
| 4855 | break; |
| 4856 | case IncompatibleBlockPointer: |
Steve Naroff | 82324d6 | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4857 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4858 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4859 | case IncompatibleObjCQualifiedId: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4860 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4861 | // it can give a more specific diagnostic. |
| 4862 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4863 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4864 | case IncompatibleVectors: |
| 4865 | DiagKind = diag::warn_incompatible_vectors; |
| 4866 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4867 | case Incompatible: |
| 4868 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4869 | isInvalid = true; |
| 4870 | break; |
| 4871 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4872 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4873 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4874 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4875 | return isInvalid; |
| 4876 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4877 | |
| 4878 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4879 | { |
| 4880 | Expr::EvalResult EvalResult; |
| 4881 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4882 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4883 | EvalResult.HasSideEffects) { |
| 4884 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4885 | |
| 4886 | if (EvalResult.Diag) { |
| 4887 | // We only show the note if it's not the usual "invalid subexpression" |
| 4888 | // or if it's actually in a subexpression. |
| 4889 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4890 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4891 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4892 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4893 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4894 | return true; |
| 4895 | } |
| 4896 | |
| 4897 | if (EvalResult.Diag) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4898 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4899 | E->getSourceRange(); |
| 4900 | |
| 4901 | // Print the reason it's not a constant. |
| 4902 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4903 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4904 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4905 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4906 | if (Result) |
| 4907 | *Result = EvalResult.Val.getInt(); |
| 4908 | return false; |
| 4909 | } |