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