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