Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Lex/LiteralSupport.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 24 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Standard Promotions and Conversions |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 32 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 33 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 34 | QualType Ty = E->getType(); |
| 35 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 36 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 37 | if (Ty->isFunctionType()) |
| 38 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 39 | else if (Ty->isArrayType()) { |
| 40 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 41 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 42 | // type 'array of type' is converted to an expression that has type 'pointer |
| 43 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 44 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 45 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 46 | // |
| 47 | // C++ 4.2p1: |
| 48 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 49 | // T" can be converted to an rvalue of type "pointer to T". |
| 50 | // |
| 51 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 52 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 53 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 54 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 58 | /// operators (C99 6.3). The conversions of array and function types are |
| 59 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 60 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 61 | /// In these instances, this routine should *not* be called. |
| 62 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 63 | QualType Ty = Expr->getType(); |
| 64 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 65 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 66 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 67 | ImpCastExprToType(Expr, Context.IntTy); |
| 68 | else |
| 69 | DefaultFunctionArrayConversion(Expr); |
| 70 | |
| 71 | return Expr; |
| 72 | } |
| 73 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 74 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 75 | /// do not have a prototype. Arguments that have type float are promoted to |
| 76 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 77 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 78 | QualType Ty = Expr->getType(); |
| 79 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 80 | |
| 81 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 82 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 83 | if (BT->getKind() == BuiltinType::Float) |
| 84 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 85 | |
| 86 | UsualUnaryConversions(Expr); |
| 87 | } |
| 88 | |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 89 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 90 | // will warn if the resulting type is not a POD type. |
| 91 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) |
| 92 | |
| 93 | { |
| 94 | DefaultArgumentPromotion(Expr); |
| 95 | |
| 96 | if (!Expr->getType()->isPODType()) { |
| 97 | Diag(Expr->getLocStart(), |
| 98 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 99 | Expr->getType() << CT; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 104 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 105 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 106 | /// routine returns the first non-arithmetic type found. The client is |
| 107 | /// responsible for emitting appropriate error diagnostics. |
| 108 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 109 | /// GCC. |
| 110 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 111 | bool isCompAssign) { |
| 112 | if (!isCompAssign) { |
| 113 | UsualUnaryConversions(lhsExpr); |
| 114 | UsualUnaryConversions(rhsExpr); |
| 115 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 116 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 117 | // For conversion purposes, we ignore any qualifiers. |
| 118 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 119 | QualType lhs = |
| 120 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 121 | QualType rhs = |
| 122 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 123 | |
| 124 | // If both types are identical, no conversion is needed. |
| 125 | if (lhs == rhs) |
| 126 | return lhs; |
| 127 | |
| 128 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 129 | // The caller can deal with this (e.g. pointer + int). |
| 130 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 131 | return lhs; |
| 132 | |
| 133 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
| 134 | if (!isCompAssign) { |
| 135 | ImpCastExprToType(lhsExpr, destType); |
| 136 | ImpCastExprToType(rhsExpr, destType); |
| 137 | } |
| 138 | return destType; |
| 139 | } |
| 140 | |
| 141 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 142 | // Perform the usual unary conversions. We do this early so that |
| 143 | // integral promotions to "int" can allow us to exit early, in the |
| 144 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 145 | // qualifiers. For example, "const float" and "float" are |
| 146 | // equivalent. |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 147 | if (lhs->isPromotableIntegerType()) lhs = Context.IntTy; |
| 148 | else lhs = lhs.getUnqualifiedType(); |
| 149 | if (rhs->isPromotableIntegerType()) rhs = Context.IntTy; |
| 150 | else rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 151 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 152 | // If both types are identical, no conversion is needed. |
| 153 | if (lhs == rhs) |
| 154 | return lhs; |
| 155 | |
| 156 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 157 | // The caller can deal with this (e.g. pointer + int). |
| 158 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 159 | return lhs; |
| 160 | |
| 161 | // At this point, we have two different arithmetic types. |
| 162 | |
| 163 | // Handle complex types first (C99 6.3.1.8p1). |
| 164 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 165 | // if we have an integer operand, the result is the complex type. |
| 166 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 167 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 168 | return lhs; |
| 169 | } |
| 170 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 171 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 172 | return rhs; |
| 173 | } |
| 174 | // This handles complex/complex, complex/float, or float/complex. |
| 175 | // When both operands are complex, the shorter operand is converted to the |
| 176 | // type of the longer, and that is the type of the result. This corresponds |
| 177 | // to what is done when combining two real floating-point operands. |
| 178 | // The fun begins when size promotion occur across type domains. |
| 179 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 180 | // floating-point type, the less precise type is converted, within it's |
| 181 | // real or complex domain, to the precision of the other type. For example, |
| 182 | // when combining a "long double" with a "double _Complex", the |
| 183 | // "double _Complex" is promoted to "long double _Complex". |
| 184 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 185 | |
| 186 | if (result > 0) { // The left side is bigger, convert rhs. |
| 187 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 188 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 189 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 190 | } |
| 191 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 192 | // domains match. This is a requirement for our implementation, C99 |
| 193 | // does not require this promotion. |
| 194 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 195 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 196 | return rhs; |
| 197 | } else { // handle "_Complex double, double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 198 | return lhs; |
| 199 | } |
| 200 | } |
| 201 | return lhs; // The domain/size match exactly. |
| 202 | } |
| 203 | // Now handle "real" floating types (i.e. float, double, long double). |
| 204 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 205 | // if we have an integer operand, the result is the real floating type. |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 206 | if (rhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 207 | // convert rhs to the lhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 208 | return lhs; |
| 209 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 210 | if (rhs->isComplexIntegerType()) { |
| 211 | // convert rhs to the complex floating point type. |
| 212 | return Context.getComplexType(lhs); |
| 213 | } |
| 214 | if (lhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 215 | // convert lhs to the rhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 216 | return rhs; |
| 217 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 218 | if (lhs->isComplexIntegerType()) { |
| 219 | // convert lhs to the complex floating point type. |
| 220 | return Context.getComplexType(rhs); |
| 221 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 222 | // We have two real floating types, float/complex combos were handled above. |
| 223 | // Convert the smaller operand to the bigger result. |
| 224 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 225 | |
| 226 | if (result > 0) { // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 227 | return lhs; |
| 228 | } |
| 229 | if (result < 0) { // convert the lhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 230 | return rhs; |
| 231 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 232 | assert(0 && "Sema::UsualArithmeticConversionsType(): illegal float comparison"); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 233 | } |
| 234 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 235 | // Handle GCC complex int extension. |
| 236 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 237 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 238 | |
| 239 | if (lhsComplexInt && rhsComplexInt) { |
| 240 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 241 | rhsComplexInt->getElementType()) >= 0) { |
| 242 | // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 243 | return lhs; |
| 244 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 245 | return rhs; |
| 246 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 247 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 248 | return lhs; |
| 249 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 250 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 251 | return rhs; |
| 252 | } |
| 253 | } |
| 254 | // Finally, we have two differing integer types. |
| 255 | // The rules for this case are in C99 6.3.1.8 |
| 256 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 257 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 258 | rhsSigned = rhs->isSignedIntegerType(); |
| 259 | QualType destType; |
| 260 | if (lhsSigned == rhsSigned) { |
| 261 | // Same signedness; use the higher-ranked type |
| 262 | destType = compare >= 0 ? lhs : rhs; |
| 263 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 264 | // The unsigned type has greater than or equal rank to the |
| 265 | // signed type, so use the unsigned type |
| 266 | destType = lhsSigned ? rhs : lhs; |
| 267 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 268 | // The two types are different widths; if we are here, that |
| 269 | // means the signed type is larger than the unsigned type, so |
| 270 | // use the signed type. |
| 271 | destType = lhsSigned ? lhs : rhs; |
| 272 | } else { |
| 273 | // The signed type is higher-ranked than the unsigned type, |
| 274 | // but isn't actually any bigger (like unsigned int and long |
| 275 | // on most 32-bit systems). Use the unsigned type corresponding |
| 276 | // to the signed type. |
| 277 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 278 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | return destType; |
| 280 | } |
| 281 | |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | // Semantic Analysis for various Expression Types |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | |
| 286 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 287 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 289 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 290 | /// multiple tokens. However, the common case is that StringToks points to one |
| 291 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 292 | /// |
| 293 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 294 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | assert(NumStringToks && "Must have at least one string!"); |
| 296 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 297 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 299 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | |
| 301 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 302 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 303 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 304 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 305 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 306 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 307 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 308 | |
| 309 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 310 | if (getLangOptions().CPlusPlus) |
| 311 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 312 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 313 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 314 | // the nul terminator character as well as the string length for pascal |
| 315 | // strings. |
| 316 | StrTy = Context.getConstantArrayType(StrTy, |
| 317 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 318 | ArrayType::Normal, 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 319 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 321 | return Owned(new (Context) StringLiteral(Literal.GetString(), |
| 322 | Literal.GetStringLength(), |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 323 | Literal.AnyWide, StrTy, |
| 324 | StringToks[0].getLocation(), |
| 325 | StringToks[NumStringToks-1].getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 328 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 329 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 330 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 331 | /// for values inside the block or for globals). |
| 332 | /// |
| 333 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 334 | /// function references, etc which is suboptimal :) and breaks |
| 335 | /// things like "integer constant expression" tests. |
| 336 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 337 | ValueDecl *VD) { |
| 338 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 339 | // we wanted to. |
| 340 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 341 | return false; |
| 342 | |
| 343 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 344 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 345 | return false; |
| 346 | |
| 347 | // If this is a reference to an extern, static, or global variable, no need to |
| 348 | // snapshot it. |
| 349 | // FIXME: What about 'const' variables in C++? |
| 350 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 351 | return Var->hasLocalStorage(); |
| 352 | |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 358 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | 0d755ad | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 360 | /// identifier is used in a function call context. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 361 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 362 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 363 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 364 | IdentifierInfo &II, |
| 365 | bool HasTrailingLParen, |
| 366 | const CXXScopeSpec *SS) { |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 367 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS); |
| 368 | } |
| 369 | |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 370 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 371 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 372 | /// nested-name-specifier. |
| 373 | DeclRefExpr *Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 374 | bool TypeDependent, bool ValueDependent, |
| 375 | const CXXScopeSpec *SS) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 376 | if (SS && !SS->isEmpty()) |
| 377 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 378 | ValueDependent, SS->getRange().getBegin()); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 379 | else |
| 380 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 383 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 384 | /// variable corresponding to the anonymous union or struct whose type |
| 385 | /// is Record. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 386 | static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 387 | assert(Record->isAnonymousStructOrUnion() && |
| 388 | "Record must be an anonymous struct or union!"); |
| 389 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 390 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 391 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 392 | // vector (which itself will be eliminated). DeclGroups might make |
| 393 | // this even better. |
| 394 | DeclContext *Ctx = Record->getDeclContext(); |
| 395 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 396 | DEnd = Ctx->decls_end(); |
| 397 | D != DEnd; ++D) { |
| 398 | if (*D == Record) { |
| 399 | // The object for the anonymous struct/union directly |
| 400 | // follows its type in the list of declarations. |
| 401 | ++D; |
| 402 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 403 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 404 | return *D; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | assert(false && "Missing object for anonymous record"); |
| 409 | return 0; |
| 410 | } |
| 411 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 412 | Sema::OwningExprResult |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 413 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 414 | FieldDecl *Field, |
| 415 | Expr *BaseObjectExpr, |
| 416 | SourceLocation OpLoc) { |
| 417 | assert(Field->getDeclContext()->isRecord() && |
| 418 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 419 | && "Field must be stored inside an anonymous struct or union"); |
| 420 | |
| 421 | // Construct the sequence of field member references |
| 422 | // we'll have to perform to get to the field in the anonymous |
| 423 | // union/struct. The list of members is built from the field |
| 424 | // outward, so traverse it backwards to go from an object in |
| 425 | // the current context to the field we found. |
| 426 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 427 | AnonFields.push_back(Field); |
| 428 | VarDecl *BaseObject = 0; |
| 429 | DeclContext *Ctx = Field->getDeclContext(); |
| 430 | do { |
| 431 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 432 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 433 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 434 | AnonFields.push_back(AnonField); |
| 435 | else { |
| 436 | BaseObject = cast<VarDecl>(AnonObject); |
| 437 | break; |
| 438 | } |
| 439 | Ctx = Ctx->getParent(); |
| 440 | } while (Ctx->isRecord() && |
| 441 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 442 | |
| 443 | // Build the expression that refers to the base object, from |
| 444 | // which we will build a sequence of member references to each |
| 445 | // of the anonymous union objects and, eventually, the field we |
| 446 | // found via name lookup. |
| 447 | bool BaseObjectIsPointer = false; |
| 448 | unsigned ExtraQuals = 0; |
| 449 | if (BaseObject) { |
| 450 | // BaseObject is an anonymous struct/union variable (and is, |
| 451 | // therefore, not part of another non-anonymous record). |
| 452 | delete BaseObjectExpr; |
| 453 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 454 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 455 | SourceLocation()); |
| 456 | ExtraQuals |
| 457 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 458 | } else if (BaseObjectExpr) { |
| 459 | // The caller provided the base object expression. Determine |
| 460 | // whether its a pointer and whether it adds any qualifiers to the |
| 461 | // anonymous struct/union fields we're looking into. |
| 462 | QualType ObjectType = BaseObjectExpr->getType(); |
| 463 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 464 | BaseObjectIsPointer = true; |
| 465 | ObjectType = ObjectPtr->getPointeeType(); |
| 466 | } |
| 467 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 468 | } else { |
| 469 | // We've found a member of an anonymous struct/union that is |
| 470 | // inside a non-anonymous struct/union, so in a well-formed |
| 471 | // program our base object expression is "this". |
| 472 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 473 | if (!MD->isStatic()) { |
| 474 | QualType AnonFieldType |
| 475 | = Context.getTagDeclType( |
| 476 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 477 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 478 | if ((Context.getCanonicalType(AnonFieldType) |
| 479 | == Context.getCanonicalType(ThisType)) || |
| 480 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 481 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 482 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 483 | MD->getThisType(Context)); |
| 484 | BaseObjectIsPointer = true; |
| 485 | } |
| 486 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 487 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 488 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 489 | } |
| 490 | ExtraQuals = MD->getTypeQualifiers(); |
| 491 | } |
| 492 | |
| 493 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 494 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 495 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Build the implicit member references to the field of the |
| 499 | // anonymous struct/union. |
| 500 | Expr *Result = BaseObjectExpr; |
| 501 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 502 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 503 | FI != FIEnd; ++FI) { |
| 504 | QualType MemberType = (*FI)->getType(); |
| 505 | if (!(*FI)->isMutable()) { |
| 506 | unsigned combinedQualifiers |
| 507 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 508 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 509 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 510 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 511 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 512 | BaseObjectIsPointer = false; |
| 513 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
| 514 | OpLoc = SourceLocation(); |
| 515 | } |
| 516 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 517 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 520 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 521 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 522 | /// performs lookup on that name and returns an expression that refers |
| 523 | /// to that name. This routine isn't directly called from the parser, |
| 524 | /// because the parser doesn't know about DeclarationName. Rather, |
| 525 | /// this routine is called by ActOnIdentifierExpr, |
| 526 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 527 | /// which form the DeclarationName from the corresponding syntactic |
| 528 | /// forms. |
| 529 | /// |
| 530 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 531 | /// function call context. LookupCtx is only used for a C++ |
| 532 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 533 | /// the identifier must be a member of. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 534 | /// |
| 535 | /// If ForceResolution is true, then we will attempt to resolve the |
| 536 | /// name even if it looks like a dependent name. This option is off by |
| 537 | /// default. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 538 | Sema::OwningExprResult |
| 539 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 540 | DeclarationName Name, bool HasTrailingLParen, |
| 541 | const CXXScopeSpec *SS, bool ForceResolution) { |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 542 | if (S->getTemplateParamParent() && Name.getAsIdentifierInfo() && |
| 543 | HasTrailingLParen && !SS && !ForceResolution) { |
| 544 | // We've seen something of the form |
| 545 | // identifier( |
| 546 | // and we are in a template, so it is likely that 's' is a |
| 547 | // dependent name. However, we won't know until we've parsed all |
| 548 | // of the call arguments. So, build a CXXDependentNameExpr node |
| 549 | // to represent this name. Then, if it turns out that none of the |
| 550 | // arguments are type-dependent, we'll force the resolution of the |
| 551 | // dependent name at that point. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 552 | return Owned(new (Context) CXXDependentNameExpr(Name.getAsIdentifierInfo(), |
| 553 | Context.DependentTy, Loc)); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 556 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 557 | Decl *D = 0; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 558 | if (SS && SS->isInvalid()) |
| 559 | return ExprError(); |
| 560 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 561 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 562 | if (Lookup.isAmbiguous()) { |
| 563 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 564 | SS && SS->isSet() ? SS->getRange() |
| 565 | : SourceRange()); |
| 566 | return ExprError(); |
| 567 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 568 | D = Lookup.getAsDecl(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 570 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 571 | // well. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 572 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 573 | if (II && getCurMethodDecl()) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 574 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 575 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 576 | // found a decl, but that decl is outside the current method (i.e. a global |
| 577 | // variable). In these two cases, we do a lookup for an ivar with this |
| 578 | // name, if the lookup suceeds, we replace it our current decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 579 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 580 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 581 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II)) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 582 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 583 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 584 | IdentifierInfo &II = Context.Idents.get("self"); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 585 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 586 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 587 | Loc, static_cast<Expr*>(SelfExpr.release()), |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 588 | true, true); |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 589 | Context.setFieldDecl(IFace, IV, MRef); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 590 | return Owned(MRef); |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
Steve Naroff | 76de9d7 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 593 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 594 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 595 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 596 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 597 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 598 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 599 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 600 | if (D == 0) { |
| 601 | // Otherwise, this could be an implicitly declared function reference (legal |
| 602 | // in C90, extension in C99). |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 603 | if (HasTrailingLParen && II && |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 604 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 605 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | else { |
| 607 | // If this name wasn't predeclared and if this is not a function call, |
| 608 | // diagnose the problem. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 609 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 610 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 611 | << Name << SS->getRange()); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 612 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 613 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 614 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 615 | << Name.getAsString()); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 616 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 617 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 620 | |
| 621 | // We may have found a field within an anonymous union or struct |
| 622 | // (C++ [class.union]). |
| 623 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 624 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 625 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 626 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 627 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 628 | if (!MD->isStatic()) { |
| 629 | // C++ [class.mfct.nonstatic]p2: |
| 630 | // [...] if name lookup (3.4.1) resolves the name in the |
| 631 | // id-expression to a nonstatic nontype member of class X or of |
| 632 | // a base class of X, the id-expression is transformed into a |
| 633 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 634 | // as the postfix-expression to the left of the '.' operator. |
| 635 | DeclContext *Ctx = 0; |
| 636 | QualType MemberType; |
| 637 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 638 | Ctx = FD->getDeclContext(); |
| 639 | MemberType = FD->getType(); |
| 640 | |
| 641 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 642 | MemberType = RefType->getPointeeType(); |
| 643 | else if (!FD->isMutable()) { |
| 644 | unsigned combinedQualifiers |
| 645 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 646 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 647 | } |
| 648 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 649 | if (!Method->isStatic()) { |
| 650 | Ctx = Method->getParent(); |
| 651 | MemberType = Method->getType(); |
| 652 | } |
| 653 | } else if (OverloadedFunctionDecl *Ovl |
| 654 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 655 | for (OverloadedFunctionDecl::function_iterator |
| 656 | Func = Ovl->function_begin(), |
| 657 | FuncEnd = Ovl->function_end(); |
| 658 | Func != FuncEnd; ++Func) { |
| 659 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 660 | if (!DMethod->isStatic()) { |
| 661 | Ctx = Ovl->getDeclContext(); |
| 662 | MemberType = Context.OverloadTy; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 667 | |
| 668 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 669 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 670 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 671 | if ((Context.getCanonicalType(CtxType) |
| 672 | == Context.getCanonicalType(ThisType)) || |
| 673 | IsDerivedFrom(ThisType, CtxType)) { |
| 674 | // Build the implicit member access expression. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 675 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 676 | MD->getThisType(Context)); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 677 | return Owned(new (Context) MemberExpr(This, true, cast<NamedDecl>(D), |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 678 | SourceLocation(), MemberType)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 684 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 685 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 686 | if (MD->isStatic()) |
| 687 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 688 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 689 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 692 | // Any other ways we could have found the field in a well-formed |
| 693 | // program would have been turned into implicit member expressions |
| 694 | // above. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 695 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 696 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 697 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 700 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 701 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 702 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 703 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 704 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 706 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 707 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 708 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 709 | false, false, SS)); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 710 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 711 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 712 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 713 | // check if referencing an identifier with __attribute__((deprecated)). |
| 714 | if (VD->getAttr<DeprecatedAttr>()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 715 | ExprError(Diag(Loc, diag::warn_deprecated) << VD->getDeclName()); |
| 716 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 717 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
| 718 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 719 | Scope *CheckS = S; |
| 720 | while (CheckS) { |
| 721 | if (CheckS->isWithinElse() && |
| 722 | CheckS->getControlParent()->isDeclScope(Var)) { |
| 723 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 724 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 725 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 726 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 727 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 728 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 729 | break; |
| 730 | } |
| 731 | |
| 732 | // Move up one more control parent to check again. |
| 733 | CheckS = CheckS->getControlParent(); |
| 734 | if (CheckS) |
| 735 | CheckS = CheckS->getParent(); |
| 736 | } |
| 737 | } |
| 738 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 739 | |
| 740 | // Only create DeclRefExpr's for valid Decl's. |
| 741 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 742 | return ExprError(); |
| 743 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 744 | // If the identifier reference is inside a block, and it refers to a value |
| 745 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 746 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 747 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 748 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 749 | // We do not do this for things like enum constants, global variables, etc, |
| 750 | // as they do not get snapshotted. |
| 751 | // |
| 752 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 753 | // The BlocksAttr indicates the variable is bound by-reference. |
| 754 | if (VD->getAttr<BlocksAttr>()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 755 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 756 | VD->getType().getNonReferenceType(), Loc, true)); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 757 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 758 | // Variable will be bound by-copy, make it const within the closure. |
| 759 | VD->getType().addConst(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 760 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 761 | VD->getType().getNonReferenceType(), Loc, false)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 762 | } |
| 763 | // If this reference is not in a block or if the referenced variable is |
| 764 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 766 | bool TypeDependent = false; |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 767 | bool ValueDependent = false; |
| 768 | if (getLangOptions().CPlusPlus) { |
| 769 | // C++ [temp.dep.expr]p3: |
| 770 | // An id-expression is type-dependent if it contains: |
| 771 | // - an identifier that was declared with a dependent type, |
| 772 | if (VD->getType()->isDependentType()) |
| 773 | TypeDependent = true; |
| 774 | // - FIXME: a template-id that is dependent, |
| 775 | // - a conversion-function-id that specifies a dependent type, |
| 776 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 777 | Name.getCXXNameType()->isDependentType()) |
| 778 | TypeDependent = true; |
| 779 | // - a nested-name-specifier that contains a class-name that |
| 780 | // names a dependent type. |
| 781 | else if (SS && !SS->isEmpty()) { |
| 782 | for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 783 | DC; DC = DC->getParent()) { |
| 784 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 785 | if (DC->isRecord()) { |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 786 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 787 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 788 | TypeDependent = true; |
| 789 | break; |
| 790 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 795 | // C++ [temp.dep.constexpr]p2: |
| 796 | // |
| 797 | // An identifier is value-dependent if it is: |
| 798 | // - a name declared with a dependent type, |
| 799 | if (TypeDependent) |
| 800 | ValueDependent = true; |
| 801 | // - the name of a non-type template parameter, |
| 802 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 803 | ValueDependent = true; |
| 804 | // - a constant with integral or enumeration type and is |
| 805 | // initialized with an expression that is value-dependent |
| 806 | // (FIXME!). |
| 807 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 808 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 809 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 810 | TypeDependent, ValueDependent, SS)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 813 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 814 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 815 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 816 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 817 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 818 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 819 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 820 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 821 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 823 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 824 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 825 | // string. |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 826 | unsigned Length; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 827 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 828 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 829 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 830 | Length = MD->getSynthesizedMethodSize(); |
| 831 | else { |
| 832 | Diag(Loc, diag::ext_predef_outside_function); |
| 833 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 834 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 835 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 836 | |
| 837 | |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 838 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 839 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 840 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 841 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 844 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 845 | llvm::SmallString<16> CharBuffer; |
| 846 | CharBuffer.resize(Tok.getLength()); |
| 847 | const char *ThisTokBegin = &CharBuffer[0]; |
| 848 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 849 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 850 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 851 | Tok.getLocation(), PP); |
| 852 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 853 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 854 | |
| 855 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 856 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 857 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 858 | Literal.isWide(), |
| 859 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 862 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 863 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 864 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 865 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 866 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 867 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 868 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 869 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 871 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 872 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 873 | // Add padding so that NumericLiteralParser can overread by one character. |
| 874 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 876 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 877 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 878 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 881 | Tok.getLocation(), PP); |
| 882 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 883 | return ExprError(); |
| 884 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 885 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 887 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 888 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 889 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 890 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 891 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 892 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 893 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 894 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 895 | |
| 896 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 897 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 898 | // isExact will be set by GetFloatValue(). |
| 899 | bool isExact = false; |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 900 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 901 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 902 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 903 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 904 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 905 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 906 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 908 | // long long is a C99 feature. |
| 909 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 910 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 911 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 912 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 913 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 914 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 916 | if (Literal.GetIntegerValue(ResultVal)) { |
| 917 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 918 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 919 | Ty = Context.UnsignedLongLongTy; |
| 920 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 921 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 922 | } else { |
| 923 | // If this value fits into a ULL, try to figure out what else it fits into |
| 924 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 925 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 926 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 927 | // be an unsigned int. |
| 928 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 929 | |
| 930 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 931 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 932 | if (!Literal.isLong && !Literal.isLongLong) { |
| 933 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 934 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 935 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 936 | // Does it fit in a unsigned int? |
| 937 | if (ResultVal.isIntN(IntSize)) { |
| 938 | // Does it fit in a signed int? |
| 939 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 940 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 942 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 943 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 944 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 945 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 946 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 947 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 948 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 949 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 950 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | // Does it fit in a unsigned long? |
| 952 | if (ResultVal.isIntN(LongSize)) { |
| 953 | // Does it fit in a signed long? |
| 954 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 955 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 957 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 958 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 962 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 963 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 964 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 965 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | // Does it fit in a unsigned long long? |
| 967 | if (ResultVal.isIntN(LongLongSize)) { |
| 968 | // Does it fit in a signed long long? |
| 969 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 970 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 971 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 972 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 973 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 976 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | // If we still couldn't decide a type, we probably have something that |
| 978 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 979 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 981 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 982 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 985 | if (ResultVal.getBitWidth() != Width) |
| 986 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 988 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 991 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 992 | if (Literal.isImaginary) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 993 | Res = new (Context) ImaginaryLiteral(Res, |
| 994 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 995 | |
| 996 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 999 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1000 | SourceLocation R, ExprArg Val) { |
| 1001 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1002 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1003 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1007 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1008 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 1009 | SourceLocation OpLoc, |
| 1010 | const SourceRange &ExprRange, |
| 1011 | bool isSizeof) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1012 | // C99 6.5.3.4p1: |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1013 | if (isa<FunctionType>(exprType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | // alignof(function) is allowed. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1015 | if (isSizeof) |
| 1016 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1021 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1022 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1026 | return DiagnoseIncompleteType(OpLoc, exprType, |
| 1027 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1028 | diag::err_alignof_incomplete_type, |
| 1029 | ExprRange); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1032 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1033 | const SourceRange &ExprRange) { |
| 1034 | E = E->IgnoreParens(); |
| 1035 | |
| 1036 | // alignof decl is always ok. |
| 1037 | if (isa<DeclRefExpr>(E)) |
| 1038 | return false; |
| 1039 | |
| 1040 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1041 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1042 | if (FD->isBitField()) { |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1043 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1044 | return true; |
| 1045 | } |
| 1046 | // Other fields are ok. |
| 1047 | return false; |
| 1048 | } |
| 1049 | } |
| 1050 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1051 | } |
| 1052 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1053 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1054 | /// the same for @c alignof and @c __alignof |
| 1055 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1056 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1057 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1058 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1060 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1062 | QualType ArgTy; |
| 1063 | SourceRange Range; |
| 1064 | if (isType) { |
| 1065 | ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1066 | Range = ArgRange; |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1067 | |
| 1068 | // Verify that the operand is valid. |
| 1069 | if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) |
| 1070 | return ExprError(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | // Get the end location. |
| 1073 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1074 | Range = ArgEx->getSourceRange(); |
| 1075 | ArgTy = ArgEx->getType(); |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Verify that the operand is valid. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1078 | bool isInvalid; |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1079 | if (!isSizeof) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1080 | isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range); |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1081 | } else if (ArgEx->isBitField()) { // C99 6.5.3.4p1. |
| 1082 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1083 | isInvalid = true; |
| 1084 | } else { |
| 1085 | isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true); |
| 1086 | } |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (isInvalid) { |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1089 | DeleteExpr(ArgEx); |
| 1090 | return ExprError(); |
| 1091 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1094 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1095 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeof, isType, TyOrEx, |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1096 | Context.getSizeType(), OpLoc, |
| 1097 | Range.getEnd())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 1100 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) { |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1101 | DefaultFunctionArrayConversion(V); |
| 1102 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1103 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1104 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1105 | return CT->getElementType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Otherwise they pass through real integer and floating point types here. |
| 1108 | if (V->getType()->isArithmeticType()) |
| 1109 | return V->getType(); |
| 1110 | |
| 1111 | // Reject anything else. |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1112 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1113 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1117 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1118 | Action::OwningExprResult |
| 1119 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1120 | tok::TokenKind Kind, ExprArg Input) { |
| 1121 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | UnaryOperator::Opcode Opc; |
| 1124 | switch (Kind) { |
| 1125 | default: assert(0 && "Unknown unary op!"); |
| 1126 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1127 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1128 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1130 | if (getLangOptions().CPlusPlus && |
| 1131 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1132 | // Which overloaded operator? |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1133 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1134 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1135 | |
| 1136 | // C++ [over.inc]p1: |
| 1137 | // |
| 1138 | // [...] If the function is a member function with one |
| 1139 | // parameter (which shall be of type int) or a non-member |
| 1140 | // function with two parameters (the second of which shall be |
| 1141 | // of type int), it defines the postfix increment operator ++ |
| 1142 | // for objects of that type. When the postfix increment is |
| 1143 | // called as a result of using the ++ operator, the int |
| 1144 | // argument will have value zero. |
| 1145 | Expr *Args[2] = { |
| 1146 | Arg, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1147 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1148 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1149 | }; |
| 1150 | |
| 1151 | // Build the candidate set for overloading |
| 1152 | OverloadCandidateSet CandidateSet; |
| 1153 | AddOperatorCandidates(OverOp, S, Args, 2, CandidateSet); |
| 1154 | |
| 1155 | // Perform overload resolution. |
| 1156 | OverloadCandidateSet::iterator Best; |
| 1157 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1158 | case OR_Success: { |
| 1159 | // We found a built-in operator or an overloaded operator. |
| 1160 | FunctionDecl *FnDecl = Best->Function; |
| 1161 | |
| 1162 | if (FnDecl) { |
| 1163 | // We matched an overloaded operator. Build a call to that |
| 1164 | // operator. |
| 1165 | |
| 1166 | // Convert the arguments. |
| 1167 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1168 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1169 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | // Convert the arguments. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1172 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1173 | FnDecl->getParamDecl(0)->getType(), |
| 1174 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1175 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1179 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1180 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1181 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1183 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1184 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1185 | SourceLocation()); |
| 1186 | UsualUnaryConversions(FnExpr); |
| 1187 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1188 | Input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1189 | return Owned(new (Context)CXXOperatorCallExpr(FnExpr, Args, 2, ResultTy, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1190 | OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1191 | } else { |
| 1192 | // We matched a built-in operator. Convert the arguments, then |
| 1193 | // break out so that we will build the appropriate built-in |
| 1194 | // operator node. |
| 1195 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1196 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1197 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1198 | |
| 1199 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1200 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | case OR_No_Viable_Function: |
| 1204 | // No viable function; fall through to handling this as a |
| 1205 | // built-in operator, which will produce an error message for us. |
| 1206 | break; |
| 1207 | |
| 1208 | case OR_Ambiguous: |
| 1209 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1210 | << UnaryOperator::getOpcodeStr(Opc) |
| 1211 | << Arg->getSourceRange(); |
| 1212 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1213 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | // Either we found no viable overloaded operator or we matched a |
| 1217 | // built-in operator. In either case, fall through to trying to |
| 1218 | // build a built-in operation. |
| 1219 | } |
| 1220 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1221 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1222 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1223 | if (result.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1224 | return ExprError(); |
| 1225 | Input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1226 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1229 | Action::OwningExprResult |
| 1230 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1231 | ExprArg Idx, SourceLocation RLoc) { |
| 1232 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1233 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1235 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1236 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1237 | LHSExp->getType()->isEnumeralType() || |
| 1238 | RHSExp->getType()->isRecordType() || |
| 1239 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1240 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1241 | // to the candidate set. |
| 1242 | OverloadCandidateSet CandidateSet; |
| 1243 | Expr *Args[2] = { LHSExp, RHSExp }; |
| 1244 | AddOperatorCandidates(OO_Subscript, S, Args, 2, CandidateSet); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1246 | // Perform overload resolution. |
| 1247 | OverloadCandidateSet::iterator Best; |
| 1248 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1249 | case OR_Success: { |
| 1250 | // We found a built-in operator or an overloaded operator. |
| 1251 | FunctionDecl *FnDecl = Best->Function; |
| 1252 | |
| 1253 | if (FnDecl) { |
| 1254 | // We matched an overloaded operator. Build a call to that |
| 1255 | // operator. |
| 1256 | |
| 1257 | // Convert the arguments. |
| 1258 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1259 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1260 | PerformCopyInitialization(RHSExp, |
| 1261 | FnDecl->getParamDecl(0)->getType(), |
| 1262 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1263 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1264 | } else { |
| 1265 | // Convert the arguments. |
| 1266 | if (PerformCopyInitialization(LHSExp, |
| 1267 | FnDecl->getParamDecl(0)->getType(), |
| 1268 | "passing") || |
| 1269 | PerformCopyInitialization(RHSExp, |
| 1270 | FnDecl->getParamDecl(1)->getType(), |
| 1271 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1272 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1276 | QualType ResultTy |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1277 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1278 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1279 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1280 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1281 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1282 | SourceLocation()); |
| 1283 | UsualUnaryConversions(FnExpr); |
| 1284 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1285 | Base.release(); |
| 1286 | Idx.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1287 | return Owned(new (Context) CXXOperatorCallExpr(FnExpr, Args, 2, |
| 1288 | ResultTy, LLoc)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1289 | } else { |
| 1290 | // We matched a built-in operator. Convert the arguments, then |
| 1291 | // break out so that we will build the appropriate built-in |
| 1292 | // operator node. |
| 1293 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1294 | "passing") || |
| 1295 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1296 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1297 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1298 | |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | case OR_No_Viable_Function: |
| 1304 | // No viable function; fall through to handling this as a |
| 1305 | // built-in operator, which will produce an error message for us. |
| 1306 | break; |
| 1307 | |
| 1308 | case OR_Ambiguous: |
| 1309 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1310 | << "[]" |
| 1311 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1312 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1313 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | // Either we found no viable overloaded operator or we matched a |
| 1317 | // built-in operator. In either case, fall through to trying to |
| 1318 | // build a built-in operation. |
| 1319 | } |
| 1320 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1321 | // Perform default conversions. |
| 1322 | DefaultFunctionArrayConversion(LHSExp); |
| 1323 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1325 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1328 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | // in the subscript position. As a result, we need to derive the array base |
| 1330 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1331 | Expr *BaseExpr, *IndexExpr; |
| 1332 | QualType ResultType; |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1333 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1334 | BaseExpr = LHSExp; |
| 1335 | IndexExpr = RHSExp; |
| 1336 | // FIXME: need to deal with const... |
| 1337 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1338 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1339 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1340 | BaseExpr = RHSExp; |
| 1341 | IndexExpr = LHSExp; |
| 1342 | // FIXME: need to deal with const... |
| 1343 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1344 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1345 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1346 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1348 | // FIXME: need to deal with const... |
| 1349 | ResultType = VTy->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1350 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1351 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1352 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1353 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | // C99 6.5.2.1p1 |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1355 | if (!IndexExpr->getType()->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1356 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1357 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1359 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 1360 | // the following check catches trying to index a pointer to a function (e.g. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1361 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 1362 | // objects in C99. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1363 | if (!ResultType->isObjectType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1364 | return ExprError(Diag(BaseExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1365 | diag::err_typecheck_subscript_not_object) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1366 | << BaseExpr->getType() << BaseExpr->getSourceRange()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1367 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1368 | Base.release(); |
| 1369 | Idx.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1370 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1371 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1374 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1375 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1376 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1377 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1378 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1379 | // The vector accessor can't exceed the number of elements. |
| 1380 | const char *compStr = CompName.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1381 | |
| 1382 | // This flag determines whether or not the component is one of the four |
| 1383 | // special names that indicate a subset of exactly half the elements are |
| 1384 | // to be selected. |
| 1385 | bool HalvingSwizzle = false; |
| 1386 | |
| 1387 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1388 | // indicating that it is a string of hex values to be used as vector indices. |
| 1389 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Check that we've found one of the special components, or that the component |
| 1392 | // names must come from the same set. |
| 1393 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1394 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1395 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1396 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1397 | do |
| 1398 | compStr++; |
| 1399 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1400 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1401 | do |
| 1402 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1403 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1404 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1407 | // We didn't get to the end of the string. This means the component names |
| 1408 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1409 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1410 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1411 | return QualType(); |
| 1412 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Ensure no component accessor exceeds the width of the vector type it |
| 1415 | // operates on. |
| 1416 | if (!HalvingSwizzle) { |
| 1417 | compStr = CompName.getName(); |
| 1418 | |
| 1419 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1420 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1421 | |
| 1422 | while (*compStr) { |
| 1423 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1424 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1425 | << baseType << SourceRange(CompLoc); |
| 1426 | return QualType(); |
| 1427 | } |
| 1428 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1429 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1430 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1431 | // If this is a halving swizzle, verify that the base type has an even |
| 1432 | // number of elements. |
| 1433 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1434 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1435 | << baseType << SourceRange(CompLoc); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1436 | return QualType(); |
| 1437 | } |
| 1438 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1439 | // The component accessor looks fine - now we need to compute the actual type. |
| 1440 | // The vector type is implied by the component accessor. For example, |
| 1441 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1442 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1443 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1444 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1445 | : CompName.getLength(); |
| 1446 | if (HexSwizzle) |
| 1447 | CompSize--; |
| 1448 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1449 | if (CompSize == 1) |
| 1450 | return vecType->getElementType(); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1451 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1452 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1453 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1454 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1455 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1456 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1457 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1458 | } |
| 1459 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1462 | /// constructSetterName - Return the setter name for the given |
| 1463 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 1464 | /// has been capitalized. |
| 1465 | // FIXME: Merge with same routine in Parser. But where should this |
| 1466 | // live? |
| 1467 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 1468 | const IdentifierInfo *Name) { |
| 1469 | llvm::SmallString<100> SelectorName; |
| 1470 | SelectorName = "set"; |
| 1471 | SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); |
| 1472 | SelectorName[3] = toupper(SelectorName[3]); |
| 1473 | return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); |
| 1474 | } |
| 1475 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1476 | Action::OwningExprResult |
| 1477 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1478 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 1479 | IdentifierInfo &Member) { |
| 1480 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1481 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Perform default conversions. |
| 1484 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1485 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1486 | QualType BaseType = BaseExpr->getType(); |
| 1487 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1489 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1490 | // must have pointer type, and the accessed type is the pointee. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | if (OpKind == tok::arrow) { |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1492 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1493 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1494 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1495 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1496 | MemberLoc, Member)); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1497 | else |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1498 | return ExprError(Diag(MemberLoc, |
| 1499 | diag::err_typecheck_member_reference_arrow) |
| 1500 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1502 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1503 | // Handle field access to simple records. This also handles access to fields |
| 1504 | // of the ObjC 'id' struct. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1505 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1506 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1507 | if (DiagnoseIncompleteType(OpLoc, BaseType, |
| 1508 | diag::err_typecheck_incomplete_tag, |
| 1509 | BaseExpr->getSourceRange())) |
| 1510 | return ExprError(); |
| 1511 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1512 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1513 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1514 | // than this. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1515 | LookupResult Result |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1516 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1517 | LookupMemberName, false); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1518 | |
| 1519 | Decl *MemberDecl = 0; |
| 1520 | if (!Result) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1521 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1522 | << &Member << BaseExpr->getSourceRange()); |
| 1523 | else if (Result.isAmbiguous()) { |
| 1524 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1525 | MemberLoc, BaseExpr->getSourceRange()); |
| 1526 | return ExprError(); |
| 1527 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1528 | MemberDecl = Result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1530 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1531 | // We may have found a field within an anonymous union or struct |
| 1532 | // (C++ [class.union]). |
| 1533 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1534 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1535 | BaseExpr, OpLoc); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1536 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1537 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1538 | // FIXME: Handle address space modifiers |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1539 | QualType MemberType = FD->getType(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1540 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1541 | MemberType = Ref->getPointeeType(); |
| 1542 | else { |
| 1543 | unsigned combinedQualifiers = |
| 1544 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1545 | if (FD->isMutable()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1546 | combinedQualifiers &= ~QualType::Const; |
| 1547 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1548 | } |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1549 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1550 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1551 | MemberLoc, MemberType)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1552 | } else if (CXXClassVarDecl *Var = dyn_cast<CXXClassVarDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1553 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1554 | Var, MemberLoc, |
| 1555 | Var->getType().getNonReferenceType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1556 | else if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1557 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1558 | MemberFn, MemberLoc, MemberFn->getType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1559 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1560 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1561 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1562 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1563 | else if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1564 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Enum, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1565 | MemberLoc, Enum->getType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1566 | else if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1567 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1568 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1569 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1570 | // We found a declaration kind that we didn't expect. This is a |
| 1571 | // generic error message that tells the user that she can't refer |
| 1572 | // to this member with '.' or '->'. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1573 | return ExprError(Diag(MemberLoc, |
| 1574 | diag::err_typecheck_member_reference_unknown) |
| 1575 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1576 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1578 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1579 | // (*Obj).ivar. |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1580 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1581 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member)) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1582 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 1583 | MemberLoc, BaseExpr, |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1584 | OpKind == tok::arrow); |
| 1585 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1586 | return Owned(MRef); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1587 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1588 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1589 | << IFTy->getDecl()->getDeclName() << &Member |
| 1590 | << BaseExpr->getSourceRange()); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1591 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1593 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1594 | // pointer to a (potentially qualified) interface type. |
| 1595 | const PointerType *PTy; |
| 1596 | const ObjCInterfaceType *IFTy; |
| 1597 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1598 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1599 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1600 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1601 | // Search for a declared property first. |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1602 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1603 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1604 | MemberLoc, BaseExpr)); |
| 1605 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1606 | // Check protocols on qualified interfaces. |
Chris Lattner | 9baefc2 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1607 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1608 | E = IFTy->qual_end(); I != E; ++I) |
| 1609 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1610 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1611 | MemberLoc, BaseExpr)); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1612 | |
| 1613 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1614 | // selector is implemented. |
| 1615 | |
| 1616 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1617 | // shared with the code in ActOnInstanceMessage. |
| 1618 | |
| 1619 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1620 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1621 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1622 | // If this reference is in an @implementation, check for 'private' methods. |
| 1623 | if (!Getter) |
| 1624 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1625 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1626 | if (ObjCImplementationDecl *ImpDecl = |
| 1627 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1628 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 1629 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1630 | // Look through local category implementations associated with the class. |
| 1631 | if (!Getter) { |
| 1632 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1633 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1634 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1635 | } |
| 1636 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1637 | if (Getter) { |
| 1638 | // If we found a getter then this may be a valid dot-reference, we |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1639 | // will look for the matching setter, in case it is needed. |
| 1640 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 1641 | &Member); |
| 1642 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 1643 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 1644 | if (!Setter) { |
| 1645 | // If this reference is in an @implementation, also check for 'private' |
| 1646 | // methods. |
| 1647 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1648 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1649 | if (ObjCImplementationDecl *ImpDecl = |
| 1650 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1651 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 1652 | } |
| 1653 | // Look through local category implementations associated with the class. |
| 1654 | if (!Setter) { |
| 1655 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 1656 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1657 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
| 1658 | } |
| 1659 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1660 | |
| 1661 | // FIXME: we must check that the setter has property type. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1662 | return Owned(new (Context) ObjCKVCRefExpr(Getter, Getter->getResultType(), |
| 1663 | Setter, MemberLoc, BaseExpr)); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1664 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1665 | |
| 1666 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1667 | << &Member << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1668 | } |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1669 | // Handle properties on qualified "id" protocols. |
| 1670 | const ObjCQualifiedIdType *QIdTy; |
| 1671 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 1672 | // Check protocols on qualified interfaces. |
| 1673 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1674 | E = QIdTy->qual_end(); I != E; ++I) { |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1675 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1676 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1677 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1678 | // Also must look for a getter name which uses property syntax. |
| 1679 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1680 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1681 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
| 1682 | OMD->getResultType(), OMD, OpLoc, MemberLoc, NULL, 0)); |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1685 | |
| 1686 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1687 | << &Member << BaseType); |
| 1688 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1689 | // Handle 'field access' to vectors, such as 'V.xx'. |
| 1690 | if (BaseType->isExtVectorType() && OpKind == tok::period) { |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1691 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1692 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1693 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1694 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
| 1695 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1696 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1697 | |
| 1698 | return ExprError(Diag(MemberLoc, |
| 1699 | diag::err_typecheck_member_reference_struct_union) |
| 1700 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1703 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 1704 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 1705 | /// function prototype Proto. Call is the call expression itself, and |
| 1706 | /// Fn is the function expression. For a C++ member function, this |
| 1707 | /// routine does not attempt to convert the object argument. Returns |
| 1708 | /// true if the call is ill-formed. |
| 1709 | bool |
| 1710 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
| 1711 | FunctionDecl *FDecl, |
| 1712 | const FunctionTypeProto *Proto, |
| 1713 | Expr **Args, unsigned NumArgs, |
| 1714 | SourceLocation RParenLoc) { |
| 1715 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 1716 | // assignment, to the types of the corresponding parameter, ... |
| 1717 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1718 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1719 | bool Invalid = false; |
| 1720 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1721 | // If too few arguments are available (and we don't have default |
| 1722 | // arguments for the remaining parameters), don't make the call. |
| 1723 | if (NumArgs < NumArgsInProto) { |
| 1724 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 1725 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 1726 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 1727 | // Use default arguments for missing arguments |
| 1728 | NumArgsToCheck = NumArgsInProto; |
| 1729 | Call->setNumArgs(NumArgsInProto); |
| 1730 | } |
| 1731 | |
| 1732 | // If too many are passed and not variadic, error on the extras and drop |
| 1733 | // them. |
| 1734 | if (NumArgs > NumArgsInProto) { |
| 1735 | if (!Proto->isVariadic()) { |
| 1736 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 1737 | diag::err_typecheck_call_too_many_args) |
| 1738 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 1739 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 1740 | Args[NumArgs-1]->getLocEnd()); |
| 1741 | // This deletes the extra arguments. |
| 1742 | Call->setNumArgs(NumArgsInProto); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1743 | Invalid = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1744 | } |
| 1745 | NumArgsToCheck = NumArgsInProto; |
| 1746 | } |
| 1747 | |
| 1748 | // Continue to check argument types (even if we have too few/many args). |
| 1749 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 1750 | QualType ProtoArgType = Proto->getArgType(i); |
| 1751 | |
| 1752 | Expr *Arg; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1753 | if (i < NumArgs) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1754 | Arg = Args[i]; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1755 | |
| 1756 | // Pass the argument. |
| 1757 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 1758 | return true; |
| 1759 | } else |
| 1760 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1761 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1762 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1764 | Call->setArg(i, Arg); |
| 1765 | } |
| 1766 | |
| 1767 | // If this is a variadic call, handle args passed through "...". |
| 1768 | if (Proto->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1769 | VariadicCallType CallType = VariadicFunction; |
| 1770 | if (Fn->getType()->isBlockPointerType()) |
| 1771 | CallType = VariadicBlock; // Block |
| 1772 | else if (isa<MemberExpr>(Fn)) |
| 1773 | CallType = VariadicMethod; |
| 1774 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1775 | // Promote the arguments (C99 6.5.2.2p7). |
| 1776 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 1777 | Expr *Arg = Args[i]; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1778 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1779 | Call->setArg(i, Arg); |
| 1780 | } |
| 1781 | } |
| 1782 | |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1783 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1786 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | /// This provides the location of the left/right parens and a list of comma |
| 1788 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1789 | Action::OwningExprResult |
| 1790 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 1791 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1792 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1793 | unsigned NumArgs = args.size(); |
| 1794 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 1795 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 1796 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1797 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1798 | OverloadedFunctionDecl *Ovl = NULL; |
| 1799 | |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1800 | // Determine whether this is a dependent call inside a C++ template, |
| 1801 | // in which case we won't do any semantic analysis now. |
| 1802 | bool Dependent = false; |
| 1803 | if (Fn->isTypeDependent()) { |
| 1804 | if (CXXDependentNameExpr *FnName = dyn_cast<CXXDependentNameExpr>(Fn)) { |
| 1805 | if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 1806 | Dependent = true; |
| 1807 | else { |
| 1808 | // Resolve the CXXDependentNameExpr to an actual identifier; |
| 1809 | // it wasn't really a dependent name after all. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1810 | OwningExprResult Resolved |
| 1811 | = ActOnDeclarationNameExpr(S, FnName->getLocation(), |
| 1812 | FnName->getName(), |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1813 | /*HasTrailingLParen=*/true, |
| 1814 | /*SS=*/0, |
| 1815 | /*ForceResolution=*/true); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1816 | if (Resolved.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1817 | return ExprError(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1818 | else { |
| 1819 | delete Fn; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1820 | Fn = (Expr *)Resolved.release(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | } else |
| 1824 | Dependent = true; |
| 1825 | } else |
| 1826 | Dependent = Expr::hasAnyTypeDependentArguments(Args, NumArgs); |
| 1827 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1828 | // FIXME: Will need to cache the results of name lookup (including |
| 1829 | // ADL) in Fn. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 1830 | if (Dependent) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1831 | return Owned(new (Context) CallExpr(Fn, Args, NumArgs, |
| 1832 | Context.DependentTy, RParenLoc)); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1834 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 1835 | if (getLangOptions().CPlusPlus && Fn->getType()->isRecordType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1836 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 1837 | CommaLocs, RParenLoc)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Determine whether this is a call to a member function. |
| 1840 | if (getLangOptions().CPlusPlus) { |
| 1841 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 1842 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 1843 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1844 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 1845 | CommaLocs, RParenLoc)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1848 | // If we're directly calling a function or a set of overloaded |
| 1849 | // functions, get the appropriate declaration. |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1850 | DeclRefExpr *DRExpr = NULL; |
| 1851 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 1852 | DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()); |
| 1853 | else |
| 1854 | DRExpr = dyn_cast<DeclRefExpr>(Fn); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1856 | if (DRExpr) { |
| 1857 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
| 1858 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1861 | if (Ovl) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1862 | FDecl = ResolveOverloadedCallFn(Fn, Ovl, LParenLoc, Args, NumArgs, |
| 1863 | CommaLocs, RParenLoc); |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 1864 | if (!FDecl) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1865 | return ExprError(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1866 | |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 1867 | // Update Fn to refer to the actual function selected. |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1868 | Expr *NewFn = 0; |
| 1869 | if (QualifiedDeclRefExpr *QDRExpr = dyn_cast<QualifiedDeclRefExpr>(DRExpr)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1870 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1871 | QDRExpr->getLocation(), false, false, |
| 1872 | QDRExpr->getSourceRange().getBegin()); |
| 1873 | else |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1874 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
| 1875 | Fn->getSourceRange().getBegin()); |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 1876 | Fn->Destroy(Context); |
| 1877 | Fn = NewFn; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1878 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1879 | |
| 1880 | // Promote the function operand. |
| 1881 | UsualUnaryConversions(Fn); |
| 1882 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1883 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 1884 | // of arguments and function on error. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1885 | // FIXME: Except that llvm::OwningPtr uses delete, when it really must be |
| 1886 | // Destroy(), or nothing gets cleaned up. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1887 | llvm::OwningPtr<CallExpr> TheCall(new (Context) CallExpr(Fn, Args, NumArgs, |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1888 | Context.BoolTy, RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1889 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1890 | const FunctionType *FuncT; |
| 1891 | if (!Fn->getType()->isBlockPointerType()) { |
| 1892 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 1893 | // have type pointer to function". |
| 1894 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 1895 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1896 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 1897 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1898 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 1899 | } else { // This is a block call. |
| 1900 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 1901 | getAsFunctionType(); |
| 1902 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1903 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1904 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 1905 | << Fn->getType() << Fn->getSourceRange()); |
| 1906 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1907 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 1908 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1910 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1911 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
| 1912 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1913 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1914 | } else { |
| 1915 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1916 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1917 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1918 | for (unsigned i = 0; i != NumArgs; i++) { |
| 1919 | Expr *Arg = Args[i]; |
| 1920 | DefaultArgumentPromotion(Arg); |
| 1921 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1922 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1923 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1925 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 1926 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1927 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 1928 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1930 | // Do special checking on direct calls to functions. |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1931 | if (FDecl) |
| 1932 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1933 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1934 | return Owned(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1937 | Action::OwningExprResult |
| 1938 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 1939 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1940 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1941 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 1942 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1943 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1944 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 1945 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1946 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1947 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1948 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 1949 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1950 | } else if (DiagnoseIncompleteType(LParenLoc, literalType, |
| 1951 | diag::err_typecheck_decl_incomplete_type, |
| 1952 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1953 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1954 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1955 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1956 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1957 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1958 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1959 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1960 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1961 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1962 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1963 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1964 | InitExpr.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1965 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
| 1966 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1969 | Action::OwningExprResult |
| 1970 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| 1971 | InitListDesignations &Designators, |
| 1972 | SourceLocation RBraceLoc) { |
| 1973 | unsigned NumInit = initlist.size(); |
| 1974 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1975 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1976 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 1977 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1978 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1979 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1980 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1981 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 1982 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1985 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 58d5ebb | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 1986 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1987 | UsualUnaryConversions(castExpr); |
| 1988 | |
| 1989 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 1990 | // type needs to be scalar. |
| 1991 | if (castType->isVoidType()) { |
| 1992 | // Cast to void allows any expr type. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1993 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 1994 | // We can't check any more until template instantiation time. |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1995 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 1996 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 1997 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 1998 | (castType->isStructureType() || castType->isUnionType())) { |
| 1999 | // GCC struct/union extension: allow cast to self. |
| 2000 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2001 | << castType << castExpr->getSourceRange(); |
| 2002 | } else if (castType->isUnionType()) { |
| 2003 | // GCC cast to union extension |
| 2004 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2005 | RecordDecl::field_iterator Field, FieldEnd; |
| 2006 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2007 | Field != FieldEnd; ++Field) { |
| 2008 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2009 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2010 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2011 | << castExpr->getSourceRange(); |
| 2012 | break; |
| 2013 | } |
| 2014 | } |
| 2015 | if (Field == FieldEnd) |
| 2016 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2017 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2018 | } else { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2019 | // Reject any other conversions to non-scalar types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2020 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2021 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2022 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2023 | } else if (!castExpr->getType()->isScalarType() && |
| 2024 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2025 | return Diag(castExpr->getLocStart(), |
| 2026 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2027 | << castExpr->getType() << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2028 | } else if (castExpr->getType()->isVectorType()) { |
| 2029 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2030 | return true; |
| 2031 | } else if (castType->isVectorType()) { |
| 2032 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2033 | return true; |
| 2034 | } |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2038 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2039 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 2040 | |
| 2041 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2042 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2043 | return Diag(R.getBegin(), |
| 2044 | Ty->isVectorType() ? |
| 2045 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2046 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2047 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2048 | } else |
| 2049 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2050 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2051 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2052 | |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2056 | Action::OwningExprResult |
| 2057 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2058 | SourceLocation RParenLoc, ExprArg Op) { |
| 2059 | assert((Ty != 0) && (Op.get() != 0) && |
| 2060 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2061 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2062 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2063 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2064 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2065 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2066 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2067 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2068 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2071 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 2072 | /// In that case, lex = cond. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2073 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2074 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2075 | UsualUnaryConversions(cond); |
| 2076 | UsualUnaryConversions(lex); |
| 2077 | UsualUnaryConversions(rex); |
| 2078 | QualType condT = cond->getType(); |
| 2079 | QualType lexT = lex->getType(); |
| 2080 | QualType rexT = rex->getType(); |
| 2081 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2082 | // first, check the condition. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2083 | if (!cond->isTypeDependent()) { |
| 2084 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 2085 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) << condT; |
| 2086 | return QualType(); |
| 2087 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2088 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Now check the two expressions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2091 | if ((lex && lex->isTypeDependent()) || (rex && rex->isTypeDependent())) |
| 2092 | return Context.DependentTy; |
| 2093 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2094 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2095 | // to find a common type: C99 6.5.15p3,5. |
| 2096 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2097 | UsualArithmeticConversions(lex, rex); |
| 2098 | return lex->getType(); |
| 2099 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2100 | |
| 2101 | // If both operands are the same structure or union type, the result is that |
| 2102 | // type. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2103 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2104 | if (const RecordType *RHSRT = rexT->getAsRecordType()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2105 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2106 | // "If both the operands have structure or union type, the result has |
| 2107 | // that type." This implies that CV qualifiers are dropped. |
| 2108 | return lexT.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2110 | |
| 2111 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2112 | // The following || allows only one side to be void (a GCC-ism). |
| 2113 | if (lexT->isVoidType() || rexT->isVoidType()) { |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2114 | if (!lexT->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2115 | Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2116 | << rex->getSourceRange(); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2117 | if (!rexT->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2118 | Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2119 | << lex->getSourceRange(); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2120 | ImpCastExprToType(lex, Context.VoidTy); |
| 2121 | ImpCastExprToType(rex, Context.VoidTy); |
| 2122 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2123 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2124 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2125 | // the type of the other operand." |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2126 | if ((lexT->isPointerType() || lexT->isBlockPointerType() || |
| 2127 | Context.isObjCObjectPointerType(lexT)) && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 2128 | rex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2129 | ImpCastExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2130 | return lexT; |
| 2131 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2132 | if ((rexT->isPointerType() || rexT->isBlockPointerType() || |
| 2133 | Context.isObjCObjectPointerType(rexT)) && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 2134 | lex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2135 | ImpCastExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2136 | return rexT; |
| 2137 | } |
Chris Lattner | bd57d36 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2138 | // Handle the case where both operands are pointers before we handle null |
| 2139 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2140 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2141 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 2142 | // get the "pointed to" types |
| 2143 | QualType lhptee = LHSPT->getPointeeType(); |
| 2144 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2145 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2146 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2147 | if (lhptee->isVoidType() && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2148 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2149 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2150 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2151 | QualType destType = Context.getPointerType(destPointee); |
| 2152 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 2153 | ImpCastExprToType(rex, destType); // promote to void* |
| 2154 | return destType; |
| 2155 | } |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2156 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2157 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2158 | QualType destType = Context.getPointerType(destPointee); |
| 2159 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 2160 | ImpCastExprToType(rex, destType); // promote to void* |
| 2161 | return destType; |
| 2162 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2163 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2164 | QualType compositeType = lexT; |
| 2165 | |
| 2166 | // If either type is an Objective-C object type then check |
| 2167 | // compatibility according to Objective-C. |
| 2168 | if (Context.isObjCObjectPointerType(lexT) || |
| 2169 | Context.isObjCObjectPointerType(rexT)) { |
| 2170 | // If both operands are interfaces and either operand can be |
| 2171 | // assigned to the other, use that type as the composite |
| 2172 | // type. This allows |
| 2173 | // xxx ? (A*) a : (B*) b |
| 2174 | // where B is a subclass of A. |
| 2175 | // |
| 2176 | // Additionally, as for assignment, if either type is 'id' |
| 2177 | // allow silent coercion. Finally, if the types are |
| 2178 | // incompatible then make sure to use 'id' as the composite |
| 2179 | // type so the result is acceptable for sending messages to. |
| 2180 | |
| 2181 | // FIXME: This code should not be localized to here. Also this |
| 2182 | // should use a compatible check instead of abusing the |
| 2183 | // canAssignObjCInterfaces code. |
| 2184 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2185 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2186 | if (LHSIface && RHSIface && |
| 2187 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 2188 | compositeType = lexT; |
| 2189 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 7ffd0de | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2190 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2191 | compositeType = rexT; |
| 2192 | } else if (Context.isObjCIdType(lhptee) || |
| 2193 | Context.isObjCIdType(rhptee)) { |
| 2194 | // FIXME: This code looks wrong, because isObjCIdType checks |
| 2195 | // the struct but getObjCIdType returns the pointer to |
| 2196 | // struct. This is horrible and should be fixed. |
| 2197 | compositeType = Context.getObjCIdType(); |
| 2198 | } else { |
| 2199 | QualType incompatTy = Context.getObjCIdType(); |
| 2200 | ImpCastExprToType(lex, incompatTy); |
| 2201 | ImpCastExprToType(rex, incompatTy); |
| 2202 | return incompatTy; |
| 2203 | } |
| 2204 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2205 | rhptee.getUnqualifiedType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2206 | Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2207 | << lexT << rexT << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2208 | // In this situation, we assume void* type. No especially good |
| 2209 | // reason, but this is what gcc does, and we do have to pick |
| 2210 | // to get a consistent AST. |
| 2211 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2212 | ImpCastExprToType(lex, incompatTy); |
| 2213 | ImpCastExprToType(rex, incompatTy); |
| 2214 | return incompatTy; |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2215 | } |
| 2216 | // The pointer types are compatible. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2217 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2218 | // differently qualified versions of compatible types, the result type is |
| 2219 | // a pointer to an appropriately qualified version of the *composite* |
| 2220 | // type. |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2221 | // FIXME: Need to calculate the composite type. |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2222 | // FIXME: Need to add qualifiers |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2223 | ImpCastExprToType(lex, compositeType); |
| 2224 | ImpCastExprToType(rex, compositeType); |
| 2225 | return compositeType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2228 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2229 | // evaluates to "struct objc_object *" (and is handled above when comparing |
| 2230 | // id with statically typed objects). |
| 2231 | if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) { |
| 2232 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2233 | // id. Currently localizing to here until clear this should be |
| 2234 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 2235 | if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true) || |
| 2236 | (lexT->isObjCQualifiedIdType() && |
| 2237 | Context.isObjCObjectPointerType(rexT)) || |
| 2238 | (rexT->isObjCQualifiedIdType() && |
| 2239 | Context.isObjCObjectPointerType(lexT))) { |
| 2240 | // FIXME: This is not the correct composite type. This only |
| 2241 | // happens to work because id can more or less be used anywhere, |
| 2242 | // however this may change the type of method sends. |
| 2243 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2244 | // (confusing) incompatible comparison warnings in some |
| 2245 | // cases. Investigate. |
| 2246 | QualType compositeType = Context.getObjCIdType(); |
| 2247 | ImpCastExprToType(lex, compositeType); |
| 2248 | ImpCastExprToType(rex, compositeType); |
| 2249 | return compositeType; |
| 2250 | } |
| 2251 | } |
| 2252 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 2253 | // Selection between block pointer types is ok as long as they are the same. |
| 2254 | if (lexT->isBlockPointerType() && rexT->isBlockPointerType() && |
| 2255 | Context.getCanonicalType(lexT) == Context.getCanonicalType(rexT)) |
| 2256 | return lexT; |
| 2257 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2258 | // Otherwise, the operands are not compatible. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2259 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2260 | << lexT << rexT << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2261 | return QualType(); |
| 2262 | } |
| 2263 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2264 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2265 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2266 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2267 | SourceLocation ColonLoc, |
| 2268 | ExprArg Cond, ExprArg LHS, |
| 2269 | ExprArg RHS) { |
| 2270 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2271 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2272 | |
| 2273 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2274 | // was the condition. |
| 2275 | bool isLHSNull = LHSExpr == 0; |
| 2276 | if (isLHSNull) |
| 2277 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2278 | |
| 2279 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 2280 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2281 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2282 | return ExprError(); |
| 2283 | |
| 2284 | Cond.release(); |
| 2285 | LHS.release(); |
| 2286 | RHS.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2287 | return Owned(new (Context) ConditionalOperator(CondExpr, |
| 2288 | isLHSNull ? 0 : LHSExpr, |
| 2289 | RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2292 | |
| 2293 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 2294 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 2295 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2296 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2297 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2298 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2299 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2300 | QualType lhptee, rhptee; |
| 2301 | |
| 2302 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2303 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2304 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2305 | |
| 2306 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2307 | lhptee = Context.getCanonicalType(lhptee); |
| 2308 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2309 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2310 | AssignConvertType ConvTy = Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2311 | |
| 2312 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2313 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2314 | // qualifiers of the type *pointed to* by the right; |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2315 | // FIXME: Handle ASQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2316 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2317 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2318 | |
| 2319 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2320 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 2321 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2322 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2323 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2324 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2325 | |
| 2326 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2327 | assert(rhptee->isFunctionType()); |
| 2328 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2332 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2333 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2334 | |
| 2335 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2336 | assert(lhptee->isFunctionType()); |
| 2337 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2338 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Check for ObjC interfaces |
| 2341 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2342 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2343 | if (LHSIface && RHSIface && |
| 2344 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 2345 | return ConvTy; |
| 2346 | |
| 2347 | // ID acts sort of like void* for ObjC interfaces |
| 2348 | if (LHSIface && Context.isObjCIdType(rhptee)) |
| 2349 | return ConvTy; |
| 2350 | if (RHSIface && Context.isObjCIdType(lhptee)) |
| 2351 | return ConvTy; |
| 2352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2353 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 2354 | // unqualified versions of compatible types, ... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2355 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2356 | rhptee.getUnqualifiedType())) |
| 2357 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2358 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2361 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2362 | /// block pointer types are compatible or whether a block and normal pointer |
| 2363 | /// are compatible. It is more restrict than comparing two function pointer |
| 2364 | // types. |
| 2365 | Sema::AssignConvertType |
| 2366 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
| 2367 | QualType rhsType) { |
| 2368 | QualType lhptee, rhptee; |
| 2369 | |
| 2370 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2371 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
| 2372 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2373 | |
| 2374 | // make sure we operate on the canonical type |
| 2375 | lhptee = Context.getCanonicalType(lhptee); |
| 2376 | rhptee = Context.getCanonicalType(rhptee); |
| 2377 | |
| 2378 | AssignConvertType ConvTy = Compatible; |
| 2379 | |
| 2380 | // For blocks we enforce that qualifiers are identical. |
| 2381 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2382 | ConvTy = CompatiblePointerDiscardsQualifiers; |
| 2383 | |
| 2384 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
| 2385 | return IncompatibleBlockPointer; |
| 2386 | return ConvTy; |
| 2387 | } |
| 2388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2389 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2390 | /// has code to accommodate several GCC extensions when type checking |
| 2391 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2392 | /// |
| 2393 | /// int a, *pint; |
| 2394 | /// short *pshort; |
| 2395 | /// struct foo *pfoo; |
| 2396 | /// |
| 2397 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2398 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2399 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2400 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2401 | /// |
| 2402 | /// As a result, the code for dealing with pointers is more complex than the |
| 2403 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2404 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2405 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2406 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2407 | // Get canonical types. We're not formatting these types, just comparing |
| 2408 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2409 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2410 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2411 | |
| 2412 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2413 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 2414 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2415 | // If the left-hand side is a reference type, then we are in a |
| 2416 | // (rare!) case where we've allowed the use of references in C, |
| 2417 | // e.g., as a parameter type in a built-in function. In this case, |
| 2418 | // just make sure that the type referenced is compatible with the |
| 2419 | // right-hand side type. The caller is responsible for adjusting |
| 2420 | // lhsType so that the resulting expression does not have reference |
| 2421 | // type. |
| 2422 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2423 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2424 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2425 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2426 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2428 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2429 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2430 | return Compatible; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2431 | // Relax integer conversions like we do for pointers below. |
| 2432 | if (rhsType->isIntegerType()) |
| 2433 | return IntToPointer; |
| 2434 | if (lhsType->isIntegerType()) |
| 2435 | return PointerToInt; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2436 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2437 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2438 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2439 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2440 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2441 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2442 | if (LV->getElementType() == rhsType) |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2443 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2444 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2445 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 2446 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 2447 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2448 | if (getLangOptions().LaxVectorConversions && |
| 2449 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2450 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
| 2451 | return Compatible; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2452 | } |
| 2453 | return Incompatible; |
| 2454 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2456 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2457 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2458 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2459 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2460 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2461 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2462 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2463 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2464 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2465 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2466 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2467 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2468 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2469 | |
| 2470 | // Treat block pointers as objects. |
| 2471 | if (getLangOptions().ObjC1 && |
| 2472 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2473 | return Compatible; |
| 2474 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2475 | return Incompatible; |
| 2476 | } |
| 2477 | |
| 2478 | if (isa<BlockPointerType>(lhsType)) { |
| 2479 | if (rhsType->isIntegerType()) |
| 2480 | return IntToPointer; |
| 2481 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2482 | // Treat block pointers as objects. |
| 2483 | if (getLangOptions().ObjC1 && |
| 2484 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2485 | return Compatible; |
| 2486 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2487 | if (rhsType->isBlockPointerType()) |
| 2488 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
| 2489 | |
| 2490 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 2491 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2492 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2493 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2494 | return Incompatible; |
| 2495 | } |
| 2496 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2497 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2498 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2499 | if (lhsType == Context.BoolTy) |
| 2500 | return Compatible; |
| 2501 | |
| 2502 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2503 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2504 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2505 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2506 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2507 | |
| 2508 | if (isa<BlockPointerType>(lhsType) && |
| 2509 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2510 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2511 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2512 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2514 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2515 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2516 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2517 | } |
| 2518 | return Incompatible; |
| 2519 | } |
| 2520 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2521 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2522 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2523 | if (getLangOptions().CPlusPlus) { |
| 2524 | if (!lhsType->isRecordType()) { |
| 2525 | // C++ 5.17p3: If the left operand is not of class type, the |
| 2526 | // expression is implicitly converted (C++ 4) to the |
| 2527 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2528 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 2529 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2530 | return Incompatible; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2531 | else |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2532 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 2536 | // structures. |
| 2537 | } |
| 2538 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2539 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 2540 | // a null pointer constant. |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2541 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType() || |
| 2542 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 2543 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2544 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2545 | return Compatible; |
| 2546 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2547 | |
| 2548 | // We don't allow conversion of non-null-pointer constants to integers. |
| 2549 | if (lhsType->isBlockPointerType() && rExpr->getType()->isIntegerType()) |
| 2550 | return IntToBlockPointer; |
| 2551 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2552 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2553 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2554 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2555 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2556 | // |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2557 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2558 | if (!lhsType->isReferenceType()) |
| 2559 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2560 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2561 | Sema::AssignConvertType result = |
| 2562 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2563 | |
| 2564 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 2565 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2566 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 2567 | // so that we can use references in built-in functions even in C. |
| 2568 | // The getNonReferenceType() call makes sure that the resulting expression |
| 2569 | // does not have reference type. |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2570 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2571 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2572 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2575 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2576 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 2577 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 2578 | } |
| 2579 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2580 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2581 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 2582 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2583 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2584 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2587 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2588 | Expr *&rex) { |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2589 | // For conversion purposes, we ignore any qualifiers. |
| 2590 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2591 | QualType lhsType = |
| 2592 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 2593 | QualType rhsType = |
| 2594 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2595 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2596 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2597 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2599 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2600 | // Handle the case of a vector & extvector type of the same size and element |
| 2601 | // type. It would be nice if we only had one vector type someday. |
| 2602 | if (getLangOptions().LaxVectorConversions) |
| 2603 | if (const VectorType *LV = lhsType->getAsVectorType()) |
| 2604 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 2605 | if (LV->getElementType() == RV->getElementType() && |
| 2606 | LV->getNumElements() == RV->getNumElements()) |
| 2607 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
| 2608 | |
| 2609 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 2610 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2611 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2612 | QualType eltType = V->getElementType(); |
| 2613 | |
| 2614 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
| 2615 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 2616 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2617 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2618 | return lhsType; |
| 2619 | } |
| 2620 | } |
| 2621 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2622 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2623 | // promote the lhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2624 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2625 | QualType eltType = V->getElementType(); |
| 2626 | |
| 2627 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
| 2628 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 2629 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2630 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2631 | return rhsType; |
| 2632 | } |
| 2633 | } |
| 2634 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2635 | // You cannot convert between vector values of different size. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2636 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2637 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2638 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2639 | return QualType(); |
| 2640 | } |
| 2641 | |
| 2642 | inline QualType Sema::CheckMultiplyDivideOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2643 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2644 | { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 2645 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2646 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2647 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2648 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2649 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2650 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2651 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2652 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | inline QualType Sema::CheckRemainderOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2656 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2657 | { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 2658 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 2659 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 2660 | return CheckVectorOperands(Loc, lex, rex); |
| 2661 | return InvalidOperands(Loc, lex, rex); |
| 2662 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2663 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2664 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2665 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2666 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2667 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2668 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2672 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2673 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2674 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2675 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2676 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2677 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2678 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2679 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2680 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2681 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2682 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2683 | // Put any potential pointer into PExp |
| 2684 | Expr* PExp = lex, *IExp = rex; |
| 2685 | if (IExp->getType()->isPointerType()) |
| 2686 | std::swap(PExp, IExp); |
| 2687 | |
| 2688 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 2689 | if (IExp->getType()->isIntegerType()) { |
| 2690 | // Check for arithmetic on pointers to incomplete types |
| 2691 | if (!PTy->getPointeeType()->isObjectType()) { |
| 2692 | if (PTy->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2693 | if (getLangOptions().CPlusPlus) { |
| 2694 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 2695 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2696 | return QualType(); |
| 2697 | } |
| 2698 | |
| 2699 | // GNU extension: arithmetic on pointer to void |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2700 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2701 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2702 | } else if (PTy->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2703 | if (getLangOptions().CPlusPlus) { |
| 2704 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2705 | << lex->getType() << lex->getSourceRange(); |
| 2706 | return QualType(); |
| 2707 | } |
| 2708 | |
| 2709 | // GNU extension: arithmetic on pointer to function |
| 2710 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2711 | << lex->getType() << lex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2712 | } else { |
| 2713 | DiagnoseIncompleteType(Loc, PTy->getPointeeType(), |
| 2714 | diag::err_typecheck_arithmetic_incomplete_type, |
| 2715 | lex->getSourceRange(), SourceRange(), |
| 2716 | lex->getType()); |
| 2717 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | return PExp->getType(); |
| 2721 | } |
| 2722 | } |
| 2723 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2724 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2727 | // C99 6.5.6 |
| 2728 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2729 | SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2730 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2731 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2732 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2733 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2734 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2735 | // Enforce type constraints: C99 6.5.6p3. |
| 2736 | |
| 2737 | // Handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2738 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2739 | return compType; |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Either ptr - int or ptr - ptr. |
| 2742 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2743 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2744 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2745 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2746 | if (!lpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2747 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2748 | if (lpointee->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2749 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2750 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2751 | } else if (lpointee->isFunctionType()) { |
| 2752 | if (getLangOptions().CPlusPlus) { |
| 2753 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2754 | << lex->getType() << lex->getSourceRange(); |
| 2755 | return QualType(); |
| 2756 | } |
| 2757 | |
| 2758 | // GNU extension: arithmetic on pointer to function |
| 2759 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 2760 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2761 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2762 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2763 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2764 | return QualType(); |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | // The result type of a pointer-int computation is the pointer type. |
| 2769 | if (rex->getType()->isIntegerType()) |
| 2770 | return lex->getType(); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2772 | // Handle pointer-pointer subtractions. |
| 2773 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2774 | QualType rpointee = RHSPTy->getPointeeType(); |
| 2775 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2776 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2777 | if (!rpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2778 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2779 | if (rpointee->isVoidType()) { |
| 2780 | if (!lpointee->isVoidType()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2781 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2782 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 0804888 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 2783 | } else if (rpointee->isFunctionType()) { |
| 2784 | if (getLangOptions().CPlusPlus) { |
| 2785 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2786 | << rex->getType() << rex->getSourceRange(); |
| 2787 | return QualType(); |
| 2788 | } |
| 2789 | |
| 2790 | // GNU extension: arithmetic on pointer to function |
| 2791 | if (!lpointee->isFunctionType()) |
| 2792 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 2793 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2794 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2795 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2796 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2797 | return QualType(); |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | // Pointee types must be compatible. |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 2802 | if (!Context.typesAreCompatible( |
| 2803 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 2804 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2805 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2806 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2807 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2808 | return QualType(); |
| 2809 | } |
| 2810 | |
| 2811 | return Context.getPointerDiffType(); |
| 2812 | } |
| 2813 | } |
| 2814 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2815 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2818 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2819 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2820 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2821 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 2822 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2823 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2825 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 2826 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 2827 | if (!isCompAssign) |
| 2828 | UsualUnaryConversions(lex); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2829 | UsualUnaryConversions(rex); |
| 2830 | |
| 2831 | // "The type of the result is that of the promoted left operand." |
| 2832 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2835 | static bool areComparableObjCInterfaces(QualType LHS, QualType RHS, |
| 2836 | ASTContext& Context) { |
| 2837 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 2838 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 2839 | // ID acts sort of like void* for ObjC interfaces |
| 2840 | if (LHSIface && Context.isObjCIdType(RHS)) |
| 2841 | return true; |
| 2842 | if (RHSIface && Context.isObjCIdType(LHS)) |
| 2843 | return true; |
| 2844 | if (!LHSIface || !RHSIface) |
| 2845 | return false; |
| 2846 | return Context.canAssignObjCInterfaces(LHSIface, RHSIface) || |
| 2847 | Context.canAssignObjCInterfaces(RHSIface, LHSIface); |
| 2848 | } |
| 2849 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2850 | // C99 6.5.8 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2851 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2852 | bool isRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2853 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2854 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2856 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 2857 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 2858 | UsualArithmeticConversions(lex, rex); |
| 2859 | else { |
| 2860 | UsualUnaryConversions(lex); |
| 2861 | UsualUnaryConversions(rex); |
| 2862 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2863 | QualType lType = lex->getType(); |
| 2864 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2865 | |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2866 | // For non-floating point types, check for self-comparisons of the form |
| 2867 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 2868 | // often indicate logic errors in the program. |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2869 | if (!lType->isFloatingType()) { |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2870 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 2871 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2872 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2873 | Diag(Loc, diag::warn_selfcomparison); |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2876 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| 2877 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy : Context.IntTy; |
| 2878 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2879 | if (isRelational) { |
| 2880 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2881 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2882 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2883 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2884 | if (lType->isFloatingType()) { |
| 2885 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2886 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2889 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2890 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2891 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2892 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2893 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 2894 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 2895 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2896 | // All of the following pointer related warnings are GCC extensions, except |
| 2897 | // when handling null pointer constants. One day, we can consider making them |
| 2898 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 2899 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2900 | QualType LCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2901 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2902 | QualType RCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2903 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2904 | |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 2905 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2906 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 2907 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2908 | RCanPointeeTy.getUnqualifiedType()) && |
| 2909 | !areComparableObjCInterfaces(LCanPointeeTy, RCanPointeeTy, Context)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2910 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2911 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2912 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2913 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2914 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2915 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2916 | // Handle block pointer types. |
| 2917 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 2918 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 2919 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
| 2920 | |
| 2921 | if (!LHSIsNull && !RHSIsNull && |
| 2922 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2923 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2924 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2925 | } |
| 2926 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2927 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2928 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 2929 | // Allow block pointers to be compared with null pointer constants. |
| 2930 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 2931 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 2932 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2933 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2934 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 2935 | } |
| 2936 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2937 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 2938 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2939 | |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2940 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 2941 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 2942 | const PointerType *LPT = lType->getAsPointerType(); |
| 2943 | const PointerType *RPT = rType->getAsPointerType(); |
| 2944 | bool LPtrToVoid = LPT ? |
| 2945 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
| 2946 | bool RPtrToVoid = RPT ? |
| 2947 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
| 2948 | |
| 2949 | if (!LPtrToVoid && !RPtrToVoid && |
| 2950 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2951 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2952 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 2953 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2954 | return ResultTy; |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 2955 | } |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 2956 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2957 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 2958 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2959 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 2960 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2961 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2962 | } else { |
| 2963 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2964 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2965 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 2966 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2967 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2968 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2969 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 2970 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2971 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
| 2972 | rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2973 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2974 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2975 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2976 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2977 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2978 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2979 | if (lType->isIntegerType() && |
| 2980 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2981 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2982 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2983 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2984 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2985 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2986 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2987 | // Handle block pointers. |
| 2988 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 2989 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2990 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2991 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2992 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2993 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2994 | } |
| 2995 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 2996 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2997 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2998 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2999 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3000 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3001 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3002 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3005 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| 3006 | /// operates on extended vector types. Instead of producing an IntTy result, |
| 3007 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3008 | /// types. |
| 3009 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3010 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3011 | bool isRelational) { |
| 3012 | // Check to make sure we're operating on vectors of the same type and width, |
| 3013 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3014 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3015 | if (vType.isNull()) |
| 3016 | return vType; |
| 3017 | |
| 3018 | QualType lType = lex->getType(); |
| 3019 | QualType rType = rex->getType(); |
| 3020 | |
| 3021 | // For non-floating point types, check for self-comparisons of the form |
| 3022 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3023 | // often indicate logic errors in the program. |
| 3024 | if (!lType->isFloatingType()) { |
| 3025 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3026 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3027 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3028 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | // Check for comparisons of floating point operands using != and ==. |
| 3032 | if (!isRelational && lType->isFloatingType()) { |
| 3033 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3034 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | // Return the type for the comparison, which is the same as vector type for |
| 3038 | // integer vectors, or an integer type of identical size and number of |
| 3039 | // elements for floating point vectors. |
| 3040 | if (lType->isIntegerType()) |
| 3041 | return lType; |
| 3042 | |
| 3043 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3044 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3045 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3046 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3047 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 3048 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3049 | |
| 3050 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 3051 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3052 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3053 | } |
| 3054 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | inline QualType Sema::CheckBitwiseOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3056 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3057 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 3058 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3059 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3060 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3061 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3062 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3063 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3064 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3065 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3069 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3070 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3071 | UsualUnaryConversions(lex); |
| 3072 | UsualUnaryConversions(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3073 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3074 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3075 | return Context.IntTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3076 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3079 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3080 | /// is a read-only property; return true if so. A readonly property expression |
| 3081 | /// depends on various declarations and thus must be treated specially. |
| 3082 | /// |
| 3083 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
| 3084 | { |
| 3085 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3086 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3087 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3088 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3089 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
| 3090 | if (const ObjCInterfaceType *IFTy = |
| 3091 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3092 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3093 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3094 | return true; |
| 3095 | } |
| 3096 | } |
| 3097 | return false; |
| 3098 | } |
| 3099 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3100 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3101 | /// emit an error and return true. If so, return false. |
| 3102 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3103 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3104 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3105 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3106 | if (IsLV == Expr::MLV_Valid) |
| 3107 | return false; |
| 3108 | |
| 3109 | unsigned Diag = 0; |
| 3110 | bool NeedType = false; |
| 3111 | switch (IsLV) { // C99 6.5.16p2 |
| 3112 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3113 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3114 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3115 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3116 | NeedType = true; |
| 3117 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3118 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3119 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3120 | NeedType = true; |
| 3121 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3122 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3123 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3124 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3125 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3126 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3127 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3128 | case Expr::MLV_IncompleteType: |
| 3129 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3130 | return S.DiagnoseIncompleteType(Loc, E->getType(), |
| 3131 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3132 | E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3133 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3134 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3135 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3136 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3137 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3138 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3139 | case Expr::MLV_ReadonlyProperty: |
| 3140 | Diag = diag::error_readonly_property_assignment; |
| 3141 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3142 | case Expr::MLV_NoSetterProperty: |
| 3143 | Diag = diag::error_nosetter_property_assignment; |
| 3144 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3147 | if (NeedType) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3148 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3149 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3150 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3151 | return true; |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | |
| 3156 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3157 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3158 | SourceLocation Loc, |
| 3159 | QualType CompoundType) { |
| 3160 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3161 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3162 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3163 | |
| 3164 | QualType LHSType = LHS->getType(); |
| 3165 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3167 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3168 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3169 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3170 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3171 | // Special case of NSObject attributes on c-style pointer types. |
| 3172 | if (ConvTy == IncompatiblePointer && |
| 3173 | ((Context.isObjCNSObjectType(LHSType) && |
| 3174 | Context.isObjCObjectPointerType(RHSType)) || |
| 3175 | (Context.isObjCNSObjectType(RHSType) && |
| 3176 | Context.isObjCObjectPointerType(LHSType)))) |
| 3177 | ConvTy = Compatible; |
| 3178 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3179 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3180 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3181 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3182 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3183 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3184 | RHSCheck = ICE->getSubExpr(); |
| 3185 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3186 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3187 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3188 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3189 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3190 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3191 | Diag(Loc, diag::warn_not_compound_assign) |
| 3192 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3193 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3194 | } |
| 3195 | } else { |
| 3196 | // Compound assignment "x += y" |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3197 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3198 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3199 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3200 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3201 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3202 | return QualType(); |
| 3203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3204 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3205 | // left operand unless the left operand has qualified type, in which case |
| 3206 | // it is the unqualified version of the type of the left operand. |
| 3207 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3208 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3209 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3210 | // oprdu. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3211 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3214 | // C99 6.5.17 |
| 3215 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
| 3216 | // FIXME: what is required for LHS? |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3217 | |
| 3218 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3219 | DefaultFunctionArrayConversion(RHS); |
| 3220 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3223 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3224 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3225 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3226 | bool isInc) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3227 | QualType ResType = Op->getType(); |
| 3228 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3229 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3230 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3231 | // Decrement of bool is not allowed. |
| 3232 | if (!isInc) { |
| 3233 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3234 | return QualType(); |
| 3235 | } |
| 3236 | // Increment of bool sets it to true, but is deprecated. |
| 3237 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3238 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3239 | // OK! |
| 3240 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3241 | // C99 6.5.2.4p2, 6.5.6p2 |
| 3242 | if (PT->getPointeeType()->isObjectType()) { |
| 3243 | // Pointer to object is ok! |
| 3244 | } else if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3245 | if (getLangOptions().CPlusPlus) { |
| 3246 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3247 | << Op->getSourceRange(); |
| 3248 | return QualType(); |
| 3249 | } |
| 3250 | |
| 3251 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3252 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3253 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3254 | if (getLangOptions().CPlusPlus) { |
| 3255 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3256 | << Op->getType() << Op->getSourceRange(); |
| 3257 | return QualType(); |
| 3258 | } |
| 3259 | |
| 3260 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3261 | << ResType << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3262 | return QualType(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3263 | } else { |
| 3264 | DiagnoseIncompleteType(OpLoc, PT->getPointeeType(), |
| 3265 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3266 | Op->getSourceRange(), SourceRange(), |
| 3267 | ResType); |
| 3268 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3269 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3270 | } else if (ResType->isComplexType()) { |
| 3271 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3272 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3273 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3274 | } else { |
| 3275 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3276 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3277 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3278 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 3279 | // At this point, we know we have a real, complex or pointer type. |
| 3280 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3281 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3282 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3283 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3286 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3287 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3288 | /// where the declaration is needed for type checking. We only need to |
| 3289 | /// handle cases when the expression references a function designator |
| 3290 | /// or is an lvalue. Here are some examples: |
| 3291 | /// - &(x) => x |
| 3292 | /// - &*****f => f for f a function designator. |
| 3293 | /// - &s.xx => s |
| 3294 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3295 | /// - *(x + 1) -> x, if x is an array |
| 3296 | /// - &"123"[2] -> 0 |
| 3297 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3298 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3299 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3300 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3301 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3302 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3303 | case Stmt::MemberExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3304 | // Fields cannot be declared with a 'register' storage class. |
| 3305 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3306 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3307 | return 0; |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3308 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3309 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3310 | // &X[4] and &4[X] refers to X if X is not a pointer. |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3311 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3312 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 48d04ae | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3313 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | f2a4b84 | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3314 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3315 | return 0; |
| 3316 | else |
| 3317 | return VD; |
| 3318 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3319 | case Stmt::UnaryOperatorClass: { |
| 3320 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| 3321 | |
| 3322 | switch(UO->getOpcode()) { |
| 3323 | case UnaryOperator::Deref: { |
| 3324 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3325 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3326 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3327 | if (!VD || VD->getType()->isPointerType()) |
| 3328 | return 0; |
| 3329 | return VD; |
| 3330 | } |
| 3331 | return 0; |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3332 | } |
| 3333 | case UnaryOperator::Real: |
| 3334 | case UnaryOperator::Imag: |
| 3335 | case UnaryOperator::Extension: |
| 3336 | return getPrimaryDecl(UO->getSubExpr()); |
| 3337 | default: |
| 3338 | return 0; |
| 3339 | } |
| 3340 | } |
| 3341 | case Stmt::BinaryOperatorClass: { |
| 3342 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3343 | |
| 3344 | // Handle cases involving pointer arithmetic. The result of an |
| 3345 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3346 | |
| 3347 | // (x + n) or (n + x) => x |
| 3348 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3349 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3350 | return getPrimaryDecl(BO->getLHS()); |
| 3351 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3352 | return getPrimaryDecl(BO->getRHS()); |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | return 0; |
| 3357 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3358 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3359 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3360 | case Stmt::ImplicitCastExprClass: |
| 3361 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3362 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3363 | default: |
| 3364 | return 0; |
| 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 3369 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 3370 | /// object cannot be declared with storage class register or be a bit field. |
| 3371 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 3372 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3373 | /// In C++, the operand might be an overloaded function name, in which case |
| 3374 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3375 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3376 | if (op->isTypeDependent()) |
| 3377 | return Context.DependentTy; |
| 3378 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3379 | if (getLangOptions().C99) { |
| 3380 | // Implement C99-only parts of addressof rules. |
| 3381 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 3382 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 3383 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 3384 | // (assuming the deref expression is valid). |
| 3385 | return uOp->getSubExpr()->getType(); |
| 3386 | } |
| 3387 | // Technically, there should be a check for array subscript |
| 3388 | // expressions here, but the result of one is always an lvalue anyway. |
| 3389 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3390 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 3391 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 3392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3393 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3394 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 3395 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3396 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 3397 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3398 | return QualType(); |
| 3399 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3400 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 3401 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 3402 | if (Field->isBitField()) { |
| 3403 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3404 | << "bit-field" << op->getSourceRange(); |
| 3405 | return QualType(); |
| 3406 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3407 | } |
| 3408 | // Check for Apple extension for accessing vector components. |
| 3409 | } else if (isa<ArraySubscriptExpr>(op) && |
| 3410 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3411 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3412 | << "vector" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3413 | return QualType(); |
| 3414 | } else if (dcl) { // C99 6.5.3.2p1 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3415 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 3416 | // with the register storage-class specifier. |
| 3417 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 3418 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3419 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3420 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3421 | return QualType(); |
| 3422 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3423 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3424 | return Context.OverloadTy; |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3425 | } else if (isa<FieldDecl>(dcl)) { |
| 3426 | // Okay: we can take the address of a field. |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3427 | } else if (isa<FunctionDecl>(dcl)) { |
| 3428 | // Okay: we can take the address of a function. |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3429 | } |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3430 | else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3431 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3432 | } |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 3433 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3434 | // If the operand has type "type", the result has type "pointer to type". |
| 3435 | return Context.getPointerType(op->getType()); |
| 3436 | } |
| 3437 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3438 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
| 3439 | UsualUnaryConversions(Op); |
| 3440 | QualType Ty = Op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3441 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3442 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 3443 | // incomplete type or void. It would be possible to warn about dereferencing |
| 3444 | // a void pointer, but it's completely well-defined, and such a warning is |
| 3445 | // unlikely to catch any mistakes. |
| 3446 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3447 | return PT->getPointeeType(); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3448 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3449 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3450 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3451 | return QualType(); |
| 3452 | } |
| 3453 | |
| 3454 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 3455 | tok::TokenKind Kind) { |
| 3456 | BinaryOperator::Opcode Opc; |
| 3457 | switch (Kind) { |
| 3458 | default: assert(0 && "Unknown binop!"); |
| 3459 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 3460 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 3461 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 3462 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 3463 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 3464 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 3465 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 3466 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 3467 | case tok::less: Opc = BinaryOperator::LT; break; |
| 3468 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 3469 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 3470 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 3471 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 3472 | case tok::amp: Opc = BinaryOperator::And; break; |
| 3473 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 3474 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 3475 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 3476 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 3477 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 3478 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 3479 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 3480 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 3481 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 3482 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 3483 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 3484 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 3485 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 3486 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 3487 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 3488 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 3489 | } |
| 3490 | return Opc; |
| 3491 | } |
| 3492 | |
| 3493 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 3494 | tok::TokenKind Kind) { |
| 3495 | UnaryOperator::Opcode Opc; |
| 3496 | switch (Kind) { |
| 3497 | default: assert(0 && "Unknown unary op!"); |
| 3498 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 3499 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 3500 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 3501 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 3502 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 3503 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 3504 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 3505 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3506 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 3507 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 3508 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 3509 | } |
| 3510 | return Opc; |
| 3511 | } |
| 3512 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3513 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 3514 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 3515 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3516 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 3517 | unsigned Op, |
| 3518 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3519 | QualType ResultTy; // Result type of the binary operator. |
| 3520 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 3521 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
| 3522 | |
| 3523 | switch (Opc) { |
| 3524 | default: |
| 3525 | assert(0 && "Unknown binary expr!"); |
| 3526 | case BinaryOperator::Assign: |
| 3527 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 3528 | break; |
| 3529 | case BinaryOperator::Mul: |
| 3530 | case BinaryOperator::Div: |
| 3531 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 3532 | break; |
| 3533 | case BinaryOperator::Rem: |
| 3534 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 3535 | break; |
| 3536 | case BinaryOperator::Add: |
| 3537 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 3538 | break; |
| 3539 | case BinaryOperator::Sub: |
| 3540 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 3541 | break; |
| 3542 | case BinaryOperator::Shl: |
| 3543 | case BinaryOperator::Shr: |
| 3544 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 3545 | break; |
| 3546 | case BinaryOperator::LE: |
| 3547 | case BinaryOperator::LT: |
| 3548 | case BinaryOperator::GE: |
| 3549 | case BinaryOperator::GT: |
| 3550 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, true); |
| 3551 | break; |
| 3552 | case BinaryOperator::EQ: |
| 3553 | case BinaryOperator::NE: |
| 3554 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, false); |
| 3555 | break; |
| 3556 | case BinaryOperator::And: |
| 3557 | case BinaryOperator::Xor: |
| 3558 | case BinaryOperator::Or: |
| 3559 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 3560 | break; |
| 3561 | case BinaryOperator::LAnd: |
| 3562 | case BinaryOperator::LOr: |
| 3563 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 3564 | break; |
| 3565 | case BinaryOperator::MulAssign: |
| 3566 | case BinaryOperator::DivAssign: |
| 3567 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 3568 | if (!CompTy.isNull()) |
| 3569 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3570 | break; |
| 3571 | case BinaryOperator::RemAssign: |
| 3572 | CompTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 3573 | if (!CompTy.isNull()) |
| 3574 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3575 | break; |
| 3576 | case BinaryOperator::AddAssign: |
| 3577 | CompTy = CheckAdditionOperands(lhs, rhs, OpLoc, true); |
| 3578 | if (!CompTy.isNull()) |
| 3579 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3580 | break; |
| 3581 | case BinaryOperator::SubAssign: |
| 3582 | CompTy = CheckSubtractionOperands(lhs, rhs, OpLoc, true); |
| 3583 | if (!CompTy.isNull()) |
| 3584 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3585 | break; |
| 3586 | case BinaryOperator::ShlAssign: |
| 3587 | case BinaryOperator::ShrAssign: |
| 3588 | CompTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 3589 | if (!CompTy.isNull()) |
| 3590 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3591 | break; |
| 3592 | case BinaryOperator::AndAssign: |
| 3593 | case BinaryOperator::XorAssign: |
| 3594 | case BinaryOperator::OrAssign: |
| 3595 | CompTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 3596 | if (!CompTy.isNull()) |
| 3597 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3598 | break; |
| 3599 | case BinaryOperator::Comma: |
| 3600 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 3601 | break; |
| 3602 | } |
| 3603 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3604 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3605 | if (CompTy.isNull()) |
| 3606 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 3607 | else |
| 3608 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Steve Naroff | 9e0b600 | 2009-01-20 21:06:31 +0000 | [diff] [blame] | 3609 | CompTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3612 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3613 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 3614 | tok::TokenKind Kind, |
| 3615 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3616 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3617 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3618 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3619 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 3620 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3621 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3622 | // If either expression is type-dependent, just build the AST. |
| 3623 | // FIXME: We'll need to perform some caching of the result of name |
| 3624 | // lookup for operator+. |
| 3625 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3626 | if (Opc > BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) |
| 3627 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3628 | Context.DependentTy, |
| 3629 | Context.DependentTy, TokLoc)); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3630 | else |
| 3631 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, Context.DependentTy, |
| 3632 | TokLoc)); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3635 | if (getLangOptions().CPlusPlus && |
| 3636 | (lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType() || |
| 3637 | rhs->getType()->isRecordType() || rhs->getType()->isEnumeralType())) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3638 | // If this is one of the assignment operators, we only perform |
| 3639 | // overload resolution if the left-hand side is a class or |
| 3640 | // enumeration type (C++ [expr.ass]p3). |
| 3641 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3642 | !(lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType())) { |
| 3643 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
| 3644 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3646 | // Determine which overloaded operator we're dealing with. |
| 3647 | static const OverloadedOperatorKind OverOps[] = { |
| 3648 | OO_Star, OO_Slash, OO_Percent, |
| 3649 | OO_Plus, OO_Minus, |
| 3650 | OO_LessLess, OO_GreaterGreater, |
| 3651 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 3652 | OO_EqualEqual, OO_ExclaimEqual, |
| 3653 | OO_Amp, |
| 3654 | OO_Caret, |
| 3655 | OO_Pipe, |
| 3656 | OO_AmpAmp, |
| 3657 | OO_PipePipe, |
| 3658 | OO_Equal, OO_StarEqual, |
| 3659 | OO_SlashEqual, OO_PercentEqual, |
| 3660 | OO_PlusEqual, OO_MinusEqual, |
| 3661 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 3662 | OO_AmpEqual, OO_CaretEqual, |
| 3663 | OO_PipeEqual, |
| 3664 | OO_Comma |
| 3665 | }; |
| 3666 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3667 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3668 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 3669 | // to the candidate set. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3670 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3671 | Expr *Args[2] = { lhs, rhs }; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3672 | AddOperatorCandidates(OverOp, S, Args, 2, CandidateSet); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3673 | |
| 3674 | // Perform overload resolution. |
| 3675 | OverloadCandidateSet::iterator Best; |
| 3676 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3677 | case OR_Success: { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3678 | // We found a built-in operator or an overloaded operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3679 | FunctionDecl *FnDecl = Best->Function; |
| 3680 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3681 | if (FnDecl) { |
| 3682 | // We matched an overloaded operator. Build a call to that |
| 3683 | // operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3684 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3685 | // Convert the arguments. |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3686 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3687 | if (PerformObjectArgumentInitialization(lhs, Method) || |
| 3688 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(0)->getType(), |
| 3689 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3690 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3691 | } else { |
| 3692 | // Convert the arguments. |
| 3693 | if (PerformCopyInitialization(lhs, FnDecl->getParamDecl(0)->getType(), |
| 3694 | "passing") || |
| 3695 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(1)->getType(), |
| 3696 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3697 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3698 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3700 | // Determine the result type |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3701 | QualType ResultTy |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3702 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3703 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3704 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3705 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3706 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3707 | SourceLocation()); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 3708 | UsualUnaryConversions(FnExpr); |
| 3709 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3710 | return Owned(new (Context) CXXOperatorCallExpr(FnExpr, Args, 2, |
| 3711 | ResultTy, TokLoc)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3712 | } else { |
| 3713 | // We matched a built-in operator. Convert the arguments, then |
| 3714 | // break out so that we will build the appropriate built-in |
| 3715 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3716 | if (PerformImplicitConversion(lhs, Best->BuiltinTypes.ParamTypes[0], |
| 3717 | Best->Conversions[0], "passing") || |
| 3718 | PerformImplicitConversion(rhs, Best->BuiltinTypes.ParamTypes[1], |
| 3719 | Best->Conversions[1], "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3720 | return ExprError(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3721 | |
| 3722 | break; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3723 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | case OR_No_Viable_Function: |
| 3727 | // No viable function; fall through to handling this as a |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3728 | // built-in operator, which will produce an error message for us. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3729 | break; |
| 3730 | |
| 3731 | case OR_Ambiguous: |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3732 | Diag(TokLoc, diag::err_ovl_ambiguous_oper) |
| 3733 | << BinaryOperator::getOpcodeStr(Opc) |
| 3734 | << lhs->getSourceRange() << rhs->getSourceRange(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3735 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3736 | return ExprError(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3739 | // Either we found no viable overloaded operator or we matched a |
| 3740 | // built-in operator. In either case, fall through to trying to |
| 3741 | // build a built-in operation. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3744 | // Build a built-in binary operation. |
| 3745 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | // Unary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3749 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 3750 | tok::TokenKind Op, ExprArg input) { |
| 3751 | // FIXME: Input is modified later, but smart pointer not reassigned. |
| 3752 | Expr *Input = (Expr*)input.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3753 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3754 | |
| 3755 | if (getLangOptions().CPlusPlus && |
| 3756 | (Input->getType()->isRecordType() |
| 3757 | || Input->getType()->isEnumeralType())) { |
| 3758 | // Determine which overloaded operator we're dealing with. |
| 3759 | static const OverloadedOperatorKind OverOps[] = { |
| 3760 | OO_None, OO_None, |
| 3761 | OO_PlusPlus, OO_MinusMinus, |
| 3762 | OO_Amp, OO_Star, |
| 3763 | OO_Plus, OO_Minus, |
| 3764 | OO_Tilde, OO_Exclaim, |
| 3765 | OO_None, OO_None, |
| 3766 | OO_None, |
| 3767 | OO_None |
| 3768 | }; |
| 3769 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3770 | |
| 3771 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 3772 | // to the candidate set. |
| 3773 | OverloadCandidateSet CandidateSet; |
| 3774 | if (OverOp != OO_None) |
| 3775 | AddOperatorCandidates(OverOp, S, &Input, 1, CandidateSet); |
| 3776 | |
| 3777 | // Perform overload resolution. |
| 3778 | OverloadCandidateSet::iterator Best; |
| 3779 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3780 | case OR_Success: { |
| 3781 | // We found a built-in operator or an overloaded operator. |
| 3782 | FunctionDecl *FnDecl = Best->Function; |
| 3783 | |
| 3784 | if (FnDecl) { |
| 3785 | // We matched an overloaded operator. Build a call to that |
| 3786 | // operator. |
| 3787 | |
| 3788 | // Convert the arguments. |
| 3789 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3790 | if (PerformObjectArgumentInitialization(Input, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3791 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3792 | } else { |
| 3793 | // Convert the arguments. |
| 3794 | if (PerformCopyInitialization(Input, |
| 3795 | FnDecl->getParamDecl(0)->getType(), |
| 3796 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3797 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3801 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3802 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3803 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3805 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3806 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3807 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3808 | UsualUnaryConversions(FnExpr); |
| 3809 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3810 | input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3811 | return Owned(new (Context) CXXOperatorCallExpr(FnExpr, &Input, 1, |
| 3812 | ResultTy, OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3813 | } else { |
| 3814 | // We matched a built-in operator. Convert the arguments, then |
| 3815 | // break out so that we will build the appropriate built-in |
| 3816 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3817 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 3818 | Best->Conversions[0], "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3819 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3820 | |
| 3821 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3822 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | case OR_No_Viable_Function: |
| 3826 | // No viable function; fall through to handling this as a |
| 3827 | // built-in operator, which will produce an error message for us. |
| 3828 | break; |
| 3829 | |
| 3830 | case OR_Ambiguous: |
| 3831 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 3832 | << UnaryOperator::getOpcodeStr(Opc) |
| 3833 | << Input->getSourceRange(); |
| 3834 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3835 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | // Either we found no viable overloaded operator or we matched a |
| 3839 | // built-in operator. In either case, fall through to trying to |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3840 | // build a built-in operation. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3843 | QualType resultType; |
| 3844 | switch (Opc) { |
| 3845 | default: |
| 3846 | assert(0 && "Unimplemented unary expr!"); |
| 3847 | case UnaryOperator::PreInc: |
| 3848 | case UnaryOperator::PreDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3849 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 3850 | Opc == UnaryOperator::PreInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3851 | break; |
| 3852 | case UnaryOperator::AddrOf: |
| 3853 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 3854 | break; |
| 3855 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 3856 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3857 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 3858 | break; |
| 3859 | case UnaryOperator::Plus: |
| 3860 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3861 | UsualUnaryConversions(Input); |
| 3862 | resultType = Input->getType(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3863 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 3864 | break; |
| 3865 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 3866 | resultType->isEnumeralType()) |
| 3867 | break; |
| 3868 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 3869 | Opc == UnaryOperator::Plus && |
| 3870 | resultType->isPointerType()) |
| 3871 | break; |
| 3872 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3873 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 3874 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3875 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3876 | UsualUnaryConversions(Input); |
| 3877 | resultType = Input->getType(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 3878 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 3879 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 3880 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3881 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3882 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 3883 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3884 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 3885 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3886 | break; |
| 3887 | case UnaryOperator::LNot: // logical negation |
| 3888 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3889 | DefaultFunctionArrayConversion(Input); |
| 3890 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3891 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3892 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 3893 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3894 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3895 | // In C++, it's bool. C++ 5.3.1p8 |
| 3896 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3898 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3899 | case UnaryOperator::Imag: |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 3900 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3901 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3902 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3903 | resultType = Input->getType(); |
| 3904 | break; |
| 3905 | } |
| 3906 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3907 | return ExprError(); |
| 3908 | input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3909 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 3912 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 3913 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3914 | SourceLocation LabLoc, |
| 3915 | IdentifierInfo *LabelII) { |
| 3916 | // Look up the record for this label identifier. |
| 3917 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 3918 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 3919 | // If we haven't seen this label yet, create a forward reference. It |
| 3920 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3921 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3922 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3923 | |
| 3924 | // Create the AST node. The address of a label always has type 'void*'. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3925 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 3926 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 3929 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 3930 | SourceLocation RPLoc) { // "({..})" |
| 3931 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 3932 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 3933 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 3934 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 3935 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 3936 | if (isFileScope) { |
| 3937 | return Diag(LPLoc, diag::err_stmtexpr_file_scope); |
| 3938 | } |
| 3939 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 3940 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 3941 | // example, it is not possible to goto into a stmt expression apparently. |
| 3942 | // More semantic analysis is needed. |
| 3943 | |
| 3944 | // FIXME: the last statement in the compount stmt has its value used. We |
| 3945 | // should not warn about it being unused. |
| 3946 | |
| 3947 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 3948 | // as the type of the stmtexpr. |
| 3949 | QualType Ty = Context.VoidTy; |
| 3950 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 3951 | if (!Compound->body_empty()) { |
| 3952 | Stmt *LastStmt = Compound->body_back(); |
| 3953 | // If LastStmt is a label, skip down through into the body. |
| 3954 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 3955 | LastStmt = Label->getSubStmt(); |
| 3956 | |
| 3957 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 3958 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 3959 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 3960 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3961 | return new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 3962 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 3964 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 3965 | SourceLocation BuiltinLoc, |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3966 | SourceLocation TypeLoc, |
| 3967 | TypeTy *argty, |
| 3968 | OffsetOfComponent *CompPtr, |
| 3969 | unsigned NumComponents, |
| 3970 | SourceLocation RPLoc) { |
| 3971 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 3972 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 3973 | |
| 3974 | // We must have at least one component that refers to the type, and the first |
| 3975 | // one is known to be a field designator. Verify that the ArgTy represents |
| 3976 | // a struct/union/class. |
| 3977 | if (!ArgTy->isRecordType()) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3978 | return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3979 | |
| 3980 | // Otherwise, create a compound literal expression as the base, and |
| 3981 | // iteratively process the offsetof designators. |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 3982 | InitListExpr *IList = |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3983 | new (Context) InitListExpr(SourceLocation(), 0, 0, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 3984 | IList->setType(ArgTy); |
| 3985 | Expr *Res = |
| 3986 | new (Context) CompoundLiteralExpr(SourceLocation(), ArgTy, IList, false); |
| 3987 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 3988 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 3989 | // GCC extension, diagnose them. |
| 3990 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3991 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 3992 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 3993 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3994 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 3995 | const OffsetOfComponent &OC = CompPtr[i]; |
| 3996 | if (OC.isBrackets) { |
| 3997 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3998 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3999 | if (!AT) { |
| 4000 | delete Res; |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4001 | return Diag(OC.LocEnd, diag::err_offsetof_array_type) << Res->getType(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4004 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4005 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4006 | // C99 6.5.2.1p1 |
| 4007 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 4008 | if (!Idx->getType()->isIntegerType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4009 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript) |
| 4010 | << Idx->getSourceRange(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4011 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4012 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4013 | OC.LocEnd); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4014 | continue; |
| 4015 | } |
| 4016 | |
| 4017 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4018 | if (!RC) { |
| 4019 | delete Res; |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4020 | return Diag(OC.LocEnd, diag::err_offsetof_record_type) << Res->getType(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | // Get the decl corresponding to this. |
| 4024 | RecordDecl *RD = RC->getDecl(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4025 | FieldDecl *MemberDecl |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 4026 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4027 | LookupMemberName) |
| 4028 | .getAsDecl()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4029 | if (!MemberDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 4030 | return Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4031 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd); |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4032 | |
| 4033 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4034 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 4035 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4036 | // matter here. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4037 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 4038 | MemberDecl->getType().getNonReferenceType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4041 | return new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 4042 | Context.getSizeType(), BuiltinLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4046 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4047 | TypeTy *arg1, TypeTy *arg2, |
| 4048 | SourceLocation RPLoc) { |
| 4049 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4050 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 4051 | |
| 4052 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 4053 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4054 | return new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, |
| 4055 | argT2, RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4058 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4059 | ExprTy *expr1, ExprTy *expr2, |
| 4060 | SourceLocation RPLoc) { |
| 4061 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 4062 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 4063 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 4064 | |
| 4065 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4066 | |
| 4067 | // The conditional expression is required to be a constant expression. |
| 4068 | llvm::APSInt condEval(32); |
| 4069 | SourceLocation ExpLoc; |
| 4070 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4071 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant) |
| 4072 | << CondExpr->getSourceRange(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4073 | |
| 4074 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4075 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 4076 | RHSExpr->getType(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4077 | return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 4078 | resType, RPLoc); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4081 | //===----------------------------------------------------------------------===// |
| 4082 | // Clang Extensions. |
| 4083 | //===----------------------------------------------------------------------===// |
| 4084 | |
| 4085 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4086 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4087 | // Analyze block parameters. |
| 4088 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
| 4089 | |
| 4090 | // Add BSI to CurBlock. |
| 4091 | BSI->PrevBlockInfo = CurBlock; |
| 4092 | CurBlock = BSI; |
| 4093 | |
| 4094 | BSI->ReturnType = 0; |
| 4095 | BSI->TheScope = BlockScope; |
| 4096 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4097 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4098 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | void Sema::ActOnBlockArguments(Declarator &ParamInfo) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4102 | // Analyze arguments to block. |
| 4103 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4104 | "Not a function declarator!"); |
| 4105 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
| 4106 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4107 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4108 | CurBlock->isVariadic = true; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4109 | |
| 4110 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4111 | // no arguments, not a function that takes a single void argument. |
| 4112 | if (FTI.hasPrototype && |
| 4113 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4114 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 4115 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 4116 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4117 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4118 | } else if (FTI.hasPrototype) { |
| 4119 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4120 | CurBlock->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 4121 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4122 | } |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4123 | CurBlock->TheDecl->setArgs(&CurBlock->Params[0], CurBlock->Params.size()); |
| 4124 | |
| 4125 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4126 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4127 | // If this has an identifier, add it to the scope stack. |
| 4128 | if ((*AI)->getIdentifier()) |
| 4129 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4133 | /// is invoked to pop the information about the block from the action impl. |
| 4134 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4135 | // Ensure that CurBlock is deleted. |
| 4136 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
| 4137 | |
| 4138 | // Pop off CurBlock, handle nested blocks. |
| 4139 | CurBlock = CurBlock->PrevBlockInfo; |
| 4140 | |
| 4141 | // FIXME: Delete the ParmVarDecl objects as well??? |
| 4142 | |
| 4143 | } |
| 4144 | |
| 4145 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4146 | /// literal was successfully completed. ^(int x){...} |
| 4147 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 4148 | Scope *CurScope) { |
| 4149 | // Ensure that CurBlock is deleted. |
| 4150 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
| 4151 | llvm::OwningPtr<CompoundStmt> Body(static_cast<CompoundStmt*>(body)); |
| 4152 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4153 | PopDeclContext(); |
| 4154 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4155 | // Pop off CurBlock, handle nested blocks. |
| 4156 | CurBlock = CurBlock->PrevBlockInfo; |
| 4157 | |
| 4158 | QualType RetTy = Context.VoidTy; |
| 4159 | if (BSI->ReturnType) |
| 4160 | RetTy = QualType(BSI->ReturnType, 0); |
| 4161 | |
| 4162 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4163 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4164 | ArgTypes.push_back(BSI->Params[i]->getType()); |
| 4165 | |
| 4166 | QualType BlockTy; |
| 4167 | if (!BSI->hasPrototype) |
| 4168 | BlockTy = Context.getFunctionTypeNoProto(RetTy); |
| 4169 | else |
| 4170 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4171 | BSI->isVariadic, 0); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4172 | |
| 4173 | BlockTy = Context.getBlockPointerType(BlockTy); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4174 | |
Steve Naroff | 1c90bfc | 2008-10-08 18:44:00 +0000 | [diff] [blame] | 4175 | BSI->TheDecl->setBody(Body.take()); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4176 | return new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4179 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4180 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4181 | /// The number of arguments has already been validated to match the number of |
| 4182 | /// arguments in FnType. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4183 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType, |
| 4184 | ASTContext &Context) { |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4185 | unsigned NumParams = FnType->getNumArgs(); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4186 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4187 | QualType ExprTy = Context.getCanonicalType(Args[i]->getType()); |
| 4188 | QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i)); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4189 | |
| 4190 | if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType()) |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4191 | return false; |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4192 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4193 | return true; |
| 4194 | } |
| 4195 | |
| 4196 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 4197 | SourceLocation *CommaLocs, |
| 4198 | SourceLocation BuiltinLoc, |
| 4199 | SourceLocation RParenLoc) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4200 | // __builtin_overload requires at least 2 arguments |
| 4201 | if (NumArgs < 2) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4202 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4203 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4204 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4205 | // The first argument is required to be a constant expression. It tells us |
| 4206 | // the number of arguments to pass to each of the functions to be overloaded. |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4207 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4208 | Expr *NParamsExpr = Args[0]; |
| 4209 | llvm::APSInt constEval(32); |
| 4210 | SourceLocation ExpLoc; |
| 4211 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4212 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4213 | << NParamsExpr->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4214 | |
| 4215 | // Verify that the number of parameters is > 0 |
| 4216 | unsigned NumParams = constEval.getZExtValue(); |
| 4217 | if (NumParams == 0) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4218 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4219 | << NParamsExpr->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4220 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 4221 | if ((NumParams + 1) > NumArgs) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4222 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4223 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4224 | |
| 4225 | // Figure out the return type, by matching the args to one of the functions |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4226 | // listed after the parameters. |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4227 | OverloadExpr *OE = 0; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4228 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 4229 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 4230 | // pointer to function. |
| 4231 | Expr *Fn = UsualUnaryConversions(Args[i]); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4232 | const FunctionTypeProto *FnType = 0; |
| 4233 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) |
| 4234 | FnType = PT->getPointeeType()->getAsFunctionTypeProto(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4235 | |
| 4236 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 4237 | // parameters, and the number of parameters must match the value passed to |
| 4238 | // the builtin. |
| 4239 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4240 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype) |
| 4241 | << Fn->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4242 | |
| 4243 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4244 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4245 | // If they match, return a new OverloadExpr. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4246 | if (ExprsMatchFnType(Args+1, FnType, Context)) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4247 | if (OE) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4248 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match) |
| 4249 | << OE->getFn()->getSourceRange(); |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4250 | // Remember our match, and continue processing the remaining arguments |
| 4251 | // to catch any errors. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4252 | OE = new (Context) OverloadExpr(Args, NumArgs, i, |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4253 | FnType->getResultType().getNonReferenceType(), |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4254 | BuiltinLoc, RParenLoc); |
| 4255 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4256 | } |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4257 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 4258 | // exactly one of the candidate functions. |
| 4259 | if (OE) |
| 4260 | return OE; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4261 | |
| 4262 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 4263 | // the return an error. |
| 4264 | std::string typeNames; |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4265 | for (unsigned i = 0; i != NumParams; ++i) { |
| 4266 | if (i != 0) typeNames += ", "; |
| 4267 | typeNames += Args[i+1]->getType().getAsString(); |
| 4268 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4269 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4270 | return Diag(BuiltinLoc, diag::err_overload_no_match) |
| 4271 | << typeNames << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4274 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4275 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4276 | SourceLocation RPLoc) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4277 | Expr *E = static_cast<Expr*>(expr); |
| 4278 | QualType T = QualType::getFromOpaquePtr(type); |
| 4279 | |
| 4280 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4281 | |
| 4282 | // Get the va_list type |
| 4283 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4284 | // Deal with implicit array decay; for example, on x86-64, |
| 4285 | // va_list is an array, but it's supposed to decay to |
| 4286 | // a pointer for va_arg. |
| 4287 | if (VaListType->isArrayType()) |
| 4288 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | efbe85c | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4289 | // Make sure the input expression also decays appropriately. |
| 4290 | UsualUnaryConversions(E); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4291 | |
| 4292 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4293 | return Diag(E->getLocStart(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4294 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4295 | << E->getType() << E->getSourceRange(); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4296 | |
| 4297 | // FIXME: Warn if a non-POD type is passed in. |
| 4298 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4299 | return new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), RPLoc); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4302 | Sema::ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| 4303 | // The type of __null will be int or long, depending on the size of |
| 4304 | // pointers on the target. |
| 4305 | QualType Ty; |
| 4306 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4307 | Ty = Context.IntTy; |
| 4308 | else |
| 4309 | Ty = Context.LongTy; |
| 4310 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4311 | return new (Context) GNUNullExpr(Ty, TokenLoc); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4314 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4315 | SourceLocation Loc, |
| 4316 | QualType DstType, QualType SrcType, |
| 4317 | Expr *SrcExpr, const char *Flavor) { |
| 4318 | // Decode the result (notice that AST's are still created for extensions). |
| 4319 | bool isInvalid = false; |
| 4320 | unsigned DiagKind; |
| 4321 | switch (ConvTy) { |
| 4322 | default: assert(0 && "Unknown conversion type"); |
| 4323 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4324 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4325 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4326 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4327 | case IntToPointer: |
| 4328 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4329 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4330 | case IncompatiblePointer: |
| 4331 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4332 | break; |
| 4333 | case FunctionVoidPointer: |
| 4334 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4335 | break; |
| 4336 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4337 | // If the qualifiers lost were because we were applying the |
| 4338 | // (deprecated) C++ conversion from a string literal to a char* |
| 4339 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4340 | // Ideally, this check would be performed in |
| 4341 | // CheckPointerTypesForAssignment. However, that would require a |
| 4342 | // bit of refactoring (so that the second argument is an |
| 4343 | // expression, rather than a type), which should be done as part |
| 4344 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4345 | // C++ semantics. |
| 4346 | if (getLangOptions().CPlusPlus && |
| 4347 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4348 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4349 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4350 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4351 | case IntToBlockPointer: |
| 4352 | DiagKind = diag::err_int_to_block_pointer; |
| 4353 | break; |
| 4354 | case IncompatibleBlockPointer: |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4355 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4356 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4357 | case IncompatibleObjCQualifiedId: |
| 4358 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
| 4359 | // it can give a more specific diagnostic. |
| 4360 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4361 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4362 | case Incompatible: |
| 4363 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4364 | isInvalid = true; |
| 4365 | break; |
| 4366 | } |
| 4367 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4368 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4369 | << SrcExpr->getSourceRange(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4370 | return isInvalid; |
| 4371 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4372 | |
| 4373 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4374 | { |
| 4375 | Expr::EvalResult EvalResult; |
| 4376 | |
| 4377 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
| 4378 | EvalResult.HasSideEffects) { |
| 4379 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4380 | |
| 4381 | if (EvalResult.Diag) { |
| 4382 | // We only show the note if it's not the usual "invalid subexpression" |
| 4383 | // or if it's actually in a subexpression. |
| 4384 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4385 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4386 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4387 | } |
| 4388 | |
| 4389 | return true; |
| 4390 | } |
| 4391 | |
| 4392 | if (EvalResult.Diag) { |
| 4393 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 4394 | E->getSourceRange(); |
| 4395 | |
| 4396 | // Print the reason it's not a constant. |
| 4397 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4398 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4399 | } |
| 4400 | |
| 4401 | if (Result) |
| 4402 | *Result = EvalResult.Val.getInt(); |
| 4403 | return false; |
| 4404 | } |