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