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