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