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