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