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