Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 29 | /// \brief Determine whether the use of this declaration is valid, and |
| 30 | /// emit any corresponding diagnostics. |
| 31 | /// |
| 32 | /// This routine diagnoses various problems with referencing |
| 33 | /// declarations that can occur when using a declaration. For example, |
| 34 | /// it might warn if a deprecated or unavailable declaration is being |
| 35 | /// used, or produce an error (and return true) if a C++0x deleted |
| 36 | /// function is being used. |
| 37 | /// |
| 38 | /// \returns true if there was an error (this declaration cannot be |
| 39 | /// referenced), false otherwise. |
| 40 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 41 | // See if the decl is deprecated. |
| 42 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 43 | // Implementing deprecated stuff requires referencing deprecated |
| 44 | // stuff. Don't warn if we are implementing a deprecated |
| 45 | // construct. |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 46 | bool isSilenced = false; |
| 47 | |
| 48 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 49 | // If this reference happens *in* a deprecated function or method, don't |
| 50 | // warn. |
| 51 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
| 52 | |
| 53 | // If this is an Objective-C method implementation, check to see if the |
| 54 | // method was deprecated on the declaration, not the definition. |
| 55 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 56 | // The semantic decl context of a ObjCMethodDecl is the |
| 57 | // ObjCImplementationDecl. |
| 58 | if (ObjCImplementationDecl *Impl |
| 59 | = dyn_cast<ObjCImplementationDecl>(MD->getParent())) { |
| 60 | |
| 61 | MD = Impl->getClassInterface()->getMethod(MD->getSelector(), |
| 62 | MD->isInstanceMethod()); |
| 63 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (!isSilenced) |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 69 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 70 | } |
| 71 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 72 | // See if this is a deleted function. |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 73 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 74 | if (FD->isDeleted()) { |
| 75 | Diag(Loc, diag::err_deleted_function_use); |
| 76 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 77 | return true; |
| 78 | } |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 79 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 80 | |
| 81 | // See if the decl is unavailable |
| 82 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 83 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 84 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 85 | } |
| 86 | |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 87 | if (D->getDeclContext()->isFunctionOrMethod() && |
| 88 | !D->getDeclContext()->Encloses(CurContext)) { |
| 89 | // We've found the name of a function or variable that was |
| 90 | // declared with external linkage within another function (and, |
| 91 | // therefore, a scope where we wouldn't normally see the |
| 92 | // declaration). Once we've made sure that no previous declaration |
| 93 | // was properly made visible, produce a warning. |
| 94 | bool HasGlobalScopedDeclaration = false; |
| 95 | for (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); FD; |
| 96 | FD = FD->getPreviousDeclaration()) { |
| 97 | if (FD->getDeclContext()->isFileContext()) { |
| 98 | HasGlobalScopedDeclaration = true; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | // FIXME: do the same thing for variable declarations |
| 103 | |
| 104 | if (!HasGlobalScopedDeclaration) { |
| 105 | Diag(Loc, diag::warn_use_out_of_scope_declaration) << D; |
| 106 | Diag(D->getLocation(), diag::note_previous_declaration); |
| 107 | } |
| 108 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 109 | |
| 110 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 113 | //===----------------------------------------------------------------------===// |
| 114 | // Standard Promotions and Conversions |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 117 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 118 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 119 | QualType Ty = E->getType(); |
| 120 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 121 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 122 | if (Ty->isFunctionType()) |
| 123 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 124 | else if (Ty->isArrayType()) { |
| 125 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 126 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 127 | // type 'array of type' is converted to an expression that has type 'pointer |
| 128 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 129 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 130 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 131 | // |
| 132 | // C++ 4.2p1: |
| 133 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 134 | // T" can be converted to an rvalue of type "pointer to T". |
| 135 | // |
| 136 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 137 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 138 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 139 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 143 | /// operators (C99 6.3). The conversions of array and function types are |
| 144 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 145 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 146 | /// In these instances, this routine should *not* be called. |
| 147 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 148 | QualType Ty = Expr->getType(); |
| 149 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 150 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 151 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 152 | ImpCastExprToType(Expr, Context.IntTy); |
| 153 | else |
| 154 | DefaultFunctionArrayConversion(Expr); |
| 155 | |
| 156 | return Expr; |
| 157 | } |
| 158 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 159 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 160 | /// do not have a prototype. Arguments that have type float are promoted to |
| 161 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 162 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 163 | QualType Ty = Expr->getType(); |
| 164 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 165 | |
| 166 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 167 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 168 | if (BT->getKind() == BuiltinType::Float) |
| 169 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 170 | |
| 171 | UsualUnaryConversions(Expr); |
| 172 | } |
| 173 | |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 174 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 175 | // will warn if the resulting type is not a POD type. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 176 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 177 | DefaultArgumentPromotion(Expr); |
| 178 | |
| 179 | if (!Expr->getType()->isPODType()) { |
| 180 | Diag(Expr->getLocStart(), |
| 181 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 182 | Expr->getType() << CT; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 187 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 188 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 189 | /// routine returns the first non-arithmetic type found. The client is |
| 190 | /// responsible for emitting appropriate error diagnostics. |
| 191 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 192 | /// GCC. |
| 193 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 194 | bool isCompAssign) { |
| 195 | if (!isCompAssign) { |
| 196 | UsualUnaryConversions(lhsExpr); |
| 197 | UsualUnaryConversions(rhsExpr); |
| 198 | } |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 200 | // For conversion purposes, we ignore any qualifiers. |
| 201 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 202 | QualType lhs = |
| 203 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 204 | QualType rhs = |
| 205 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 206 | |
| 207 | // If both types are identical, no conversion is needed. |
| 208 | if (lhs == rhs) |
| 209 | return lhs; |
| 210 | |
| 211 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 212 | // The caller can deal with this (e.g. pointer + int). |
| 213 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 214 | return lhs; |
| 215 | |
| 216 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
| 217 | if (!isCompAssign) { |
| 218 | ImpCastExprToType(lhsExpr, destType); |
| 219 | ImpCastExprToType(rhsExpr, destType); |
| 220 | } |
| 221 | return destType; |
| 222 | } |
| 223 | |
| 224 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 225 | // Perform the usual unary conversions. We do this early so that |
| 226 | // integral promotions to "int" can allow us to exit early, in the |
| 227 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 228 | // qualifiers. For example, "const float" and "float" are |
| 229 | // equivalent. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 230 | if (lhs->isPromotableIntegerType()) |
| 231 | lhs = Context.IntTy; |
| 232 | else |
| 233 | lhs = lhs.getUnqualifiedType(); |
| 234 | if (rhs->isPromotableIntegerType()) |
| 235 | rhs = Context.IntTy; |
| 236 | else |
| 237 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 239 | // If both types are identical, no conversion is needed. |
| 240 | if (lhs == rhs) |
| 241 | return lhs; |
| 242 | |
| 243 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 244 | // The caller can deal with this (e.g. pointer + int). |
| 245 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 246 | return lhs; |
| 247 | |
| 248 | // At this point, we have two different arithmetic types. |
| 249 | |
| 250 | // Handle complex types first (C99 6.3.1.8p1). |
| 251 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 252 | // if we have an integer operand, the result is the complex type. |
| 253 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 254 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 255 | return lhs; |
| 256 | } |
| 257 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 258 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | return rhs; |
| 260 | } |
| 261 | // This handles complex/complex, complex/float, or float/complex. |
| 262 | // When both operands are complex, the shorter operand is converted to the |
| 263 | // type of the longer, and that is the type of the result. This corresponds |
| 264 | // to what is done when combining two real floating-point operands. |
| 265 | // The fun begins when size promotion occur across type domains. |
| 266 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 267 | // floating-point type, the less precise type is converted, within it's |
| 268 | // real or complex domain, to the precision of the other type. For example, |
| 269 | // when combining a "long double" with a "double _Complex", the |
| 270 | // "double _Complex" is promoted to "long double _Complex". |
| 271 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 272 | |
| 273 | if (result > 0) { // The left side is bigger, convert rhs. |
| 274 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 275 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 276 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 277 | } |
| 278 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 279 | // domains match. This is a requirement for our implementation, C99 |
| 280 | // does not require this promotion. |
| 281 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 282 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 283 | return rhs; |
| 284 | } else { // handle "_Complex double, double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 285 | return lhs; |
| 286 | } |
| 287 | } |
| 288 | return lhs; // The domain/size match exactly. |
| 289 | } |
| 290 | // Now handle "real" floating types (i.e. float, double, long double). |
| 291 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 292 | // 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] | 293 | if (rhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 294 | // convert rhs to the lhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 295 | return lhs; |
| 296 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 297 | if (rhs->isComplexIntegerType()) { |
| 298 | // convert rhs to the complex floating point type. |
| 299 | return Context.getComplexType(lhs); |
| 300 | } |
| 301 | if (lhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 302 | // convert lhs to the rhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 303 | return rhs; |
| 304 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 305 | if (lhs->isComplexIntegerType()) { |
| 306 | // convert lhs to the complex floating point type. |
| 307 | return Context.getComplexType(rhs); |
| 308 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 309 | // We have two real floating types, float/complex combos were handled above. |
| 310 | // Convert the smaller operand to the bigger result. |
| 311 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 312 | if (result > 0) // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 313 | return lhs; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 314 | assert(result < 0 && "illegal float comparison"); |
| 315 | return rhs; // convert the lhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 316 | } |
| 317 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 318 | // Handle GCC complex int extension. |
| 319 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 320 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 321 | |
| 322 | if (lhsComplexInt && rhsComplexInt) { |
| 323 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 324 | rhsComplexInt->getElementType()) >= 0) |
| 325 | return lhs; // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 326 | return rhs; |
| 327 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 328 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 329 | return lhs; |
| 330 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 331 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 332 | return rhs; |
| 333 | } |
| 334 | } |
| 335 | // Finally, we have two differing integer types. |
| 336 | // The rules for this case are in C99 6.3.1.8 |
| 337 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 338 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 339 | rhsSigned = rhs->isSignedIntegerType(); |
| 340 | QualType destType; |
| 341 | if (lhsSigned == rhsSigned) { |
| 342 | // Same signedness; use the higher-ranked type |
| 343 | destType = compare >= 0 ? lhs : rhs; |
| 344 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 345 | // The unsigned type has greater than or equal rank to the |
| 346 | // signed type, so use the unsigned type |
| 347 | destType = lhsSigned ? rhs : lhs; |
| 348 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 349 | // The two types are different widths; if we are here, that |
| 350 | // means the signed type is larger than the unsigned type, so |
| 351 | // use the signed type. |
| 352 | destType = lhsSigned ? lhs : rhs; |
| 353 | } else { |
| 354 | // The signed type is higher-ranked than the unsigned type, |
| 355 | // but isn't actually any bigger (like unsigned int and long |
| 356 | // on most 32-bit systems). Use the unsigned type corresponding |
| 357 | // to the signed type. |
| 358 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 359 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 360 | return destType; |
| 361 | } |
| 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Semantic Analysis for various Expression Types |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 368 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 369 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 370 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 371 | /// multiple tokens. However, the common case is that StringToks points to one |
| 372 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 373 | /// |
| 374 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 375 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 376 | assert(NumStringToks && "Must have at least one string!"); |
| 377 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 378 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 379 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 380 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 381 | |
| 382 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 383 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 384 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 385 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 386 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 387 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 388 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 389 | |
| 390 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 391 | if (getLangOptions().CPlusPlus) |
| 392 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 393 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 394 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 395 | // the nul terminator character as well as the string length for pascal |
| 396 | // strings. |
| 397 | StrTy = Context.getConstantArrayType(StrTy, |
| 398 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 399 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 401 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 402 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 403 | Literal.GetStringLength(), |
| 404 | Literal.AnyWide, StrTy, |
| 405 | &StringTokLocs[0], |
| 406 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 409 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 410 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 411 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 412 | /// for values inside the block or for globals). |
| 413 | /// |
| 414 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 415 | /// function references, etc which is suboptimal :) and breaks |
| 416 | /// things like "integer constant expression" tests. |
| 417 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 418 | ValueDecl *VD) { |
| 419 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 420 | // we wanted to. |
| 421 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 422 | return false; |
| 423 | |
| 424 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 425 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 426 | return false; |
| 427 | |
| 428 | // If this is a reference to an extern, static, or global variable, no need to |
| 429 | // snapshot it. |
| 430 | // FIXME: What about 'const' variables in C++? |
| 431 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 432 | return Var->hasLocalStorage(); |
| 433 | |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 439 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 440 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 441 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 442 | /// 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] | 443 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 444 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 445 | IdentifierInfo &II, |
| 446 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 447 | const CXXScopeSpec *SS, |
| 448 | bool isAddressOfOperand) { |
| 449 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 450 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 453 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 454 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 455 | /// nested-name-specifier. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 456 | DeclRefExpr * |
| 457 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 458 | bool TypeDependent, bool ValueDependent, |
| 459 | const CXXScopeSpec *SS) { |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 460 | if (SS && !SS->isEmpty()) |
| 461 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 462 | ValueDependent, |
| 463 | SS->getRange().getBegin()); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 464 | else |
| 465 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 468 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 469 | /// variable corresponding to the anonymous union or struct whose type |
| 470 | /// is Record. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 471 | static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 472 | assert(Record->isAnonymousStructOrUnion() && |
| 473 | "Record must be an anonymous struct or union!"); |
| 474 | |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 475 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 476 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 477 | // vector (which itself will be eliminated). DeclGroups might make |
| 478 | // this even better. |
| 479 | DeclContext *Ctx = Record->getDeclContext(); |
| 480 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 481 | DEnd = Ctx->decls_end(); |
| 482 | D != DEnd; ++D) { |
| 483 | if (*D == Record) { |
| 484 | // The object for the anonymous struct/union directly |
| 485 | // follows its type in the list of declarations. |
| 486 | ++D; |
| 487 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 488 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 489 | return *D; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | assert(false && "Missing object for anonymous record"); |
| 494 | return 0; |
| 495 | } |
| 496 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 497 | Sema::OwningExprResult |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 498 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 499 | FieldDecl *Field, |
| 500 | Expr *BaseObjectExpr, |
| 501 | SourceLocation OpLoc) { |
| 502 | assert(Field->getDeclContext()->isRecord() && |
| 503 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 504 | && "Field must be stored inside an anonymous struct or union"); |
| 505 | |
| 506 | // Construct the sequence of field member references |
| 507 | // we'll have to perform to get to the field in the anonymous |
| 508 | // union/struct. The list of members is built from the field |
| 509 | // outward, so traverse it backwards to go from an object in |
| 510 | // the current context to the field we found. |
| 511 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 512 | AnonFields.push_back(Field); |
| 513 | VarDecl *BaseObject = 0; |
| 514 | DeclContext *Ctx = Field->getDeclContext(); |
| 515 | do { |
| 516 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 517 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 518 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 519 | AnonFields.push_back(AnonField); |
| 520 | else { |
| 521 | BaseObject = cast<VarDecl>(AnonObject); |
| 522 | break; |
| 523 | } |
| 524 | Ctx = Ctx->getParent(); |
| 525 | } while (Ctx->isRecord() && |
| 526 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 527 | |
| 528 | // Build the expression that refers to the base object, from |
| 529 | // which we will build a sequence of member references to each |
| 530 | // of the anonymous union objects and, eventually, the field we |
| 531 | // found via name lookup. |
| 532 | bool BaseObjectIsPointer = false; |
| 533 | unsigned ExtraQuals = 0; |
| 534 | if (BaseObject) { |
| 535 | // BaseObject is an anonymous struct/union variable (and is, |
| 536 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 537 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 538 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 539 | SourceLocation()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 540 | ExtraQuals |
| 541 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 542 | } else if (BaseObjectExpr) { |
| 543 | // The caller provided the base object expression. Determine |
| 544 | // whether its a pointer and whether it adds any qualifiers to the |
| 545 | // anonymous struct/union fields we're looking into. |
| 546 | QualType ObjectType = BaseObjectExpr->getType(); |
| 547 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 548 | BaseObjectIsPointer = true; |
| 549 | ObjectType = ObjectPtr->getPointeeType(); |
| 550 | } |
| 551 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 552 | } else { |
| 553 | // We've found a member of an anonymous struct/union that is |
| 554 | // inside a non-anonymous struct/union, so in a well-formed |
| 555 | // program our base object expression is "this". |
| 556 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 557 | if (!MD->isStatic()) { |
| 558 | QualType AnonFieldType |
| 559 | = Context.getTagDeclType( |
| 560 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 561 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 562 | if ((Context.getCanonicalType(AnonFieldType) |
| 563 | == Context.getCanonicalType(ThisType)) || |
| 564 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 565 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 566 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 567 | MD->getThisType(Context)); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 568 | BaseObjectIsPointer = true; |
| 569 | } |
| 570 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 571 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 572 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 573 | } |
| 574 | ExtraQuals = MD->getTypeQualifiers(); |
| 575 | } |
| 576 | |
| 577 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 578 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 579 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // Build the implicit member references to the field of the |
| 583 | // anonymous struct/union. |
| 584 | Expr *Result = BaseObjectExpr; |
| 585 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 586 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 587 | FI != FIEnd; ++FI) { |
| 588 | QualType MemberType = (*FI)->getType(); |
| 589 | if (!(*FI)->isMutable()) { |
| 590 | unsigned combinedQualifiers |
| 591 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 592 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 593 | } |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 594 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 595 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 596 | BaseObjectIsPointer = false; |
| 597 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
| 598 | OpLoc = SourceLocation(); |
| 599 | } |
| 600 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 601 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 604 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 605 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 606 | /// performs lookup on that name and returns an expression that refers |
| 607 | /// to that name. This routine isn't directly called from the parser, |
| 608 | /// because the parser doesn't know about DeclarationName. Rather, |
| 609 | /// this routine is called by ActOnIdentifierExpr, |
| 610 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 611 | /// which form the DeclarationName from the corresponding syntactic |
| 612 | /// forms. |
| 613 | /// |
| 614 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 615 | /// function call context. LookupCtx is only used for a C++ |
| 616 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 617 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 618 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 619 | /// isAddressOfOperand means that this expression is the direct operand |
| 620 | /// of an address-of operator. This matters because this is the only |
| 621 | /// situation where a qualified name referencing a non-static member may |
| 622 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 623 | Sema::OwningExprResult |
| 624 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 625 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 626 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 627 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 628 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 629 | if (SS && SS->isInvalid()) |
| 630 | return ExprError(); |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 631 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 632 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 633 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 634 | NamedDecl *D = 0; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 635 | if (Lookup.isAmbiguous()) { |
| 636 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 637 | SS && SS->isSet() ? SS->getRange() |
| 638 | : SourceRange()); |
| 639 | return ExprError(); |
| 640 | } else |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 641 | D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 642 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 643 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 644 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 645 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 646 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 647 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 648 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 649 | // found a decl, but that decl is outside the current method (i.e. a global |
| 650 | // variable). In these two cases, we do a lookup for an ivar with this |
| 651 | // name, if the lookup suceeds, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 652 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 653 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 654 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 655 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 656 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 657 | return ExprError(); |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 658 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 659 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 660 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 661 | IdentifierInfo &II = Context.Idents.get("self"); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 662 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 663 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 664 | Loc, static_cast<Expr*>(SelfExpr.release()), |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 665 | true, true); |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 666 | Context.setFieldDecl(IFace, IV, MRef); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 667 | return Owned(MRef); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 670 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 671 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 672 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 673 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 674 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 675 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 676 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 678 | // Determine whether this name might be a candidate for |
| 679 | // argument-dependent lookup. |
| 680 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 681 | HasTrailingLParen; |
| 682 | |
| 683 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 684 | // We've seen something of the form |
| 685 | // |
| 686 | // identifier( |
| 687 | // |
| 688 | // and we did not find any entity by the name |
| 689 | // "identifier". However, this identifier is still subject to |
| 690 | // argument-dependent lookup, so keep track of the name. |
| 691 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 692 | Context.OverloadTy, |
| 693 | Loc)); |
| 694 | } |
| 695 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 696 | if (D == 0) { |
| 697 | // Otherwise, this could be an implicitly declared function reference (legal |
| 698 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 699 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 700 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 701 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 702 | else { |
| 703 | // If this name wasn't predeclared and if this is not a function call, |
| 704 | // diagnose the problem. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 705 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 706 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 707 | << Name << SS->getRange()); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 708 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 709 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 710 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 711 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 712 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 713 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 716 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 717 | // If this is an expression of the form &Class::member, don't build an |
| 718 | // implicit member ref, because we want a pointer to the member in general, |
| 719 | // not any specific instance's member. |
| 720 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 721 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 722 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 723 | QualType DType; |
| 724 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 725 | DType = FD->getType().getNonReferenceType(); |
| 726 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 727 | DType = Method->getType(); |
| 728 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 729 | DType = Context.OverloadTy; |
| 730 | } |
| 731 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 732 | if (!DType.isNull()) { |
| 733 | // The pointer is type- and value-dependent if it points into something |
| 734 | // dependent. |
| 735 | bool Dependent = false; |
| 736 | for (; DC; DC = DC->getParent()) { |
| 737 | // FIXME: could stop early at namespace scope. |
| 738 | if (DC->isRecord()) { |
| 739 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 740 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 741 | Dependent = true; |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | } |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 746 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 751 | // We may have found a field within an anonymous union or struct |
| 752 | // (C++ [class.union]). |
| 753 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 754 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 755 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 757 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 758 | if (!MD->isStatic()) { |
| 759 | // C++ [class.mfct.nonstatic]p2: |
| 760 | // [...] if name lookup (3.4.1) resolves the name in the |
| 761 | // id-expression to a nonstatic nontype member of class X or of |
| 762 | // a base class of X, the id-expression is transformed into a |
| 763 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 764 | // as the postfix-expression to the left of the '.' operator. |
| 765 | DeclContext *Ctx = 0; |
| 766 | QualType MemberType; |
| 767 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 768 | Ctx = FD->getDeclContext(); |
| 769 | MemberType = FD->getType(); |
| 770 | |
| 771 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 772 | MemberType = RefType->getPointeeType(); |
| 773 | else if (!FD->isMutable()) { |
| 774 | unsigned combinedQualifiers |
| 775 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 776 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 777 | } |
| 778 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 779 | if (!Method->isStatic()) { |
| 780 | Ctx = Method->getParent(); |
| 781 | MemberType = Method->getType(); |
| 782 | } |
| 783 | } else if (OverloadedFunctionDecl *Ovl |
| 784 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 785 | for (OverloadedFunctionDecl::function_iterator |
| 786 | Func = Ovl->function_begin(), |
| 787 | FuncEnd = Ovl->function_end(); |
| 788 | Func != FuncEnd; ++Func) { |
| 789 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 790 | if (!DMethod->isStatic()) { |
| 791 | Ctx = Ovl->getDeclContext(); |
| 792 | MemberType = Context.OverloadTy; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 797 | |
| 798 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 799 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 800 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 801 | if ((Context.getCanonicalType(CtxType) |
| 802 | == Context.getCanonicalType(ThisType)) || |
| 803 | IsDerivedFrom(ThisType, CtxType)) { |
| 804 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 805 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 806 | MD->getThisType(Context)); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 807 | return Owned(new (Context) MemberExpr(This, true, D, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 808 | SourceLocation(), MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 814 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 815 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 816 | if (MD->isStatic()) |
| 817 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 818 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 819 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 822 | // Any other ways we could have found the field in a well-formed |
| 823 | // program would have been turned into implicit member expressions |
| 824 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 825 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 826 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 827 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 828 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 829 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 830 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 831 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 832 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 833 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 834 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 835 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 836 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 837 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 838 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 839 | false, false, SS)); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 840 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 841 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 842 | false, false, SS)); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 843 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 845 | // Check whether this declaration can be used. Note that we suppress |
| 846 | // this check when we're going to perform argument-dependent lookup |
| 847 | // on this function name, because this might not be the function |
| 848 | // that overload resolution actually selects. |
| 849 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 850 | return ExprError(); |
| 851 | |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 852 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 853 | // Warn about constructs like: |
| 854 | // if (void *X = foo()) { ... } else { X }. |
| 855 | // In the else block, the pointer is always false. |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 856 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 857 | Scope *CheckS = S; |
| 858 | while (CheckS) { |
| 859 | if (CheckS->isWithinElse() && |
| 860 | CheckS->getControlParent()->isDeclScope(Var)) { |
| 861 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 862 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 863 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 864 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 865 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 866 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | |
| 870 | // Move up one more control parent to check again. |
| 871 | CheckS = CheckS->getControlParent(); |
| 872 | if (CheckS) |
| 873 | CheckS = CheckS->getParent(); |
| 874 | } |
| 875 | } |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 876 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) { |
| 877 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 878 | // C99 DR 316 says that, if a function type comes from a |
| 879 | // function definition (without a prototype), that type is only |
| 880 | // used for checking compatibility. Therefore, when referencing |
| 881 | // the function, we pretend that we don't have the full function |
| 882 | // type. |
| 883 | QualType T = Func->getType(); |
| 884 | QualType NoProtoType = T; |
| 885 | if (const FunctionTypeProto *Proto = T->getAsFunctionTypeProto()) |
| 886 | NoProtoType = Context.getFunctionTypeNoProto(Proto->getResultType()); |
| 887 | return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS)); |
| 888 | } |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 889 | } |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 890 | |
| 891 | // Only create DeclRefExpr's for valid Decl's. |
| 892 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 893 | return ExprError(); |
| 894 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 895 | // If the identifier reference is inside a block, and it refers to a value |
| 896 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 897 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 898 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 899 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 900 | // We do not do this for things like enum constants, global variables, etc, |
| 901 | // as they do not get snapshotted. |
| 902 | // |
| 903 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 904 | // Blocks that have these can't be constant. |
| 905 | CurBlock->hasBlockDeclRefExprs = true; |
| 906 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 907 | // The BlocksAttr indicates the variable is bound by-reference. |
| 908 | if (VD->getAttr<BlocksAttr>()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 909 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 910 | VD->getType().getNonReferenceType(), Loc, true)); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 911 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 912 | // Variable will be bound by-copy, make it const within the closure. |
| 913 | VD->getType().addConst(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 914 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 915 | VD->getType().getNonReferenceType(), Loc, false)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 916 | } |
| 917 | // If this reference is not in a block or if the referenced variable is |
| 918 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 920 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 921 | bool ValueDependent = false; |
| 922 | if (getLangOptions().CPlusPlus) { |
| 923 | // C++ [temp.dep.expr]p3: |
| 924 | // An id-expression is type-dependent if it contains: |
| 925 | // - an identifier that was declared with a dependent type, |
| 926 | if (VD->getType()->isDependentType()) |
| 927 | TypeDependent = true; |
| 928 | // - FIXME: a template-id that is dependent, |
| 929 | // - a conversion-function-id that specifies a dependent type, |
| 930 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 931 | Name.getCXXNameType()->isDependentType()) |
| 932 | TypeDependent = true; |
| 933 | // - a nested-name-specifier that contains a class-name that |
| 934 | // names a dependent type. |
| 935 | else if (SS && !SS->isEmpty()) { |
| 936 | for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 937 | DC; DC = DC->getParent()) { |
| 938 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 939 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 940 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 941 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 942 | TypeDependent = true; |
| 943 | break; |
| 944 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 949 | // C++ [temp.dep.constexpr]p2: |
| 950 | // |
| 951 | // An identifier is value-dependent if it is: |
| 952 | // - a name declared with a dependent type, |
| 953 | if (TypeDependent) |
| 954 | ValueDependent = true; |
| 955 | // - the name of a non-type template parameter, |
| 956 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 957 | ValueDependent = true; |
| 958 | // - a constant with integral or enumeration type and is |
| 959 | // initialized with an expression that is value-dependent |
| 960 | // (FIXME!). |
| 961 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 962 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 963 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 964 | TypeDependent, ValueDependent, SS)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 967 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 968 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 969 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 971 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 972 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 973 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 974 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 975 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 976 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 978 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 979 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 980 | unsigned Length; |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 981 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 982 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 983 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 984 | Length = MD->getSynthesizedMethodSize(); |
| 985 | else { |
| 986 | Diag(Loc, diag::ext_predef_outside_function); |
| 987 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 988 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 989 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 990 | |
| 991 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 992 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 993 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 994 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 995 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 998 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 999 | llvm::SmallString<16> CharBuffer; |
| 1000 | CharBuffer.resize(Tok.getLength()); |
| 1001 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1002 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1004 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1005 | Tok.getLocation(), PP); |
| 1006 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1007 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1008 | |
| 1009 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1010 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1011 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1012 | Literal.isWide(), |
| 1013 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1016 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1017 | // 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] | 1018 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1019 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1020 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1021 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1022 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1023 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1024 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1026 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1027 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1028 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1029 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1031 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1032 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1034 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1035 | Tok.getLocation(), PP); |
| 1036 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1037 | return ExprError(); |
| 1038 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1039 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1041 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1042 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1043 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1044 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1045 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1046 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1047 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1048 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1049 | |
| 1050 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1051 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1052 | // isExact will be set by GetFloatValue(). |
| 1053 | bool isExact = false; |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1054 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1055 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1056 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1057 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1058 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1059 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1060 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1061 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1062 | // long long is a C99 feature. |
| 1063 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1064 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1065 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1066 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1067 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1068 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1070 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1071 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1072 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1073 | Ty = Context.UnsignedLongLongTy; |
| 1074 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1075 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1076 | } else { |
| 1077 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1078 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1080 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1081 | // be an unsigned int. |
| 1082 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1083 | |
| 1084 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1085 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1086 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1087 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1088 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1090 | // Does it fit in a unsigned int? |
| 1091 | if (ResultVal.isIntN(IntSize)) { |
| 1092 | // Does it fit in a signed int? |
| 1093 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1094 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1095 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1096 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1097 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1099 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1101 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1102 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1103 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1105 | // Does it fit in a unsigned long? |
| 1106 | if (ResultVal.isIntN(LongSize)) { |
| 1107 | // Does it fit in a signed long? |
| 1108 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1109 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1110 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1111 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1112 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1113 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1116 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1117 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1118 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1119 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | // Does it fit in a unsigned long long? |
| 1121 | if (ResultVal.isIntN(LongLongSize)) { |
| 1122 | // Does it fit in a signed long long? |
| 1123 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1124 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1125 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1126 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1127 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1131 | // If we still couldn't decide a type, we probably have something that |
| 1132 | // 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] | 1133 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1134 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1135 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1136 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1137 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1139 | if (ResultVal.getBitWidth() != Width) |
| 1140 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1141 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1142 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1143 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1145 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1146 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1147 | Res = new (Context) ImaginaryLiteral(Res, |
| 1148 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1149 | |
| 1150 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1153 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1154 | SourceLocation R, ExprArg Val) { |
| 1155 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1156 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1157 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1161 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1162 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1163 | SourceLocation OpLoc, |
| 1164 | const SourceRange &ExprRange, |
| 1165 | bool isSizeof) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1166 | if (exprType->isDependentType()) |
| 1167 | return false; |
| 1168 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1169 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1170 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1171 | // alignof(function) is allowed. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1172 | if (isSizeof) |
| 1173 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1178 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1179 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1183 | return DiagnoseIncompleteType(OpLoc, exprType, |
| 1184 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1185 | diag::err_alignof_incomplete_type, |
| 1186 | ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1189 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1190 | const SourceRange &ExprRange) { |
| 1191 | E = E->IgnoreParens(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1192 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1193 | // alignof decl is always ok. |
| 1194 | if (isa<DeclRefExpr>(E)) |
| 1195 | return false; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1196 | |
| 1197 | // Cannot know anything else if the expression is dependent. |
| 1198 | if (E->isTypeDependent()) |
| 1199 | return false; |
| 1200 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1201 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1202 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1203 | if (FD->isBitField()) { |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1204 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1205 | return true; |
| 1206 | } |
| 1207 | // Other fields are ok. |
| 1208 | return false; |
| 1209 | } |
| 1210 | } |
| 1211 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1212 | } |
| 1213 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1214 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1215 | /// the same for @c alignof and @c __alignof |
| 1216 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1217 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1218 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1219 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1220 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1221 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1222 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1223 | QualType ArgTy; |
| 1224 | SourceRange Range; |
| 1225 | if (isType) { |
| 1226 | ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1227 | Range = ArgRange; |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Verify that the operand is valid. |
| 1230 | if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) |
| 1231 | return ExprError(); |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1232 | } else { |
| 1233 | // Get the end location. |
| 1234 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1235 | Range = ArgEx->getSourceRange(); |
| 1236 | ArgTy = ArgEx->getType(); |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Verify that the operand is valid. |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1239 | bool isInvalid; |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1240 | if (!isSizeof) { |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1241 | isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range); |
Chris Lattner | 364a42d | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1242 | } else if (ArgEx->isBitField()) { // C99 6.5.3.4p1. |
| 1243 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1244 | isInvalid = true; |
| 1245 | } else { |
| 1246 | isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true); |
| 1247 | } |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (isInvalid) { |
Chris Lattner | a78909b | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1250 | DeleteExpr(ArgEx); |
| 1251 | return ExprError(); |
| 1252 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1255 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1256 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeof, isType, TyOrEx, |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1257 | Context.getSizeType(), OpLoc, |
| 1258 | Range.getEnd())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1261 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1262 | if (V->isTypeDependent()) |
| 1263 | return Context.DependentTy; |
| 1264 | |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1265 | DefaultFunctionArrayConversion(V); |
| 1266 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1267 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1268 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1269 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1270 | |
| 1271 | // Otherwise they pass through real integer and floating point types here. |
| 1272 | if (V->getType()->isArithmeticType()) |
| 1273 | return V->getType(); |
| 1274 | |
| 1275 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1276 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1277 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1278 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1282 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1283 | Action::OwningExprResult |
| 1284 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1285 | tok::TokenKind Kind, ExprArg Input) { |
| 1286 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1288 | UnaryOperator::Opcode Opc; |
| 1289 | switch (Kind) { |
| 1290 | default: assert(0 && "Unknown unary op!"); |
| 1291 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1292 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1293 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1294 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1295 | if (getLangOptions().CPlusPlus && |
| 1296 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1297 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1298 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1299 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1300 | |
| 1301 | // C++ [over.inc]p1: |
| 1302 | // |
| 1303 | // [...] If the function is a member function with one |
| 1304 | // parameter (which shall be of type int) or a non-member |
| 1305 | // function with two parameters (the second of which shall be |
| 1306 | // of type int), it defines the postfix increment operator ++ |
| 1307 | // for objects of that type. When the postfix increment is |
| 1308 | // called as a result of using the ++ operator, the int |
| 1309 | // argument will have value zero. |
| 1310 | Expr *Args[2] = { |
| 1311 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1312 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1313 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1314 | }; |
| 1315 | |
| 1316 | // Build the candidate set for overloading |
| 1317 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1318 | if (AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet)) |
| 1319 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1320 | |
| 1321 | // Perform overload resolution. |
| 1322 | OverloadCandidateSet::iterator Best; |
| 1323 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1324 | case OR_Success: { |
| 1325 | // We found a built-in operator or an overloaded operator. |
| 1326 | FunctionDecl *FnDecl = Best->Function; |
| 1327 | |
| 1328 | if (FnDecl) { |
| 1329 | // We matched an overloaded operator. Build a call to that |
| 1330 | // operator. |
| 1331 | |
| 1332 | // Convert the arguments. |
| 1333 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1334 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1335 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1336 | } else { |
| 1337 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1338 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1339 | FnDecl->getParamDecl(0)->getType(), |
| 1340 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1341 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1345 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1346 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1347 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1349 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1350 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 6d8e573 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1351 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1352 | UsualUnaryConversions(FnExpr); |
| 1353 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1354 | Input.release(); |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1355 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
| 1356 | ResultTy, OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1357 | } else { |
| 1358 | // We matched a built-in operator. Convert the arguments, then |
| 1359 | // break out so that we will build the appropriate built-in |
| 1360 | // operator node. |
| 1361 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1362 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1363 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1364 | |
| 1365 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | case OR_No_Viable_Function: |
| 1370 | // No viable function; fall through to handling this as a |
| 1371 | // built-in operator, which will produce an error message for us. |
| 1372 | break; |
| 1373 | |
| 1374 | case OR_Ambiguous: |
| 1375 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1376 | << UnaryOperator::getOpcodeStr(Opc) |
| 1377 | << Arg->getSourceRange(); |
| 1378 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1379 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1380 | |
| 1381 | case OR_Deleted: |
| 1382 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1383 | << Best->Function->isDeleted() |
| 1384 | << UnaryOperator::getOpcodeStr(Opc) |
| 1385 | << Arg->getSourceRange(); |
| 1386 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1387 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | // Either we found no viable overloaded operator or we matched a |
| 1391 | // built-in operator. In either case, fall through to trying to |
| 1392 | // build a built-in operation. |
| 1393 | } |
| 1394 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1395 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1396 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1397 | if (result.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1398 | return ExprError(); |
| 1399 | Input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1400 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1403 | Action::OwningExprResult |
| 1404 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1405 | ExprArg Idx, SourceLocation RLoc) { |
| 1406 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1407 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1409 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1410 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1411 | LHSExp->getType()->isEnumeralType() || |
| 1412 | RHSExp->getType()->isRecordType() || |
| 1413 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1414 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1415 | // to the candidate set. |
| 1416 | OverloadCandidateSet CandidateSet; |
| 1417 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1418 | if (AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1419 | SourceRange(LLoc, RLoc))) |
| 1420 | return ExprError(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1422 | // Perform overload resolution. |
| 1423 | OverloadCandidateSet::iterator Best; |
| 1424 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1425 | case OR_Success: { |
| 1426 | // We found a built-in operator or an overloaded operator. |
| 1427 | FunctionDecl *FnDecl = Best->Function; |
| 1428 | |
| 1429 | if (FnDecl) { |
| 1430 | // We matched an overloaded operator. Build a call to that |
| 1431 | // operator. |
| 1432 | |
| 1433 | // Convert the arguments. |
| 1434 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1435 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1436 | PerformCopyInitialization(RHSExp, |
| 1437 | FnDecl->getParamDecl(0)->getType(), |
| 1438 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1439 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1440 | } else { |
| 1441 | // Convert the arguments. |
| 1442 | if (PerformCopyInitialization(LHSExp, |
| 1443 | FnDecl->getParamDecl(0)->getType(), |
| 1444 | "passing") || |
| 1445 | PerformCopyInitialization(RHSExp, |
| 1446 | FnDecl->getParamDecl(1)->getType(), |
| 1447 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1448 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1452 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1453 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1454 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1456 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1457 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1458 | SourceLocation()); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1459 | UsualUnaryConversions(FnExpr); |
| 1460 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1461 | Base.release(); |
| 1462 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1463 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1464 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | // We matched a built-in operator. Convert the arguments, then |
| 1467 | // break out so that we will build the appropriate built-in |
| 1468 | // operator node. |
| 1469 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1470 | "passing") || |
| 1471 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1472 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1473 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1474 | |
| 1475 | break; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | case OR_No_Viable_Function: |
| 1480 | // No viable function; fall through to handling this as a |
| 1481 | // built-in operator, which will produce an error message for us. |
| 1482 | break; |
| 1483 | |
| 1484 | case OR_Ambiguous: |
| 1485 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1486 | << "[]" |
| 1487 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1488 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1489 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1490 | |
| 1491 | case OR_Deleted: |
| 1492 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1493 | << Best->Function->isDeleted() |
| 1494 | << "[]" |
| 1495 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1496 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1497 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Either we found no viable overloaded operator or we matched a |
| 1501 | // built-in operator. In either case, fall through to trying to |
| 1502 | // build a built-in operation. |
| 1503 | } |
| 1504 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1505 | // Perform default conversions. |
| 1506 | DefaultFunctionArrayConversion(LHSExp); |
| 1507 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1508 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1509 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1510 | |
| 1511 | // 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] | 1512 | // 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] | 1513 | // 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] | 1514 | // and index from the expression types. |
| 1515 | Expr *BaseExpr, *IndexExpr; |
| 1516 | QualType ResultType; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1517 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1518 | BaseExpr = LHSExp; |
| 1519 | IndexExpr = RHSExp; |
| 1520 | ResultType = Context.DependentTy; |
| 1521 | } else if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1522 | BaseExpr = LHSExp; |
| 1523 | IndexExpr = RHSExp; |
| 1524 | // FIXME: need to deal with const... |
| 1525 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1526 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1527 | // Handle the uncommon case of "123[Ptr]". |
| 1528 | BaseExpr = RHSExp; |
| 1529 | IndexExpr = LHSExp; |
| 1530 | // FIXME: need to deal with const... |
| 1531 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1532 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1533 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1534 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1536 | // FIXME: need to deal with const... |
| 1537 | ResultType = VTy->getElementType(); |
| 1538 | } else { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1539 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1540 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1541 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1542 | // C99 6.5.2.1p1 |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1543 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1544 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1545 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1546 | |
| 1547 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 1548 | // the following check catches trying to index a pointer to a function (e.g. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1549 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 1550 | // objects in C99. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 1551 | if (!ResultType->isObjectType() && !ResultType->isDependentType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1552 | return ExprError(Diag(BaseExpr->getLocStart(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1553 | diag::err_typecheck_subscript_not_object) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1554 | << BaseExpr->getType() << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1555 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1556 | Base.release(); |
| 1557 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1558 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1559 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1562 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1563 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1564 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1565 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1566 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1567 | // The vector accessor can't exceed the number of elements. |
| 1568 | const char *compStr = CompName.getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1569 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1570 | // 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] | 1571 | // special names that indicate a subset of exactly half the elements are |
| 1572 | // to be selected. |
| 1573 | bool HalvingSwizzle = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1574 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1575 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1576 | // indicating that it is a string of hex values to be used as vector indices. |
| 1577 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Check that we've found one of the special components, or that the component |
| 1580 | // names must come from the same set. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1581 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1582 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1583 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1584 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1585 | do |
| 1586 | compStr++; |
| 1587 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1588 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1589 | do |
| 1590 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1591 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1592 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1593 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1594 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1595 | // We didn't get to the end of the string. This means the component names |
| 1596 | // 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] | 1597 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1598 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1599 | return QualType(); |
| 1600 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1601 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1602 | // Ensure no component accessor exceeds the width of the vector type it |
| 1603 | // operates on. |
| 1604 | if (!HalvingSwizzle) { |
| 1605 | compStr = CompName.getName(); |
| 1606 | |
| 1607 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1608 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1609 | |
| 1610 | while (*compStr) { |
| 1611 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1612 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1613 | << baseType << SourceRange(CompLoc); |
| 1614 | return QualType(); |
| 1615 | } |
| 1616 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1617 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1618 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1619 | // If this is a halving swizzle, verify that the base type has an even |
| 1620 | // number of elements. |
| 1621 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1622 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1623 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1624 | return QualType(); |
| 1625 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1626 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1627 | // 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] | 1628 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1629 | // 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] | 1630 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1631 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1632 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1633 | : CompName.getLength(); |
| 1634 | if (HexSwizzle) |
| 1635 | CompSize--; |
| 1636 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1637 | if (CompSize == 1) |
| 1638 | return vecType->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1639 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1640 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1641 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1642 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1643 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1644 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1645 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1646 | } |
| 1647 | 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] | 1648 | } |
| 1649 | |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1650 | |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1651 | /// constructSetterName - Return the setter name for the given |
| 1652 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 1653 | /// has been capitalized. |
| 1654 | // FIXME: Merge with same routine in Parser. But where should this |
| 1655 | // live? |
| 1656 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 1657 | const IdentifierInfo *Name) { |
| 1658 | llvm::SmallString<100> SelectorName; |
| 1659 | SelectorName = "set"; |
| 1660 | SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); |
| 1661 | SelectorName[3] = toupper(SelectorName[3]); |
| 1662 | return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); |
| 1663 | } |
| 1664 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1665 | Action::OwningExprResult |
| 1666 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1667 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 1668 | IdentifierInfo &Member) { |
| 1669 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1670 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1671 | |
| 1672 | // Perform default conversions. |
| 1673 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1674 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1675 | QualType BaseType = BaseExpr->getType(); |
| 1676 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1678 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1679 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1680 | if (OpKind == tok::arrow) { |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1681 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1682 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1683 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1684 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1685 | MemberLoc, Member)); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1686 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1687 | return ExprError(Diag(MemberLoc, |
| 1688 | diag::err_typecheck_member_reference_arrow) |
| 1689 | << BaseType << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1690 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1692 | // Handle field access to simple records. This also handles access to fields |
| 1693 | // of the ObjC 'id' struct. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1694 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1695 | RecordDecl *RDecl = RTy->getDecl(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1696 | if (DiagnoseIncompleteType(OpLoc, BaseType, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1697 | diag::err_typecheck_incomplete_tag, |
| 1698 | BaseExpr->getSourceRange())) |
| 1699 | return ExprError(); |
| 1700 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1701 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1702 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1703 | // than this. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1704 | LookupResult Result |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1705 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1706 | LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1708 | NamedDecl *MemberDecl = 0; |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1709 | if (!Result) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1710 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1711 | << &Member << BaseExpr->getSourceRange()); |
| 1712 | else if (Result.isAmbiguous()) { |
| 1713 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1714 | MemberLoc, BaseExpr->getSourceRange()); |
| 1715 | return ExprError(); |
| 1716 | } else |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1717 | MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1718 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1719 | // If the decl being referenced had an error, return an error for this |
| 1720 | // sub-expr without emitting another error, in order to avoid cascading |
| 1721 | // error cases. |
| 1722 | if (MemberDecl->isInvalidDecl()) |
| 1723 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1725 | // Check the use of this field |
| 1726 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 1727 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1728 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1729 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1730 | // We may have found a field within an anonymous union or struct |
| 1731 | // (C++ [class.union]). |
| 1732 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1733 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1734 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1736 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1737 | // FIXME: Handle address space modifiers |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1738 | QualType MemberType = FD->getType(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1739 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1740 | MemberType = Ref->getPointeeType(); |
| 1741 | else { |
| 1742 | unsigned combinedQualifiers = |
| 1743 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1744 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1745 | combinedQualifiers &= ~QualType::Const; |
| 1746 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1747 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1748 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1749 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1750 | MemberLoc, MemberType)); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1751 | } else if (CXXClassVarDecl *Var = dyn_cast<CXXClassVarDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1752 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1753 | Var, MemberLoc, |
| 1754 | Var->getType().getNonReferenceType())); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1755 | else if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1756 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1757 | MemberFn, MemberLoc, MemberFn->getType())); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1758 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1759 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1760 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1761 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1762 | else if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1763 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1764 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1765 | else if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1766 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1767 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1769 | // We found a declaration kind that we didn't expect. This is a |
| 1770 | // generic error message that tells the user that she can't refer |
| 1771 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1772 | return ExprError(Diag(MemberLoc, |
| 1773 | diag::err_typecheck_member_reference_unknown) |
| 1774 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1775 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1777 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1778 | // (*Obj).ivar. |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1779 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1780 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member)) { |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1781 | // If the decl being referenced had an error, return an error for this |
| 1782 | // sub-expr without emitting another error, in order to avoid cascading |
| 1783 | // error cases. |
| 1784 | if (IV->isInvalidDecl()) |
| 1785 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Check whether we can reference this field. |
| 1788 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1789 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1790 | |
| 1791 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1792 | MemberLoc, BaseExpr, |
Fariborz Jahanian | ea94484 | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1793 | OpKind == tok::arrow); |
| 1794 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1795 | return Owned(MRef); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1796 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1797 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1798 | << IFTy->getDecl()->getDeclName() << &Member |
| 1799 | << BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1800 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1801 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1802 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1803 | // pointer to a (potentially qualified) interface type. |
| 1804 | const PointerType *PTy; |
| 1805 | const ObjCInterfaceType *IFTy; |
| 1806 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1807 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1808 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | dd85128 | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1809 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1810 | // Search for a declared property first. |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1811 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1812 | // Check whether we can reference this property. |
| 1813 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1814 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1815 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1816 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1817 | MemberLoc, BaseExpr)); |
| 1818 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1820 | // Check protocols on qualified interfaces. |
Chris Lattner | d5f8179 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1821 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1822 | E = IFTy->qual_end(); I != E; ++I) |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1823 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1824 | // Check whether we can reference this property. |
| 1825 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1826 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1827 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1828 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1829 | MemberLoc, BaseExpr)); |
| 1830 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1831 | |
| 1832 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1833 | // selector is implemented. |
| 1834 | |
| 1835 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1836 | // shared with the code in ActOnInstanceMessage. |
| 1837 | |
| 1838 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1839 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1840 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1841 | // If this reference is in an @implementation, check for 'private' methods. |
| 1842 | if (!Getter) |
| 1843 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1844 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1845 | if (ObjCImplementationDecl *ImpDecl = |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1846 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1847 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 1848 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1849 | // Look through local category implementations associated with the class. |
| 1850 | if (!Getter) { |
| 1851 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1852 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1853 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1854 | } |
| 1855 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1856 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1857 | // Check if we can reference this property. |
| 1858 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1859 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1860 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1861 | // If we found a getter then this may be a valid dot-reference, we |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1862 | // will look for the matching setter, in case it is needed. |
| 1863 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 1864 | &Member); |
| 1865 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 1866 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 1867 | if (!Setter) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1868 | // If this reference is in an @implementation, also check for 'private' |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1869 | // methods. |
| 1870 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1871 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1872 | if (ObjCImplementationDecl *ImpDecl = |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1873 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1874 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 1875 | } |
| 1876 | // Look through local category implementations associated with the class. |
| 1877 | if (!Setter) { |
| 1878 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 1879 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1880 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
| 1881 | } |
| 1882 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1884 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1885 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1886 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1887 | // FIXME: we must check that the setter has property type. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1888 | return Owned(new (Context) ObjCKVCRefExpr(Getter, Getter->getResultType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1889 | Setter, MemberLoc, BaseExpr)); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1890 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1891 | |
| 1892 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1893 | << &Member << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1894 | } |
Steve Naroff | d1d4440 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1895 | // Handle properties on qualified "id" protocols. |
| 1896 | const ObjCQualifiedIdType *QIdTy; |
| 1897 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 1898 | // Check protocols on qualified interfaces. |
| 1899 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1900 | E = QIdTy->qual_end(); I != E; ++I) { |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1901 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1902 | // Check the use of this declaration |
| 1903 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1904 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1905 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1906 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 1907 | MemberLoc, BaseExpr)); |
| 1908 | } |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1909 | // Also must look for a getter name which uses property syntax. |
| 1910 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1911 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1912 | // Check the use of this method. |
| 1913 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 1914 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1915 | |
| 1916 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1917 | OMD->getResultType(), OMD, OpLoc, MemberLoc, NULL, 0)); |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1918 | } |
| 1919 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1920 | |
| 1921 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1922 | << &Member << BaseType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1923 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1924 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 1925 | if (BaseType->isExtVectorType()) { |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1926 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1927 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1928 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1929 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1930 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1931 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1932 | |
| 1933 | return ExprError(Diag(MemberLoc, |
| 1934 | diag::err_typecheck_member_reference_struct_union) |
| 1935 | << BaseType << BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1938 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 1939 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 1940 | /// function prototype Proto. Call is the call expression itself, and |
| 1941 | /// Fn is the function expression. For a C++ member function, this |
| 1942 | /// routine does not attempt to convert the object argument. Returns |
| 1943 | /// true if the call is ill-formed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1944 | bool |
| 1945 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1946 | FunctionDecl *FDecl, |
| 1947 | const FunctionTypeProto *Proto, |
| 1948 | Expr **Args, unsigned NumArgs, |
| 1949 | SourceLocation RParenLoc) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1950 | // 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] | 1951 | // assignment, to the types of the corresponding parameter, ... |
| 1952 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1953 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1954 | bool Invalid = false; |
| 1955 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1956 | // If too few arguments are available (and we don't have default |
| 1957 | // arguments for the remaining parameters), don't make the call. |
| 1958 | if (NumArgs < NumArgsInProto) { |
| 1959 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 1960 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 1961 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 1962 | // Use default arguments for missing arguments |
| 1963 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1964 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | // If too many are passed and not variadic, error on the extras and drop |
| 1968 | // them. |
| 1969 | if (NumArgs > NumArgsInProto) { |
| 1970 | if (!Proto->isVariadic()) { |
| 1971 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 1972 | diag::err_typecheck_call_too_many_args) |
| 1973 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 1974 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 1975 | Args[NumArgs-1]->getLocEnd()); |
| 1976 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1977 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1978 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1979 | } |
| 1980 | NumArgsToCheck = NumArgsInProto; |
| 1981 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1983 | // Continue to check argument types (even if we have too few/many args). |
| 1984 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 1985 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1987 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1988 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1989 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1990 | |
| 1991 | // Pass the argument. |
| 1992 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 1993 | return true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1994 | } else |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1995 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1996 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1997 | QualType ArgType = Arg->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1999 | Call->setArg(i, Arg); |
| 2000 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2002 | // If this is a variadic call, handle args passed through "...". |
| 2003 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2004 | VariadicCallType CallType = VariadicFunction; |
| 2005 | if (Fn->getType()->isBlockPointerType()) |
| 2006 | CallType = VariadicBlock; // Block |
| 2007 | else if (isa<MemberExpr>(Fn)) |
| 2008 | CallType = VariadicMethod; |
| 2009 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2010 | // Promote the arguments (C99 6.5.2.2p7). |
| 2011 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2012 | Expr *Arg = Args[i]; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2013 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2014 | Call->setArg(i, Arg); |
| 2015 | } |
| 2016 | } |
| 2017 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2018 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2021 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2022 | /// This provides the location of the left/right parens and a list of comma |
| 2023 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2024 | Action::OwningExprResult |
| 2025 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2026 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2027 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2028 | unsigned NumArgs = args.size(); |
| 2029 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 2030 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2031 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2032 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2033 | DeclarationName UnqualifiedName; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2035 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2036 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2037 | // in which case we won't do any semantic analysis now. |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2038 | // FIXME: Will need to cache the results of name lookup (including ADL) in Fn. |
| 2039 | bool Dependent = false; |
| 2040 | if (Fn->isTypeDependent()) |
| 2041 | Dependent = true; |
| 2042 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2043 | Dependent = true; |
| 2044 | |
| 2045 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2046 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2047 | Context.DependentTy, RParenLoc)); |
| 2048 | |
| 2049 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2050 | if (Fn->getType()->isRecordType()) |
| 2051 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2052 | CommaLocs, RParenLoc)); |
| 2053 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2054 | // Determine whether this is a call to a member function. |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2055 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2056 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2057 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2058 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2059 | CommaLocs, RParenLoc)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2062 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2063 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2064 | Expr *FnExpr = Fn; |
| 2065 | bool ADL = true; |
| 2066 | while (true) { |
| 2067 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2068 | FnExpr = IcExpr->getSubExpr(); |
| 2069 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2070 | // Parentheses around a function disable ADL |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2071 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2072 | ADL = false; |
| 2073 | FnExpr = PExpr->getSubExpr(); |
| 2074 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2075 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2076 | == UnaryOperator::AddrOf) { |
| 2077 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2078 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2079 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2080 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2081 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2082 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2083 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2084 | UnqualifiedName = DepName->getName(); |
| 2085 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2086 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2087 | // Any kind of name that does not refer to a declaration (or |
| 2088 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2089 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2092 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2093 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2094 | OverloadedFunctionDecl *Ovl = 0; |
| 2095 | if (DRExpr) { |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2096 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2097 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
| 2098 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2100 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2101 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2102 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2103 | ADL = false; |
| 2104 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2105 | // We don't perform ADL in C. |
| 2106 | if (!getLangOptions().CPlusPlus) |
| 2107 | ADL = false; |
| 2108 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2109 | if (Ovl || ADL) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2110 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2111 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2112 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2113 | if (!FDecl) |
| 2114 | return ExprError(); |
| 2115 | |
| 2116 | // Update Fn to refer to the actual function selected. |
| 2117 | Expr *NewFn = 0; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2118 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2119 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2120 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2121 | QDRExpr->getLocation(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2122 | false, false, |
| 2123 | QDRExpr->getSourceRange().getBegin()); |
| 2124 | else |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2125 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2126 | Fn->getSourceRange().getBegin()); |
| 2127 | Fn->Destroy(Context); |
| 2128 | Fn = NewFn; |
| 2129 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2130 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2131 | |
| 2132 | // Promote the function operand. |
| 2133 | UsualUnaryConversions(Fn); |
| 2134 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2135 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2136 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2137 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2138 | Args, NumArgs, |
| 2139 | Context.BoolTy, |
| 2140 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2141 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2142 | const FunctionType *FuncT; |
| 2143 | if (!Fn->getType()->isBlockPointerType()) { |
| 2144 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2145 | // have type pointer to function". |
| 2146 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2147 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2148 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2149 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2150 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2151 | } else { // This is a block call. |
| 2152 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2153 | getAsFunctionType(); |
| 2154 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2155 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2156 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2157 | << Fn->getType() << Fn->getSourceRange()); |
| 2158 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2159 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2160 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2162 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2163 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2164 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2165 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2166 | } else { |
| 2167 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2168 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2169 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2170 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2171 | Expr *Arg = Args[i]; |
| 2172 | DefaultArgumentPromotion(Arg); |
| 2173 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2174 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2175 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2176 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2177 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2178 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2179 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2180 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2182 | // Do special checking on direct calls to functions. |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2183 | if (FDecl) |
| 2184 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2185 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2186 | return Owned(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2189 | Action::OwningExprResult |
| 2190 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2191 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2192 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2193 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 2194 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2195 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2196 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2197 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2198 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2199 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2200 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2201 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2202 | } else if (DiagnoseIncompleteType(LParenLoc, literalType, |
| 2203 | diag::err_typecheck_decl_incomplete_type, |
| 2204 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2205 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2206 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2207 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2208 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2209 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2211 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2212 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2213 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2214 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2215 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2216 | InitExpr.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2217 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2218 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2221 | Action::OwningExprResult |
| 2222 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| 2223 | InitListDesignations &Designators, |
| 2224 | SourceLocation RBraceLoc) { |
| 2225 | unsigned NumInit = initlist.size(); |
| 2226 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2227 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2228 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2229 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2230 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2231 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2232 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2233 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2234 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2237 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 5ad49de | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2238 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2239 | UsualUnaryConversions(castExpr); |
| 2240 | |
| 2241 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2242 | // type needs to be scalar. |
| 2243 | if (castType->isVoidType()) { |
| 2244 | // Cast to void allows any expr type. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2245 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2246 | // We can't check any more until template instantiation time. |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2247 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2248 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2249 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2250 | (castType->isStructureType() || castType->isUnionType())) { |
| 2251 | // GCC struct/union extension: allow cast to self. |
| 2252 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2253 | << castType << castExpr->getSourceRange(); |
| 2254 | } else if (castType->isUnionType()) { |
| 2255 | // GCC cast to union extension |
| 2256 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2257 | RecordDecl::field_iterator Field, FieldEnd; |
| 2258 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2259 | Field != FieldEnd; ++Field) { |
| 2260 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2261 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2262 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2263 | << castExpr->getSourceRange(); |
| 2264 | break; |
| 2265 | } |
| 2266 | } |
| 2267 | if (Field == FieldEnd) |
| 2268 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2269 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2270 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2271 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2272 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2273 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2274 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2275 | } else if (!castExpr->getType()->isScalarType() && |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2276 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2277 | return Diag(castExpr->getLocStart(), |
| 2278 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2279 | << castExpr->getType() << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2280 | } else if (castExpr->getType()->isVectorType()) { |
| 2281 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2282 | return true; |
| 2283 | } else if (castType->isVectorType()) { |
| 2284 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2285 | return true; |
| 2286 | } |
| 2287 | return false; |
| 2288 | } |
| 2289 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2290 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2291 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2292 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2293 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2294 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2295 | return Diag(R.getBegin(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2296 | Ty->isVectorType() ? |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2297 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2298 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2299 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2300 | } else |
| 2301 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2302 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2303 | << VectorTy << Ty << R; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2304 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2305 | return false; |
| 2306 | } |
| 2307 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2308 | Action::OwningExprResult |
| 2309 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2310 | SourceLocation RParenLoc, ExprArg Op) { |
| 2311 | assert((Ty != 0) && (Op.get() != 0) && |
| 2312 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2313 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2314 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2315 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2316 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2317 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2318 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2319 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2320 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 2323 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 2324 | /// In that case, lhs = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2325 | /// C99 6.5.15 |
| 2326 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2327 | SourceLocation QuestionLoc) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2328 | UsualUnaryConversions(Cond); |
| 2329 | UsualUnaryConversions(LHS); |
| 2330 | UsualUnaryConversions(RHS); |
| 2331 | QualType CondTy = Cond->getType(); |
| 2332 | QualType LHSTy = LHS->getType(); |
| 2333 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2334 | |
| 2335 | // first, check the condition. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2336 | if (!Cond->isTypeDependent()) { |
| 2337 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2338 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2339 | << CondTy; |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2340 | return QualType(); |
| 2341 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2342 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2344 | // Now check the two expressions. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2345 | if ((LHS && LHS->isTypeDependent()) || (RHS && RHS->isTypeDependent())) |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2346 | return Context.DependentTy; |
| 2347 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2348 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2349 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2350 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2351 | UsualArithmeticConversions(LHS, RHS); |
| 2352 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2353 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2354 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2355 | // If both operands are the same structure or union type, the result is that |
| 2356 | // type. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2357 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2358 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2359 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2360 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2361 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2362 | return LHSTy.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2363 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2365 | // 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] | 2366 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2367 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2368 | if (!LHSTy->isVoidType()) |
| 2369 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2370 | << RHS->getSourceRange(); |
| 2371 | if (!RHSTy->isVoidType()) |
| 2372 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2373 | << LHS->getSourceRange(); |
| 2374 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2375 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2376 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2377 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2378 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2379 | // the type of the other operand." |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2380 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2381 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2382 | RHS->isNullPointerConstant(Context)) { |
| 2383 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2384 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2385 | } |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2386 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2387 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2388 | LHS->isNullPointerConstant(Context)) { |
| 2389 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2390 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2391 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2393 | // Handle the case where both operands are pointers before we handle null |
| 2394 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2395 | if (const PointerType *LHSPT = LHSTy->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2396 | if (const PointerType *RHSPT = RHSTy->getAsPointerType()) { |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2397 | // get the "pointed to" types |
| 2398 | QualType lhptee = LHSPT->getPointeeType(); |
| 2399 | QualType rhptee = RHSPT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2401 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2402 | if (lhptee->isVoidType() && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2403 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2404 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2405 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2406 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2407 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2408 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2409 | return destType; |
| 2410 | } |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2411 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2412 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2413 | QualType destType = Context.getPointerType(destPointee); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2414 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2415 | ImpCastExprToType(RHS, destType); // promote to void* |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2416 | return destType; |
| 2417 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2419 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
Chris Lattner | 676c86a | 2009-02-19 04:44:58 +0000 | [diff] [blame] | 2420 | // Two identical pointer types are always compatible. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2421 | return LHSTy; |
| 2422 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2423 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2424 | QualType compositeType = LHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2425 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2426 | // If either type is an Objective-C object type then check |
| 2427 | // compatibility according to Objective-C. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2428 | if (Context.isObjCObjectPointerType(LHSTy) || |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2429 | Context.isObjCObjectPointerType(RHSTy)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2430 | // If both operands are interfaces and either operand can be |
| 2431 | // assigned to the other, use that type as the composite |
| 2432 | // type. This allows |
| 2433 | // xxx ? (A*) a : (B*) b |
| 2434 | // where B is a subclass of A. |
| 2435 | // |
| 2436 | // Additionally, as for assignment, if either type is 'id' |
| 2437 | // allow silent coercion. Finally, if the types are |
| 2438 | // incompatible then make sure to use 'id' as the composite |
| 2439 | // type so the result is acceptable for sending messages to. |
| 2440 | |
Steve Naroff | 9fc9cb5 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2441 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2442 | // It could return the composite type. |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2443 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2444 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2445 | if (LHSIface && RHSIface && |
| 2446 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2447 | compositeType = LHSTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2448 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 5183f9e | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2449 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2450 | compositeType = RHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2451 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2452 | Context.isObjCIdStructType(rhptee)) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2453 | compositeType = Context.getObjCIdType(); |
| 2454 | } else { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2455 | Diag(QuestionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2456 | << LHSTy << RHSTy |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2457 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2458 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2459 | ImpCastExprToType(LHS, incompatTy); |
| 2460 | ImpCastExprToType(RHS, incompatTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2461 | return incompatTy; |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2462 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2463 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2464 | rhptee.getUnqualifiedType())) { |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2465 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 2466 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2467 | // In this situation, we assume void* type. No especially good |
| 2468 | // reason, but this is what gcc does, and we do have to pick |
| 2469 | // to get a consistent AST. |
| 2470 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2471 | ImpCastExprToType(LHS, incompatTy); |
| 2472 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | cd23bb2 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2473 | return incompatTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2474 | } |
| 2475 | // The pointer types are compatible. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2476 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2477 | // differently qualified versions of compatible types, the result type is |
| 2478 | // a pointer to an appropriately qualified version of the *composite* |
| 2479 | // type. |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2480 | // FIXME: Need to calculate the composite type. |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2481 | // FIXME: Need to add qualifiers |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2482 | ImpCastExprToType(LHS, compositeType); |
| 2483 | ImpCastExprToType(RHS, compositeType); |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2484 | return compositeType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2485 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2486 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2488 | // Selection between block pointer types is ok as long as they are the same. |
| 2489 | if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() && |
| 2490 | Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) |
| 2491 | return LHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2492 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2493 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2494 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2495 | // id with statically typed objects). |
| 2496 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2497 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2498 | // id. Currently localizing to here until clear this should be |
| 2499 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2500 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2501 | (LHSTy->isObjCQualifiedIdType() && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2502 | Context.isObjCObjectPointerType(RHSTy)) || |
| 2503 | (RHSTy->isObjCQualifiedIdType() && |
| 2504 | Context.isObjCObjectPointerType(LHSTy))) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2505 | // FIXME: This is not the correct composite type. This only |
| 2506 | // happens to work because id can more or less be used anywhere, |
| 2507 | // however this may change the type of method sends. |
| 2508 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2509 | // (confusing) incompatible comparison warnings in some |
| 2510 | // cases. Investigate. |
| 2511 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2512 | ImpCastExprToType(LHS, compositeType); |
| 2513 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2514 | return compositeType; |
| 2515 | } |
| 2516 | } |
| 2517 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2518 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2519 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2520 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2521 | return QualType(); |
| 2522 | } |
| 2523 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2524 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2525 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2526 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2527 | SourceLocation ColonLoc, |
| 2528 | ExprArg Cond, ExprArg LHS, |
| 2529 | ExprArg RHS) { |
| 2530 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2531 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2532 | |
| 2533 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2534 | // was the condition. |
| 2535 | bool isLHSNull = LHSExpr == 0; |
| 2536 | if (isLHSNull) |
| 2537 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2538 | |
| 2539 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2540 | RHSExpr, QuestionLoc); |
| 2541 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2542 | return ExprError(); |
| 2543 | |
| 2544 | Cond.release(); |
| 2545 | LHS.release(); |
| 2546 | RHS.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2547 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2548 | isLHSNull ? 0 : LHSExpr, |
| 2549 | RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2552 | |
| 2553 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2554 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2555 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2556 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2557 | // FIXME: add a couple examples in this comment. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2558 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2559 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2560 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2562 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2563 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2564 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2565 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2566 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2567 | lhptee = Context.getCanonicalType(lhptee); |
| 2568 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2570 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2571 | |
| 2572 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2573 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2574 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2575 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2576 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2577 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2578 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2579 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2580 | // 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] | 2581 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2582 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2583 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2584 | return ConvTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2586 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2587 | assert(rhptee->isFunctionType()); |
| 2588 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2589 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2591 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2592 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2593 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2594 | |
| 2595 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2596 | assert(lhptee->isFunctionType()); |
| 2597 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2598 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2599 | // 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] | 2600 | // unqualified versions of compatible types, ... |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2601 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2602 | rhptee.getUnqualifiedType())) |
| 2603 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2604 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2607 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2608 | /// block pointer types are compatible or whether a block and normal pointer |
| 2609 | /// are compatible. It is more restrict than comparing two function pointer |
| 2610 | // types. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2611 | Sema::AssignConvertType |
| 2612 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2613 | QualType rhsType) { |
| 2614 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2615 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2616 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2617 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2618 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2619 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2620 | // make sure we operate on the canonical type |
| 2621 | lhptee = Context.getCanonicalType(lhptee); |
| 2622 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2623 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2624 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2625 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2626 | // For blocks we enforce that qualifiers are identical. |
| 2627 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2628 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2629 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2630 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2631 | return IncompatibleBlockPointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2632 | return ConvTy; |
| 2633 | } |
| 2634 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2635 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2636 | /// has code to accommodate several GCC extensions when type checking |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2637 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2638 | /// |
| 2639 | /// int a, *pint; |
| 2640 | /// short *pshort; |
| 2641 | /// struct foo *pfoo; |
| 2642 | /// |
| 2643 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2644 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2645 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2646 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2647 | /// |
| 2648 | /// 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] | 2649 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2650 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2651 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2652 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2653 | // Get canonical types. We're not formatting these types, just comparing |
| 2654 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2655 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2656 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2657 | |
| 2658 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2659 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2661 | // If the left-hand side is a reference type, then we are in a |
| 2662 | // (rare!) case where we've allowed the use of references in C, |
| 2663 | // e.g., as a parameter type in a built-in function. In this case, |
| 2664 | // just make sure that the type referenced is compatible with the |
| 2665 | // right-hand side type. The caller is responsible for adjusting |
| 2666 | // lhsType so that the resulting expression does not have reference |
| 2667 | // type. |
| 2668 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2669 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2670 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2671 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2672 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2674 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2675 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2676 | return Compatible; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2677 | // Relax integer conversions like we do for pointers below. |
| 2678 | if (rhsType->isIntegerType()) |
| 2679 | return IntToPointer; |
| 2680 | if (lhsType->isIntegerType()) |
| 2681 | return PointerToInt; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2682 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2683 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2684 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2685 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2686 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2687 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2688 | if (LV->getElementType() == rhsType) |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2689 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2690 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2691 | // 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] | 2692 | // 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] | 2693 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2694 | if (getLangOptions().LaxVectorConversions && |
| 2695 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2696 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2697 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2698 | } |
| 2699 | return Incompatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2700 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2702 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2703 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2705 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2706 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2707 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2708 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2709 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2710 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2711 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2712 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2713 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2714 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Treat block pointers as objects. |
| 2717 | if (getLangOptions().ObjC1 && |
| 2718 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2719 | return Compatible; |
| 2720 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2721 | return Incompatible; |
| 2722 | } |
| 2723 | |
| 2724 | if (isa<BlockPointerType>(lhsType)) { |
| 2725 | if (rhsType->isIntegerType()) |
Eli Friedman | c589830 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 2726 | return IntToBlockPointer; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2727 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2728 | // Treat block pointers as objects. |
| 2729 | if (getLangOptions().ObjC1 && |
| 2730 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2731 | return Compatible; |
| 2732 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2733 | if (rhsType->isBlockPointerType()) |
| 2734 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2735 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2736 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 2737 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2738 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2739 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2740 | return Incompatible; |
| 2741 | } |
| 2742 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2743 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2744 | // 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] | 2745 | if (lhsType == Context.BoolTy) |
| 2746 | return Compatible; |
| 2747 | |
| 2748 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2749 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2750 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2751 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2752 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2753 | |
| 2754 | if (isa<BlockPointerType>(lhsType) && |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2755 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2756 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2757 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2758 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2760 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2761 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2762 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2763 | } |
| 2764 | return Incompatible; |
| 2765 | } |
| 2766 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2767 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2768 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2769 | if (getLangOptions().CPlusPlus) { |
| 2770 | if (!lhsType->isRecordType()) { |
| 2771 | // C++ 5.17p3: If the left operand is not of class type, the |
| 2772 | // expression is implicitly converted (C++ 4) to the |
| 2773 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2774 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 2775 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2776 | return Incompatible; |
Douglas Gregor | bb46150 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2777 | else |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2778 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 2782 | // structures. |
| 2783 | } |
| 2784 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2785 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 2786 | // a null pointer constant. |
Steve Naroff | d305a86 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 2787 | if ((lhsType->isPointerType() || |
| 2788 | lhsType->isObjCQualifiedIdType() || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2789 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 2790 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2791 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2792 | return Compatible; |
| 2793 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2794 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2795 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2796 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2797 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2798 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2799 | // |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2800 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2801 | if (!lhsType->isReferenceType()) |
| 2802 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2803 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2804 | Sema::AssignConvertType result = |
| 2805 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2806 | |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2807 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 2808 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2809 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 2810 | // so that we can use references in built-in functions even in C. |
| 2811 | // The getNonReferenceType() call makes sure that the resulting expression |
| 2812 | // does not have reference type. |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2813 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2814 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2815 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2818 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2819 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 2820 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 2821 | } |
| 2822 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2823 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2824 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 2825 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2826 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2827 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2830 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2831 | Expr *&rex) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2832 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2833 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2834 | QualType lhsType = |
| 2835 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 2836 | QualType rhsType = |
| 2837 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2838 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2839 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2840 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2841 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2842 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2843 | // Handle the case of a vector & extvector type of the same size and element |
| 2844 | // 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] | 2845 | if (getLangOptions().LaxVectorConversions) { |
| 2846 | // FIXME: Should we warn here? |
| 2847 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2848 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 2849 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2850 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2851 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2855 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2856 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 2857 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2858 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2859 | QualType eltType = V->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2860 | |
| 2861 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2862 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 2863 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2864 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2865 | return lhsType; |
| 2866 | } |
| 2867 | } |
| 2868 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2869 | // 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] | 2870 | // promote the lhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2871 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2872 | QualType eltType = V->getElementType(); |
| 2873 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2874 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2875 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 2876 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2877 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2878 | return rhsType; |
| 2879 | } |
| 2880 | } |
| 2881 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2882 | // You cannot convert between vector values of different size. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2883 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2884 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2885 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2886 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2889 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2890 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2891 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 2892 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2893 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2894 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2895 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2896 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2897 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2898 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2899 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2903 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2904 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 2905 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 2906 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 2907 | return CheckVectorOperands(Loc, lex, rex); |
| 2908 | return InvalidOperands(Loc, lex, rex); |
| 2909 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2910 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2911 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2913 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2914 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2915 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2919 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2920 | { |
| 2921 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2922 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2923 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2924 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2926 | // handle the common case first (both operands are arithmetic). |
| 2927 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2928 | return compType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2929 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2930 | // Put any potential pointer into PExp |
| 2931 | Expr* PExp = lex, *IExp = rex; |
| 2932 | if (IExp->getType()->isPointerType()) |
| 2933 | std::swap(PExp, IExp); |
| 2934 | |
| 2935 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 2936 | if (IExp->getType()->isIntegerType()) { |
| 2937 | // Check for arithmetic on pointers to incomplete types |
| 2938 | if (!PTy->getPointeeType()->isObjectType()) { |
| 2939 | if (PTy->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2940 | if (getLangOptions().CPlusPlus) { |
| 2941 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 2942 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2943 | return QualType(); |
| 2944 | } |
| 2945 | |
| 2946 | // GNU extension: arithmetic on pointer to void |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2947 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2948 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2949 | } else if (PTy->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2950 | if (getLangOptions().CPlusPlus) { |
| 2951 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2952 | << lex->getType() << lex->getSourceRange(); |
| 2953 | return QualType(); |
| 2954 | } |
| 2955 | |
| 2956 | // GNU extension: arithmetic on pointer to function |
| 2957 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2958 | << lex->getType() << lex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2959 | } else { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2960 | DiagnoseIncompleteType(Loc, PTy->getPointeeType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2961 | diag::err_typecheck_arithmetic_incomplete_type, |
| 2962 | lex->getSourceRange(), SourceRange(), |
| 2963 | lex->getType()); |
| 2964 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | return PExp->getType(); |
| 2968 | } |
| 2969 | } |
| 2970 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2971 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2974 | // C99 6.5.6 |
| 2975 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2976 | SourceLocation Loc, bool isCompAssign) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2977 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2978 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2979 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2980 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2981 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2982 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2983 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2984 | // Handle the common case first (both operands are arithmetic). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2985 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2986 | return compType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2988 | // Either ptr - int or ptr - ptr. |
| 2989 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2990 | QualType lpointee = LHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2991 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2992 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2993 | if (!lpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2994 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2995 | if (lpointee->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2996 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2997 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2998 | } else if (lpointee->isFunctionType()) { |
| 2999 | if (getLangOptions().CPlusPlus) { |
| 3000 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3001 | << lex->getType() << lex->getSourceRange(); |
| 3002 | return QualType(); |
| 3003 | } |
| 3004 | |
| 3005 | // GNU extension: arithmetic on pointer to function |
| 3006 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3007 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3008 | } else { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3009 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3010 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3011 | return QualType(); |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | // The result type of a pointer-int computation is the pointer type. |
| 3016 | if (rex->getType()->isIntegerType()) |
| 3017 | return lex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3019 | // Handle pointer-pointer subtractions. |
| 3020 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3021 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3022 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3023 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3024 | if (!rpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3025 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3026 | if (rpointee->isVoidType()) { |
| 3027 | if (!lpointee->isVoidType()) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3028 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3029 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f93eda1 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 3030 | } else if (rpointee->isFunctionType()) { |
| 3031 | if (getLangOptions().CPlusPlus) { |
| 3032 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3033 | << rex->getType() << rex->getSourceRange(); |
| 3034 | return QualType(); |
| 3035 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | f93eda1 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 3037 | // GNU extension: arithmetic on pointer to function |
| 3038 | if (!lpointee->isFunctionType()) |
| 3039 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3040 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3041 | } else { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3042 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3043 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3044 | return QualType(); |
| 3045 | } |
| 3046 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3047 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3048 | // Pointee types must be compatible. |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3049 | if (!Context.typesAreCompatible( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3050 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 3051 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3052 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3053 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3054 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3055 | return QualType(); |
| 3056 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3058 | return Context.getPointerDiffType(); |
| 3059 | } |
| 3060 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3061 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3062 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3065 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3066 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3067 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3068 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3069 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3070 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3072 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3073 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3074 | if (!isCompAssign) |
| 3075 | UsualUnaryConversions(lex); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3076 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3077 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3078 | // "The type of the result is that of the promoted left operand." |
| 3079 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3082 | // C99 6.5.8 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3083 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3084 | bool isRelational) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3085 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3086 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3088 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3089 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3090 | UsualArithmeticConversions(lex, rex); |
| 3091 | else { |
| 3092 | UsualUnaryConversions(lex); |
| 3093 | UsualUnaryConversions(rex); |
| 3094 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3095 | QualType lType = lex->getType(); |
| 3096 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3097 | |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3098 | // For non-floating point types, check for self-comparisons of the form |
| 3099 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3100 | // often indicate logic errors in the program. |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3101 | if (!lType->isFloatingType()) { |
Ted Kremenek | 87e30c5 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 3102 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3103 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3104 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3105 | Diag(Loc, diag::warn_selfcomparison); |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3106 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3108 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| 3109 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy : Context.IntTy; |
| 3110 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3111 | if (isRelational) { |
| 3112 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3113 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3114 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3115 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3116 | if (lType->isFloatingType()) { |
| 3117 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3118 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3119 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3120 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3121 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3122 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3123 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3124 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3125 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3126 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3128 | // All of the following pointer related warnings are GCC extensions, except |
| 3129 | // when handling null pointer constants. One day, we can consider making them |
| 3130 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3131 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3132 | QualType LCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3133 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3134 | QualType RCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3135 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3136 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3137 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3138 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3139 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3140 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3141 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3142 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3143 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3144 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3145 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3146 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3147 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3148 | // Handle block pointer types. |
| 3149 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 3150 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3151 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3152 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3153 | if (!LHSIsNull && !RHSIsNull && |
| 3154 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3155 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3156 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3157 | } |
| 3158 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3159 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3160 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3161 | // Allow block pointers to be compared with null pointer constants. |
| 3162 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 3163 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 3164 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3165 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3166 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3167 | } |
| 3168 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3169 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3170 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3171 | |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3172 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3173 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3174 | const PointerType *LPT = lType->getAsPointerType(); |
| 3175 | const PointerType *RPT = rType->getAsPointerType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3176 | bool LPtrToVoid = LPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3177 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3178 | bool RPtrToVoid = RPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3179 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3180 | |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3181 | if (!LPtrToVoid && !RPtrToVoid && |
| 3182 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3183 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3184 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3185 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3186 | return ResultTy; |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3187 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3188 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3189 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 3190 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3191 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 3192 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3193 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3194 | } else { |
| 3195 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3196 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3197 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3198 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3199 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3200 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3201 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 3202 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3203 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3204 | rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3205 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3206 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3207 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3208 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3209 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3210 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3211 | if (lType->isIntegerType() && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3212 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3213 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3214 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3215 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3216 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3217 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3218 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3219 | // Handle block pointers. |
| 3220 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 3221 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3222 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3223 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3224 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3225 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3226 | } |
| 3227 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 3228 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3229 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3230 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3231 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3232 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3233 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3234 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3237 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3238 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3239 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3240 | /// types. |
| 3241 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3242 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3243 | bool isRelational) { |
| 3244 | // Check to make sure we're operating on vectors of the same type and width, |
| 3245 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3246 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3247 | if (vType.isNull()) |
| 3248 | return vType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3249 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3250 | QualType lType = lex->getType(); |
| 3251 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3252 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3253 | // For non-floating point types, check for self-comparisons of the form |
| 3254 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3255 | // often indicate logic errors in the program. |
| 3256 | if (!lType->isFloatingType()) { |
| 3257 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3258 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3259 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3260 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3261 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3262 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3263 | // Check for comparisons of floating point operands using != and ==. |
| 3264 | if (!isRelational && lType->isFloatingType()) { |
| 3265 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3266 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3267 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3268 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3269 | // Return the type for the comparison, which is the same as vector type for |
| 3270 | // integer vectors, or an integer type of identical size and number of |
| 3271 | // elements for floating point vectors. |
| 3272 | if (lType->isIntegerType()) |
| 3273 | return lType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3274 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3275 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3276 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3277 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3278 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3279 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 3280 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3281 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3282 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3283 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3284 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3285 | } |
| 3286 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3287 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3288 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3289 | { |
| 3290 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3291 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3292 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3293 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3295 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3296 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3297 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3301 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3302 | { |
| 3303 | UsualUnaryConversions(lex); |
| 3304 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3305 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3306 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3307 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3308 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3311 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3312 | /// is a read-only property; return true if so. A readonly property expression |
| 3313 | /// depends on various declarations and thus must be treated specially. |
| 3314 | /// |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3315 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3316 | { |
| 3317 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3318 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3319 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3320 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3321 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3322 | if (const ObjCInterfaceType *IFTy = |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3323 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3324 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3325 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3326 | return true; |
| 3327 | } |
| 3328 | } |
| 3329 | return false; |
| 3330 | } |
| 3331 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3332 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3333 | /// emit an error and return true. If so, return false. |
| 3334 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3335 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3336 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3337 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3338 | if (IsLV == Expr::MLV_Valid) |
| 3339 | return false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3341 | unsigned Diag = 0; |
| 3342 | bool NeedType = false; |
| 3343 | switch (IsLV) { // C99 6.5.16p2 |
| 3344 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3345 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3346 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3347 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3348 | NeedType = true; |
| 3349 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3350 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3351 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3352 | NeedType = true; |
| 3353 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3354 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3355 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3356 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3357 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3358 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3359 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3360 | case Expr::MLV_IncompleteType: |
| 3361 | case Expr::MLV_IncompleteVoidType: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3362 | return S.DiagnoseIncompleteType(Loc, E->getType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3363 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3364 | E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3365 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3366 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3367 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3368 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3369 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3370 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3371 | case Expr::MLV_ReadonlyProperty: |
| 3372 | Diag = diag::error_readonly_property_assignment; |
| 3373 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3374 | case Expr::MLV_NoSetterProperty: |
| 3375 | Diag = diag::error_nosetter_property_assignment; |
| 3376 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3377 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3379 | if (NeedType) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3380 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3381 | else |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3382 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3383 | return true; |
| 3384 | } |
| 3385 | |
| 3386 | |
| 3387 | |
| 3388 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3389 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3390 | SourceLocation Loc, |
| 3391 | QualType CompoundType) { |
| 3392 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3393 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3394 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3395 | |
| 3396 | QualType LHSType = LHS->getType(); |
| 3397 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3398 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3399 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3400 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3401 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3402 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3403 | // Special case of NSObject attributes on c-style pointer types. |
| 3404 | if (ConvTy == IncompatiblePointer && |
| 3405 | ((Context.isObjCNSObjectType(LHSType) && |
| 3406 | Context.isObjCObjectPointerType(RHSType)) || |
| 3407 | (Context.isObjCNSObjectType(RHSType) && |
| 3408 | Context.isObjCObjectPointerType(LHSType)))) |
| 3409 | ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3410 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3411 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3412 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3413 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3414 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3415 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3416 | RHSCheck = ICE->getSubExpr(); |
| 3417 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3418 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3419 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3420 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3421 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3422 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3423 | Diag(Loc, diag::warn_not_compound_assign) |
| 3424 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3425 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3426 | } |
| 3427 | } else { |
| 3428 | // Compound assignment "x += y" |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3429 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3430 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3432 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3433 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3434 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3436 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3437 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3438 | // it is the unqualified version of the type of the left operand. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3439 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3440 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3441 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3442 | // oprdu. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3443 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3446 | // C99 6.5.17 |
| 3447 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
| 3448 | // FIXME: what is required for LHS? |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3449 | |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3450 | // 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] | 3451 | DefaultFunctionArrayConversion(RHS); |
| 3452 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3456 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3457 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3458 | bool isInc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 3459 | if (Op->isTypeDependent()) |
| 3460 | return Context.DependentTy; |
| 3461 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3462 | QualType ResType = Op->getType(); |
| 3463 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3464 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3465 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3466 | // Decrement of bool is not allowed. |
| 3467 | if (!isInc) { |
| 3468 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3469 | return QualType(); |
| 3470 | } |
| 3471 | // Increment of bool sets it to true, but is deprecated. |
| 3472 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3473 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3474 | // OK! |
| 3475 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3476 | // C99 6.5.2.4p2, 6.5.6p2 |
| 3477 | if (PT->getPointeeType()->isObjectType()) { |
| 3478 | // Pointer to object is ok! |
| 3479 | } else if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3480 | if (getLangOptions().CPlusPlus) { |
| 3481 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3482 | << Op->getSourceRange(); |
| 3483 | return QualType(); |
| 3484 | } |
| 3485 | |
| 3486 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3487 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3488 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3489 | if (getLangOptions().CPlusPlus) { |
| 3490 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3491 | << Op->getType() << Op->getSourceRange(); |
| 3492 | return QualType(); |
| 3493 | } |
| 3494 | |
| 3495 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3496 | << ResType << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3497 | return QualType(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3498 | } else { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3499 | DiagnoseIncompleteType(OpLoc, PT->getPointeeType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3500 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3501 | Op->getSourceRange(), SourceRange(), |
| 3502 | ResType); |
| 3503 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3504 | } |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3505 | } else if (ResType->isComplexType()) { |
| 3506 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3507 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3508 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3509 | } else { |
| 3510 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3511 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3512 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3513 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3514 | // 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] | 3515 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3516 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3517 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3518 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3521 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3522 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3523 | /// where the declaration is needed for type checking. We only need to |
| 3524 | /// handle cases when the expression references a function designator |
| 3525 | /// or is an lvalue. Here are some examples: |
| 3526 | /// - &(x) => x |
| 3527 | /// - &*****f => f for f a function designator. |
| 3528 | /// - &s.xx => s |
| 3529 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3530 | /// - *(x + 1) -> x, if x is an array |
| 3531 | /// - &"123"[2] -> 0 |
| 3532 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3533 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3534 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3535 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3536 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3537 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3538 | case Stmt::MemberExprClass: |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3539 | // Fields cannot be declared with a 'register' storage class. |
| 3540 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3541 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3542 | return 0; |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3543 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3544 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3545 | // &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] | 3546 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3547 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 612720d | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3548 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | 655694e | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3549 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3550 | return 0; |
| 3551 | else |
| 3552 | return VD; |
| 3553 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3554 | case Stmt::UnaryOperatorClass: { |
| 3555 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3556 | |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3557 | switch(UO->getOpcode()) { |
| 3558 | case UnaryOperator::Deref: { |
| 3559 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3560 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3561 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3562 | if (!VD || VD->getType()->isPointerType()) |
| 3563 | return 0; |
| 3564 | return VD; |
| 3565 | } |
| 3566 | return 0; |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3567 | } |
| 3568 | case UnaryOperator::Real: |
| 3569 | case UnaryOperator::Imag: |
| 3570 | case UnaryOperator::Extension: |
| 3571 | return getPrimaryDecl(UO->getSubExpr()); |
| 3572 | default: |
| 3573 | return 0; |
| 3574 | } |
| 3575 | } |
| 3576 | case Stmt::BinaryOperatorClass: { |
| 3577 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3578 | |
| 3579 | // Handle cases involving pointer arithmetic. The result of an |
| 3580 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3581 | |
| 3582 | // (x + n) or (n + x) => x |
| 3583 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3584 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3585 | return getPrimaryDecl(BO->getLHS()); |
| 3586 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3587 | return getPrimaryDecl(BO->getRHS()); |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | return 0; |
| 3592 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3593 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3594 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3595 | case Stmt::ImplicitCastExprClass: |
| 3596 | // &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] | 3597 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3598 | default: |
| 3599 | return 0; |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3604 | /// 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] | 3605 | /// 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] | 3606 | /// Note: The usual conversions are *not* applied to the operand of the & |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3607 | /// 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] | 3608 | /// 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] | 3609 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3610 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3611 | if (op->isTypeDependent()) |
| 3612 | return Context.DependentTy; |
| 3613 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3614 | if (getLangOptions().C99) { |
| 3615 | // Implement C99-only parts of addressof rules. |
| 3616 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 3617 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 3618 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 3619 | // (assuming the deref expression is valid). |
| 3620 | return uOp->getSubExpr()->getType(); |
| 3621 | } |
| 3622 | // Technically, there should be a check for array subscript |
| 3623 | // expressions here, but the result of one is always an lvalue anyway. |
| 3624 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3625 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 3626 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 3627 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3628 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3629 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 3630 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3631 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 3632 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3633 | return QualType(); |
| 3634 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3635 | } 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] | 3636 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 3637 | if (Field->isBitField()) { |
| 3638 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3639 | << "bit-field" << op->getSourceRange(); |
| 3640 | return QualType(); |
| 3641 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3642 | } |
| 3643 | // Check for Apple extension for accessing vector components. |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3644 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 3645 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3646 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3647 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3648 | return QualType(); |
| 3649 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3650 | // 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] | 3651 | // with the register storage-class specifier. |
| 3652 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 3653 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3654 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3655 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3656 | return QualType(); |
| 3657 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3658 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3659 | return Context.OverloadTy; |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3660 | } else if (isa<FieldDecl>(dcl)) { |
| 3661 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 3662 | // Could be a pointer to member, though, if there is an explicit |
| 3663 | // scope qualifier for the class. |
| 3664 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3665 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3666 | if (Ctx && Ctx->isRecord()) |
| 3667 | return Context.getMemberPointerType(op->getType(), |
| 3668 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3669 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3670 | } else if (isa<FunctionDecl>(dcl)) { |
| 3671 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3672 | // As above. |
| 3673 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3674 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3675 | if (Ctx && Ctx->isRecord()) |
| 3676 | return Context.getMemberPointerType(op->getType(), |
| 3677 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3678 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3679 | } |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3680 | else |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3681 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3682 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3683 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3684 | // If the operand has type "type", the result has type "pointer to type". |
| 3685 | return Context.getPointerType(op->getType()); |
| 3686 | } |
| 3687 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3688 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 3689 | if (Op->isTypeDependent()) |
| 3690 | return Context.DependentTy; |
| 3691 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3692 | UsualUnaryConversions(Op); |
| 3693 | QualType Ty = Op->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3694 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3695 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 3696 | // incomplete type or void. It would be possible to warn about dereferencing |
| 3697 | // a void pointer, but it's completely well-defined, and such a warning is |
| 3698 | // unlikely to catch any mistakes. |
| 3699 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3700 | return PT->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3701 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3702 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3703 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3704 | return QualType(); |
| 3705 | } |
| 3706 | |
| 3707 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 3708 | tok::TokenKind Kind) { |
| 3709 | BinaryOperator::Opcode Opc; |
| 3710 | switch (Kind) { |
| 3711 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3712 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 3713 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3714 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 3715 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 3716 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 3717 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 3718 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 3719 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 3720 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 3721 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 3722 | case tok::less: Opc = BinaryOperator::LT; break; |
| 3723 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 3724 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 3725 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 3726 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 3727 | case tok::amp: Opc = BinaryOperator::And; break; |
| 3728 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 3729 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 3730 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 3731 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 3732 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 3733 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 3734 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 3735 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 3736 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 3737 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 3738 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 3739 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 3740 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 3741 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 3742 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 3743 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 3744 | } |
| 3745 | return Opc; |
| 3746 | } |
| 3747 | |
| 3748 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 3749 | tok::TokenKind Kind) { |
| 3750 | UnaryOperator::Opcode Opc; |
| 3751 | switch (Kind) { |
| 3752 | default: assert(0 && "Unknown unary op!"); |
| 3753 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 3754 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 3755 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 3756 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 3757 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 3758 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 3759 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 3760 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3761 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 3762 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 3763 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 3764 | } |
| 3765 | return Opc; |
| 3766 | } |
| 3767 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3768 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 3769 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 3770 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3771 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 3772 | unsigned Op, |
| 3773 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3774 | QualType ResultTy; // Result type of the binary operator. |
| 3775 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 3776 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
| 3777 | |
| 3778 | switch (Opc) { |
| 3779 | default: |
| 3780 | assert(0 && "Unknown binary expr!"); |
| 3781 | case BinaryOperator::Assign: |
| 3782 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 3783 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3784 | case BinaryOperator::PtrMemD: |
| 3785 | case BinaryOperator::PtrMemI: |
| 3786 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 3787 | Opc == BinaryOperator::PtrMemI); |
| 3788 | break; |
| 3789 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3790 | case BinaryOperator::Div: |
| 3791 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 3792 | break; |
| 3793 | case BinaryOperator::Rem: |
| 3794 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 3795 | break; |
| 3796 | case BinaryOperator::Add: |
| 3797 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 3798 | break; |
| 3799 | case BinaryOperator::Sub: |
| 3800 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 3801 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3802 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3803 | case BinaryOperator::Shr: |
| 3804 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 3805 | break; |
| 3806 | case BinaryOperator::LE: |
| 3807 | case BinaryOperator::LT: |
| 3808 | case BinaryOperator::GE: |
| 3809 | case BinaryOperator::GT: |
| 3810 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, true); |
| 3811 | break; |
| 3812 | case BinaryOperator::EQ: |
| 3813 | case BinaryOperator::NE: |
| 3814 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, false); |
| 3815 | break; |
| 3816 | case BinaryOperator::And: |
| 3817 | case BinaryOperator::Xor: |
| 3818 | case BinaryOperator::Or: |
| 3819 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 3820 | break; |
| 3821 | case BinaryOperator::LAnd: |
| 3822 | case BinaryOperator::LOr: |
| 3823 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 3824 | break; |
| 3825 | case BinaryOperator::MulAssign: |
| 3826 | case BinaryOperator::DivAssign: |
| 3827 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 3828 | if (!CompTy.isNull()) |
| 3829 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3830 | break; |
| 3831 | case BinaryOperator::RemAssign: |
| 3832 | CompTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 3833 | if (!CompTy.isNull()) |
| 3834 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3835 | break; |
| 3836 | case BinaryOperator::AddAssign: |
| 3837 | CompTy = CheckAdditionOperands(lhs, rhs, OpLoc, true); |
| 3838 | if (!CompTy.isNull()) |
| 3839 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3840 | break; |
| 3841 | case BinaryOperator::SubAssign: |
| 3842 | CompTy = CheckSubtractionOperands(lhs, rhs, OpLoc, true); |
| 3843 | if (!CompTy.isNull()) |
| 3844 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3845 | break; |
| 3846 | case BinaryOperator::ShlAssign: |
| 3847 | case BinaryOperator::ShrAssign: |
| 3848 | CompTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 3849 | if (!CompTy.isNull()) |
| 3850 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3851 | break; |
| 3852 | case BinaryOperator::AndAssign: |
| 3853 | case BinaryOperator::XorAssign: |
| 3854 | case BinaryOperator::OrAssign: |
| 3855 | CompTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 3856 | if (!CompTy.isNull()) |
| 3857 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3858 | break; |
| 3859 | case BinaryOperator::Comma: |
| 3860 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 3861 | break; |
| 3862 | } |
| 3863 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3864 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3865 | if (CompTy.isNull()) |
| 3866 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 3867 | else |
| 3868 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3869 | CompTy, OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3872 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3873 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 3874 | tok::TokenKind Kind, |
| 3875 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3876 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3877 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3878 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3879 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 3880 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3882 | // If either expression is type-dependent, just build the AST. |
| 3883 | // FIXME: We'll need to perform some caching of the result of name |
| 3884 | // lookup for operator+. |
| 3885 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3886 | if (Opc > BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) |
| 3887 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3888 | Context.DependentTy, |
| 3889 | Context.DependentTy, TokLoc)); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3890 | else |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3891 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, |
| 3892 | Context.DependentTy, TokLoc)); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3895 | if (getLangOptions().CPlusPlus && Opc != BinaryOperator::PtrMemD && |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3896 | (lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType() || |
| 3897 | rhs->getType()->isRecordType() || rhs->getType()->isEnumeralType())) { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3898 | // If this is one of the assignment operators, we only perform |
| 3899 | // overload resolution if the left-hand side is a class or |
| 3900 | // enumeration type (C++ [expr.ass]p3). |
| 3901 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3902 | !(lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType())) { |
| 3903 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
| 3904 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3905 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3906 | // Determine which overloaded operator we're dealing with. |
| 3907 | static const OverloadedOperatorKind OverOps[] = { |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3908 | // Overloading .* is not possible. |
| 3909 | static_cast<OverloadedOperatorKind>(0), OO_ArrowStar, |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3910 | OO_Star, OO_Slash, OO_Percent, |
| 3911 | OO_Plus, OO_Minus, |
| 3912 | OO_LessLess, OO_GreaterGreater, |
| 3913 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 3914 | OO_EqualEqual, OO_ExclaimEqual, |
| 3915 | OO_Amp, |
| 3916 | OO_Caret, |
| 3917 | OO_Pipe, |
| 3918 | OO_AmpAmp, |
| 3919 | OO_PipePipe, |
| 3920 | OO_Equal, OO_StarEqual, |
| 3921 | OO_SlashEqual, OO_PercentEqual, |
| 3922 | OO_PlusEqual, OO_MinusEqual, |
| 3923 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 3924 | OO_AmpEqual, OO_CaretEqual, |
| 3925 | OO_PipeEqual, |
| 3926 | OO_Comma |
| 3927 | }; |
| 3928 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3929 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3930 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3931 | // to the candidate set. |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3932 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3933 | Expr *Args[2] = { lhs, rhs }; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3934 | if (AddOperatorCandidates(OverOp, S, TokLoc, Args, 2, CandidateSet)) |
| 3935 | return ExprError(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3936 | |
| 3937 | // Perform overload resolution. |
| 3938 | OverloadCandidateSet::iterator Best; |
| 3939 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3940 | case OR_Success: { |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3941 | // We found a built-in operator or an overloaded operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3942 | FunctionDecl *FnDecl = Best->Function; |
| 3943 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3944 | if (FnDecl) { |
| 3945 | // We matched an overloaded operator. Build a call to that |
| 3946 | // operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3948 | // Convert the arguments. |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3949 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3950 | if (PerformObjectArgumentInitialization(lhs, Method) || |
| 3951 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(0)->getType(), |
| 3952 | "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3953 | return ExprError(); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3954 | } else { |
| 3955 | // Convert the arguments. |
| 3956 | if (PerformCopyInitialization(lhs, FnDecl->getParamDecl(0)->getType(), |
| 3957 | "passing") || |
| 3958 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(1)->getType(), |
| 3959 | "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3960 | return ExprError(); |
Douglas Gregor | 5ed1504 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3961 | } |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3963 | // Determine the result type |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3964 | QualType ResultTy |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3965 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3966 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3967 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3968 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3969 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3970 | SourceLocation()); |
Douglas Gregor | 65fedaf | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 3971 | UsualUnaryConversions(FnExpr); |
| 3972 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3973 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3974 | ResultTy, TokLoc)); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3975 | } else { |
| 3976 | // We matched a built-in operator. Convert the arguments, then |
| 3977 | // break out so that we will build the appropriate built-in |
| 3978 | // operator node. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3979 | if (PerformImplicitConversion(lhs, Best->BuiltinTypes.ParamTypes[0], |
| 3980 | Best->Conversions[0], "passing") || |
| 3981 | PerformImplicitConversion(rhs, Best->BuiltinTypes.ParamTypes[1], |
| 3982 | Best->Conversions[1], "passing")) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3983 | return ExprError(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3984 | |
| 3985 | break; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3986 | } |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | case OR_No_Viable_Function: |
| 3990 | // No viable function; fall through to handling this as a |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3991 | // built-in operator, which will produce an error message for us. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | |
| 3994 | case OR_Ambiguous: |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3995 | Diag(TokLoc, diag::err_ovl_ambiguous_oper) |
| 3996 | << BinaryOperator::getOpcodeStr(Opc) |
| 3997 | << lhs->getSourceRange() << rhs->getSourceRange(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3998 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3999 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4000 | |
| 4001 | case OR_Deleted: |
| 4002 | Diag(TokLoc, diag::err_ovl_deleted_oper) |
| 4003 | << Best->Function->isDeleted() |
| 4004 | << BinaryOperator::getOpcodeStr(Opc) |
| 4005 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 4006 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4007 | return ExprError(); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4010 | // Either we found no viable overloaded operator or we matched a |
| 4011 | // built-in operator. In either case, fall through to trying to |
| 4012 | // build a built-in operation. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4015 | // Build a built-in binary operation. |
| 4016 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | // Unary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4020 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 4021 | tok::TokenKind Op, ExprArg input) { |
| 4022 | // FIXME: Input is modified later, but smart pointer not reassigned. |
| 4023 | Expr *Input = (Expr*)input.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4024 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4025 | |
| 4026 | if (getLangOptions().CPlusPlus && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4027 | (Input->getType()->isRecordType() |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4028 | || Input->getType()->isEnumeralType())) { |
| 4029 | // Determine which overloaded operator we're dealing with. |
| 4030 | static const OverloadedOperatorKind OverOps[] = { |
| 4031 | OO_None, OO_None, |
| 4032 | OO_PlusPlus, OO_MinusMinus, |
| 4033 | OO_Amp, OO_Star, |
| 4034 | OO_Plus, OO_Minus, |
| 4035 | OO_Tilde, OO_Exclaim, |
| 4036 | OO_None, OO_None, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4037 | OO_None, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4038 | OO_None |
| 4039 | }; |
| 4040 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 4041 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4042 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4043 | // to the candidate set. |
| 4044 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 48a8732 | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 4045 | if (OverOp != OO_None && |
| 4046 | AddOperatorCandidates(OverOp, S, OpLoc, &Input, 1, CandidateSet)) |
| 4047 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4048 | |
| 4049 | // Perform overload resolution. |
| 4050 | OverloadCandidateSet::iterator Best; |
| 4051 | switch (BestViableFunction(CandidateSet, Best)) { |
| 4052 | case OR_Success: { |
| 4053 | // We found a built-in operator or an overloaded operator. |
| 4054 | FunctionDecl *FnDecl = Best->Function; |
| 4055 | |
| 4056 | if (FnDecl) { |
| 4057 | // We matched an overloaded operator. Build a call to that |
| 4058 | // operator. |
| 4059 | |
| 4060 | // Convert the arguments. |
| 4061 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4062 | if (PerformObjectArgumentInitialization(Input, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4063 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4064 | } else { |
| 4065 | // Convert the arguments. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4066 | if (PerformCopyInitialization(Input, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4067 | FnDecl->getParamDecl(0)->getType(), |
| 4068 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4069 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4073 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4074 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4075 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4077 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4078 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4079 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4080 | UsualUnaryConversions(FnExpr); |
| 4081 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4082 | input.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4083 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, &Input, |
| 4084 | 1, ResultTy, OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4085 | } else { |
| 4086 | // We matched a built-in operator. Convert the arguments, then |
| 4087 | // break out so that we will build the appropriate built-in |
| 4088 | // operator node. |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4089 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4090 | Best->Conversions[0], "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4091 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4092 | |
| 4093 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4094 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | case OR_No_Viable_Function: |
| 4098 | // No viable function; fall through to handling this as a |
| 4099 | // built-in operator, which will produce an error message for us. |
| 4100 | break; |
| 4101 | |
| 4102 | case OR_Ambiguous: |
| 4103 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4104 | << UnaryOperator::getOpcodeStr(Opc) |
| 4105 | << Input->getSourceRange(); |
| 4106 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4107 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4108 | |
| 4109 | case OR_Deleted: |
| 4110 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4111 | << Best->Function->isDeleted() |
| 4112 | << UnaryOperator::getOpcodeStr(Opc) |
| 4113 | << Input->getSourceRange(); |
| 4114 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4115 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | // Either we found no viable overloaded operator or we matched a |
| 4119 | // built-in operator. In either case, fall through to trying to |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4120 | // build a built-in operation. |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4123 | QualType resultType; |
| 4124 | switch (Opc) { |
| 4125 | default: |
| 4126 | assert(0 && "Unimplemented unary expr!"); |
| 4127 | case UnaryOperator::PreInc: |
| 4128 | case UnaryOperator::PreDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4129 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4130 | Opc == UnaryOperator::PreInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4131 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4132 | case UnaryOperator::AddrOf: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4133 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4134 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4135 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4136 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4137 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4138 | break; |
| 4139 | case UnaryOperator::Plus: |
| 4140 | case UnaryOperator::Minus: |
| 4141 | UsualUnaryConversions(Input); |
| 4142 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4143 | if (resultType->isDependentType()) |
| 4144 | break; |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4145 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4146 | break; |
| 4147 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4148 | resultType->isEnumeralType()) |
| 4149 | break; |
| 4150 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4151 | Opc == UnaryOperator::Plus && |
| 4152 | resultType->isPointerType()) |
| 4153 | break; |
| 4154 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4155 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4156 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4157 | case UnaryOperator::Not: // bitwise complement |
| 4158 | UsualUnaryConversions(Input); |
| 4159 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4160 | if (resultType->isDependentType()) |
| 4161 | break; |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4162 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4163 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4164 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4165 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4166 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4167 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4168 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4169 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4170 | break; |
| 4171 | case UnaryOperator::LNot: // logical negation |
| 4172 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 4173 | DefaultFunctionArrayConversion(Input); |
| 4174 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4175 | if (resultType->isDependentType()) |
| 4176 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4177 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4178 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4179 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4180 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4181 | // In C++, it's bool. C++ 5.3.1p8 |
| 4182 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4183 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4184 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4185 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4186 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4187 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4188 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4189 | resultType = Input->getType(); |
| 4190 | break; |
| 4191 | } |
| 4192 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4193 | return ExprError(); |
| 4194 | input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4195 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4198 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4199 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4200 | SourceLocation LabLoc, |
| 4201 | IdentifierInfo *LabelII) { |
| 4202 | // Look up the record for this label identifier. |
| 4203 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4204 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4205 | // If we haven't seen this label yet, create a forward reference. It |
| 4206 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4207 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4208 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4209 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4210 | // Create the AST node. The address of a label always has type 'void*'. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4211 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4212 | Context.getPointerType(Context.VoidTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4215 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4216 | SourceLocation RPLoc) { // "({..})" |
| 4217 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 4218 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4219 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4220 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4221 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 4222 | if (isFileScope) { |
| 4223 | return Diag(LPLoc, diag::err_stmtexpr_file_scope); |
| 4224 | } |
| 4225 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4226 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4227 | // example, it is not possible to goto into a stmt expression apparently. |
| 4228 | // More semantic analysis is needed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4229 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4230 | // FIXME: the last statement in the compount stmt has its value used. We |
| 4231 | // should not warn about it being unused. |
| 4232 | |
| 4233 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4234 | // as the type of the stmtexpr. |
| 4235 | QualType Ty = Context.VoidTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4236 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4237 | if (!Compound->body_empty()) { |
| 4238 | Stmt *LastStmt = Compound->body_back(); |
| 4239 | // If LastStmt is a label, skip down through into the body. |
| 4240 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4241 | LastStmt = Label->getSubStmt(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4242 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4243 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4244 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4245 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4246 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4247 | return new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4248 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4250 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4251 | SourceLocation BuiltinLoc, |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4252 | SourceLocation TypeLoc, |
| 4253 | TypeTy *argty, |
| 4254 | OffsetOfComponent *CompPtr, |
| 4255 | unsigned NumComponents, |
| 4256 | SourceLocation RPLoc) { |
| 4257 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4258 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4259 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4260 | bool Dependent = ArgTy->isDependentType(); |
| 4261 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4262 | // We must have at least one component that refers to the type, and the first |
| 4263 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4264 | // a struct/union/class. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4265 | if (!Dependent && !ArgTy->isRecordType()) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4266 | return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4267 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4268 | // Otherwise, create a compound literal expression as the base, and |
| 4269 | // iteratively process the offsetof designators. |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4270 | InitListExpr *IList = |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 4271 | new (Context) InitListExpr(SourceLocation(), 0, 0, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4272 | IList->setType(ArgTy); |
| 4273 | Expr *Res = |
| 4274 | new (Context) CompoundLiteralExpr(SourceLocation(), ArgTy, IList, false); |
| 4275 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4276 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4277 | // GCC extension, diagnose them. |
| 4278 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4279 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4280 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4281 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4282 | if (!Dependent) { |
| 4283 | // FIXME: Dependent case loses a lot of information here. And probably |
| 4284 | // leaks like a sieve. |
| 4285 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4286 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4287 | if (OC.isBrackets) { |
| 4288 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 4289 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 4290 | if (!AT) { |
| 4291 | Res->Destroy(Context); |
| 4292 | return Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 4293 | << Res->getType(); |
| 4294 | } |
| 4295 | |
| 4296 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4297 | |
| 4298 | // C99 6.5.2.1p1 |
| 4299 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 4300 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
| 4301 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript) |
| 4302 | << Idx->getSourceRange(); |
| 4303 | |
| 4304 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4305 | OC.LocEnd); |
| 4306 | continue; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4307 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4308 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4309 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4310 | if (!RC) { |
| 4311 | Res->Destroy(Context); |
| 4312 | return Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 4313 | << Res->getType(); |
| 4314 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4315 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4316 | // Get the decl corresponding to this. |
| 4317 | RecordDecl *RD = RC->getDecl(); |
| 4318 | FieldDecl *MemberDecl |
| 4319 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4320 | LookupMemberName) |
| 4321 | .getAsDecl()); |
| 4322 | if (!MemberDecl) |
| 4323 | return Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4324 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4325 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4326 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4327 | // FIXME: Verify that MemberDecl isn't a bitfield. |
| 4328 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4329 | // matter here. |
| 4330 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4331 | MemberDecl->getType().getNonReferenceType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4332 | } |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4333 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4334 | |
| 4335 | return new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4336 | Context.getSizeType(), BuiltinLoc); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4340 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4341 | TypeTy *arg1, TypeTy *arg2, |
| 4342 | SourceLocation RPLoc) { |
| 4343 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4344 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4345 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4346 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4347 | |
| 4348 | return new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4349 | argT2, RPLoc); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4352 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4353 | ExprTy *expr1, ExprTy *expr2, |
| 4354 | SourceLocation RPLoc) { |
| 4355 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 4356 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 4357 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4358 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4359 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4360 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4361 | QualType resType; |
| 4362 | if (CondExpr->isValueDependent()) { |
| 4363 | resType = Context.DependentTy; |
| 4364 | } else { |
| 4365 | // The conditional expression is required to be a constant expression. |
| 4366 | llvm::APSInt condEval(32); |
| 4367 | SourceLocation ExpLoc; |
| 4368 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 4369 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant) |
| 4370 | << CondExpr->getSourceRange(); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4371 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame^] | 4372 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4373 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 4374 | } |
| 4375 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4376 | return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4377 | resType, RPLoc); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4380 | //===----------------------------------------------------------------------===// |
| 4381 | // Clang Extensions. |
| 4382 | //===----------------------------------------------------------------------===// |
| 4383 | |
| 4384 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4385 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4386 | // Analyze block parameters. |
| 4387 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4388 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4389 | // Add BSI to CurBlock. |
| 4390 | BSI->PrevBlockInfo = CurBlock; |
| 4391 | CurBlock = BSI; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4392 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4393 | BSI->ReturnType = 0; |
| 4394 | BSI->TheScope = BlockScope; |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4395 | BSI->hasBlockDeclRefExprs = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4396 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4397 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4398 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4401 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| 4402 | assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); |
| 4403 | |
| 4404 | if (ParamInfo.getNumTypeObjects() == 0 |
| 4405 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
| 4406 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4407 | |
| 4408 | // The type is entirely optional as well, if none, use DependentTy. |
| 4409 | if (T.isNull()) |
| 4410 | T = Context.DependentTy; |
| 4411 | |
| 4412 | // The parameter list is optional, if there was none, assume (). |
| 4413 | if (!T->isFunctionType()) |
| 4414 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 4415 | |
| 4416 | CurBlock->hasPrototype = true; |
| 4417 | CurBlock->isVariadic = false; |
| 4418 | Type *RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4419 | .getTypePtr(); |
| 4420 | |
| 4421 | if (!RetTy->isDependentType()) |
| 4422 | CurBlock->ReturnType = RetTy; |
| 4423 | return; |
| 4424 | } |
| 4425 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4426 | // Analyze arguments to block. |
| 4427 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4428 | "Not a function declarator!"); |
| 4429 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4430 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4431 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4432 | CurBlock->isVariadic = true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4433 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4434 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4435 | // no arguments, not a function that takes a single void argument. |
| 4436 | if (FTI.hasPrototype && |
| 4437 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4438 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 4439 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 4440 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4441 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4442 | } else if (FTI.hasPrototype) { |
| 4443 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4444 | CurBlock->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 4445 | CurBlock->isVariadic = FTI.isVariadic; |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4446 | QualType T = GetTypeForDeclarator (ParamInfo, CurScope); |
| 4447 | |
| 4448 | Type* RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4449 | .getTypePtr(); |
| 4450 | |
| 4451 | if (!RetTy->isDependentType()) |
| 4452 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4453 | } |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4454 | CurBlock->TheDecl->setArgs(&CurBlock->Params[0], CurBlock->Params.size()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4455 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4456 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4457 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4458 | // If this has an identifier, add it to the scope stack. |
| 4459 | if ((*AI)->getIdentifier()) |
| 4460 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4464 | /// is invoked to pop the information about the block from the action impl. |
| 4465 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4466 | // Ensure that CurBlock is deleted. |
| 4467 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4468 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4469 | // Pop off CurBlock, handle nested blocks. |
| 4470 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4471 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4472 | // FIXME: Delete the ParmVarDecl objects as well??? |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4473 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4477 | /// literal was successfully completed. ^(int x){...} |
| 4478 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 4479 | Scope *CurScope) { |
| 4480 | // Ensure that CurBlock is deleted. |
| 4481 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4482 | ExprOwningPtr<CompoundStmt> Body(this, static_cast<CompoundStmt*>(body)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4483 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4484 | PopDeclContext(); |
| 4485 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4486 | // Pop off CurBlock, handle nested blocks. |
| 4487 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4488 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4489 | QualType RetTy = Context.VoidTy; |
| 4490 | if (BSI->ReturnType) |
| 4491 | RetTy = QualType(BSI->ReturnType, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4492 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4493 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4494 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4495 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4496 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4497 | QualType BlockTy; |
| 4498 | if (!BSI->hasPrototype) |
| 4499 | BlockTy = Context.getFunctionTypeNoProto(RetTy); |
| 4500 | else |
| 4501 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4502 | BSI->isVariadic, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4503 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4504 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4505 | |
Steve Naroff | 95029d9 | 2008-10-08 18:44:00 +0000 | [diff] [blame] | 4506 | BSI->TheDecl->setBody(Body.take()); |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4507 | return new (Context) BlockExpr(BSI->TheDecl, BlockTy, BSI->hasBlockDeclRefExprs); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4510 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4511 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4512 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4513 | Expr *E = static_cast<Expr*>(expr); |
| 4514 | QualType T = QualType::getFromOpaquePtr(type); |
| 4515 | |
| 4516 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4517 | |
| 4518 | // Get the va_list type |
| 4519 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4520 | // Deal with implicit array decay; for example, on x86-64, |
| 4521 | // va_list is an array, but it's supposed to decay to |
| 4522 | // a pointer for va_arg. |
| 4523 | if (VaListType->isArrayType()) |
| 4524 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 8754e5b | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4525 | // Make sure the input expression also decays appropriately. |
| 4526 | UsualUnaryConversions(E); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4527 | |
| 4528 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4529 | return Diag(E->getLocStart(), |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4530 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4531 | << E->getType() << E->getSourceRange(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4532 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4533 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4534 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4535 | return new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), RPLoc); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4538 | Sema::ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| 4539 | // The type of __null will be int or long, depending on the size of |
| 4540 | // pointers on the target. |
| 4541 | QualType Ty; |
| 4542 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4543 | Ty = Context.IntTy; |
| 4544 | else |
| 4545 | Ty = Context.LongTy; |
| 4546 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4547 | return new (Context) GNUNullExpr(Ty, TokenLoc); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4550 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4551 | SourceLocation Loc, |
| 4552 | QualType DstType, QualType SrcType, |
| 4553 | Expr *SrcExpr, const char *Flavor) { |
| 4554 | // Decode the result (notice that AST's are still created for extensions). |
| 4555 | bool isInvalid = false; |
| 4556 | unsigned DiagKind; |
| 4557 | switch (ConvTy) { |
| 4558 | default: assert(0 && "Unknown conversion type"); |
| 4559 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4560 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4561 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4562 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4563 | case IntToPointer: |
| 4564 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4565 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4566 | case IncompatiblePointer: |
| 4567 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4568 | break; |
| 4569 | case FunctionVoidPointer: |
| 4570 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4571 | break; |
| 4572 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4573 | // If the qualifiers lost were because we were applying the |
| 4574 | // (deprecated) C++ conversion from a string literal to a char* |
| 4575 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4576 | // Ideally, this check would be performed in |
| 4577 | // CheckPointerTypesForAssignment. However, that would require a |
| 4578 | // bit of refactoring (so that the second argument is an |
| 4579 | // expression, rather than a type), which should be done as part |
| 4580 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4581 | // C++ semantics. |
| 4582 | if (getLangOptions().CPlusPlus && |
| 4583 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4584 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4585 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4586 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4587 | case IntToBlockPointer: |
| 4588 | DiagKind = diag::err_int_to_block_pointer; |
| 4589 | break; |
| 4590 | case IncompatibleBlockPointer: |
Steve Naroff | 82324d6 | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4591 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4592 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4593 | case IncompatibleObjCQualifiedId: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4594 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4595 | // it can give a more specific diagnostic. |
| 4596 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4597 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4598 | case IncompatibleVectors: |
| 4599 | DiagKind = diag::warn_incompatible_vectors; |
| 4600 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4601 | case Incompatible: |
| 4602 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4603 | isInvalid = true; |
| 4604 | break; |
| 4605 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4606 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4607 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4608 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4609 | return isInvalid; |
| 4610 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4611 | |
| 4612 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4613 | { |
| 4614 | Expr::EvalResult EvalResult; |
| 4615 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4616 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4617 | EvalResult.HasSideEffects) { |
| 4618 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4619 | |
| 4620 | if (EvalResult.Diag) { |
| 4621 | // We only show the note if it's not the usual "invalid subexpression" |
| 4622 | // or if it's actually in a subexpression. |
| 4623 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4624 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4625 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4626 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4627 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4628 | return true; |
| 4629 | } |
| 4630 | |
| 4631 | if (EvalResult.Diag) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4632 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4633 | E->getSourceRange(); |
| 4634 | |
| 4635 | // Print the reason it's not a constant. |
| 4636 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4637 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4638 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4639 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4640 | if (Result) |
| 4641 | *Result = EvalResult.Val.getInt(); |
| 4642 | return false; |
| 4643 | } |