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