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