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