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 | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 29 | |
| 30 | /// DiagnoseUseOfDeprecatedDeclImpl - If the specified decl is deprecated or |
| 31 | // unavailable, emit the corresponding diagnostics. |
| 32 | void Sema::DiagnoseUseOfDeprecatedDeclImpl(NamedDecl *D, SourceLocation Loc) { |
| 33 | // See if the decl is deprecated. |
| 34 | if (D->getAttr<DeprecatedAttr>()) { |
| 35 | // If this reference happens *in* a deprecated function or method, don't |
| 36 | // warn. Implementing deprecated stuff requires referencing depreated |
| 37 | // stuff. |
| 38 | NamedDecl *ND = getCurFunctionOrMethodDecl(); |
| 39 | if (ND == 0 || !ND->getAttr<DeprecatedAttr>()) |
| 40 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 41 | } |
| 42 | |
| 43 | // See if hte decl is unavailable. |
| 44 | if (D->getAttr<UnavailableAttr>()) |
| 45 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 46 | } |
| 47 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Standard Promotions and Conversions |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 52 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 53 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 54 | QualType Ty = E->getType(); |
| 55 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 56 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 57 | if (Ty->isFunctionType()) |
| 58 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 59 | else if (Ty->isArrayType()) { |
| 60 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 61 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 62 | // type 'array of type' is converted to an expression that has type 'pointer |
| 63 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 64 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 65 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 66 | // |
| 67 | // C++ 4.2p1: |
| 68 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 69 | // T" can be converted to an rvalue of type "pointer to T". |
| 70 | // |
| 71 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 72 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 73 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 74 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 78 | /// operators (C99 6.3). The conversions of array and function types are |
| 79 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 80 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 81 | /// In these instances, this routine should *not* be called. |
| 82 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 83 | QualType Ty = Expr->getType(); |
| 84 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 85 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 86 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 87 | ImpCastExprToType(Expr, Context.IntTy); |
| 88 | else |
| 89 | DefaultFunctionArrayConversion(Expr); |
| 90 | |
| 91 | return Expr; |
| 92 | } |
| 93 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 94 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 95 | /// do not have a prototype. Arguments that have type float are promoted to |
| 96 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 97 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 98 | QualType Ty = Expr->getType(); |
| 99 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 100 | |
| 101 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 102 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 103 | if (BT->getKind() == BuiltinType::Float) |
| 104 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 105 | |
| 106 | UsualUnaryConversions(Expr); |
| 107 | } |
| 108 | |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 109 | // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 110 | // will warn if the resulting type is not a POD type. |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 111 | void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 112 | DefaultArgumentPromotion(Expr); |
| 113 | |
| 114 | if (!Expr->getType()->isPODType()) { |
| 115 | Diag(Expr->getLocStart(), |
| 116 | diag::warn_cannot_pass_non_pod_arg_to_vararg) << |
| 117 | Expr->getType() << CT; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 122 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 123 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 124 | /// routine returns the first non-arithmetic type found. The client is |
| 125 | /// responsible for emitting appropriate error diagnostics. |
| 126 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 127 | /// GCC. |
| 128 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 129 | bool isCompAssign) { |
| 130 | if (!isCompAssign) { |
| 131 | UsualUnaryConversions(lhsExpr); |
| 132 | UsualUnaryConversions(rhsExpr); |
| 133 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 134 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 135 | // For conversion purposes, we ignore any qualifiers. |
| 136 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 137 | QualType lhs = |
| 138 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 139 | QualType rhs = |
| 140 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 141 | |
| 142 | // If both types are identical, no conversion is needed. |
| 143 | if (lhs == rhs) |
| 144 | return lhs; |
| 145 | |
| 146 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 147 | // The caller can deal with this (e.g. pointer + int). |
| 148 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 149 | return lhs; |
| 150 | |
| 151 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
| 152 | if (!isCompAssign) { |
| 153 | ImpCastExprToType(lhsExpr, destType); |
| 154 | ImpCastExprToType(rhsExpr, destType); |
| 155 | } |
| 156 | return destType; |
| 157 | } |
| 158 | |
| 159 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 160 | // Perform the usual unary conversions. We do this early so that |
| 161 | // integral promotions to "int" can allow us to exit early, in the |
| 162 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 163 | // qualifiers. For example, "const float" and "float" are |
| 164 | // equivalent. |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 165 | if (lhs->isPromotableIntegerType()) |
| 166 | lhs = Context.IntTy; |
| 167 | else |
| 168 | lhs = lhs.getUnqualifiedType(); |
| 169 | if (rhs->isPromotableIntegerType()) |
| 170 | rhs = Context.IntTy; |
| 171 | else |
| 172 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 173 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 174 | // If both types are identical, no conversion is needed. |
| 175 | if (lhs == rhs) |
| 176 | return lhs; |
| 177 | |
| 178 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 179 | // The caller can deal with this (e.g. pointer + int). |
| 180 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 181 | return lhs; |
| 182 | |
| 183 | // At this point, we have two different arithmetic types. |
| 184 | |
| 185 | // Handle complex types first (C99 6.3.1.8p1). |
| 186 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 187 | // if we have an integer operand, the result is the complex type. |
| 188 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 189 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 190 | return lhs; |
| 191 | } |
| 192 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 193 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 194 | return rhs; |
| 195 | } |
| 196 | // This handles complex/complex, complex/float, or float/complex. |
| 197 | // When both operands are complex, the shorter operand is converted to the |
| 198 | // type of the longer, and that is the type of the result. This corresponds |
| 199 | // to what is done when combining two real floating-point operands. |
| 200 | // The fun begins when size promotion occur across type domains. |
| 201 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 202 | // floating-point type, the less precise type is converted, within it's |
| 203 | // real or complex domain, to the precision of the other type. For example, |
| 204 | // when combining a "long double" with a "double _Complex", the |
| 205 | // "double _Complex" is promoted to "long double _Complex". |
| 206 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 207 | |
| 208 | if (result > 0) { // The left side is bigger, convert rhs. |
| 209 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 210 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 211 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 212 | } |
| 213 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 214 | // domains match. This is a requirement for our implementation, C99 |
| 215 | // does not require this promotion. |
| 216 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 217 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 218 | return rhs; |
| 219 | } else { // handle "_Complex double, double". |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 220 | return lhs; |
| 221 | } |
| 222 | } |
| 223 | return lhs; // The domain/size match exactly. |
| 224 | } |
| 225 | // Now handle "real" floating types (i.e. float, double, long double). |
| 226 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 227 | // 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] | 228 | if (rhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 229 | // convert rhs to the lhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 230 | return lhs; |
| 231 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 232 | if (rhs->isComplexIntegerType()) { |
| 233 | // convert rhs to the complex floating point type. |
| 234 | return Context.getComplexType(lhs); |
| 235 | } |
| 236 | if (lhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 237 | // convert lhs to the rhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 238 | return rhs; |
| 239 | } |
Anders Carlsson | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 240 | if (lhs->isComplexIntegerType()) { |
| 241 | // convert lhs to the complex floating point type. |
| 242 | return Context.getComplexType(rhs); |
| 243 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 244 | // We have two real floating types, float/complex combos were handled above. |
| 245 | // Convert the smaller operand to the bigger result. |
| 246 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 247 | if (result > 0) // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 248 | return lhs; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 249 | assert(result < 0 && "illegal float comparison"); |
| 250 | return rhs; // convert the lhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 251 | } |
| 252 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 253 | // Handle GCC complex int extension. |
| 254 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 255 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 256 | |
| 257 | if (lhsComplexInt && rhsComplexInt) { |
| 258 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 259 | rhsComplexInt->getElementType()) >= 0) |
| 260 | return lhs; // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 261 | return rhs; |
| 262 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 263 | // convert the rhs to the lhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 264 | return lhs; |
| 265 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 266 | // convert the lhs to the rhs complex type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 267 | return rhs; |
| 268 | } |
| 269 | } |
| 270 | // Finally, we have two differing integer types. |
| 271 | // The rules for this case are in C99 6.3.1.8 |
| 272 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 273 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 274 | rhsSigned = rhs->isSignedIntegerType(); |
| 275 | QualType destType; |
| 276 | if (lhsSigned == rhsSigned) { |
| 277 | // Same signedness; use the higher-ranked type |
| 278 | destType = compare >= 0 ? lhs : rhs; |
| 279 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 280 | // The unsigned type has greater than or equal rank to the |
| 281 | // signed type, so use the unsigned type |
| 282 | destType = lhsSigned ? rhs : lhs; |
| 283 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 284 | // The two types are different widths; if we are here, that |
| 285 | // means the signed type is larger than the unsigned type, so |
| 286 | // use the signed type. |
| 287 | destType = lhsSigned ? lhs : rhs; |
| 288 | } else { |
| 289 | // The signed type is higher-ranked than the unsigned type, |
| 290 | // but isn't actually any bigger (like unsigned int and long |
| 291 | // on most 32-bit systems). Use the unsigned type corresponding |
| 292 | // to the signed type. |
| 293 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 294 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 295 | return destType; |
| 296 | } |
| 297 | |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | // Semantic Analysis for various Expression Types |
| 300 | //===----------------------------------------------------------------------===// |
| 301 | |
| 302 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 303 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 305 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 306 | /// multiple tokens. However, the common case is that StringToks points to one |
| 307 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 308 | /// |
| 309 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 310 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | assert(NumStringToks && "Must have at least one string!"); |
| 312 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 313 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 315 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | |
| 317 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 318 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 319 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 320 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 321 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 322 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 323 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 324 | |
| 325 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 326 | if (getLangOptions().CPlusPlus) |
| 327 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 328 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 329 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 330 | // the nul terminator character as well as the string length for pascal |
| 331 | // strings. |
| 332 | StrTy = Context.getConstantArrayType(StrTy, |
| 333 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 334 | ArrayType::Normal, 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 335 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 337 | return Owned(new (Context) StringLiteral(Context, Literal.GetString(), |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 338 | Literal.GetStringLength(), |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 339 | Literal.AnyWide, StrTy, |
| 340 | StringToks[0].getLocation(), |
| 341 | StringToks[NumStringToks-1].getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 344 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 345 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 346 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 347 | /// for values inside the block or for globals). |
| 348 | /// |
| 349 | /// FIXME: This will create BlockDeclRefExprs for global variables, |
| 350 | /// function references, etc which is suboptimal :) and breaks |
| 351 | /// things like "integer constant expression" tests. |
| 352 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 353 | ValueDecl *VD) { |
| 354 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 355 | // we wanted to. |
| 356 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 357 | return false; |
| 358 | |
| 359 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 360 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 361 | return false; |
| 362 | |
| 363 | // If this is a reference to an extern, static, or global variable, no need to |
| 364 | // snapshot it. |
| 365 | // FIXME: What about 'const' variables in C++? |
| 366 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
| 367 | return Var->hasLocalStorage(); |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 374 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | 0d755ad | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 376 | /// identifier is used in a function call context. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 377 | /// 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] | 378 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 379 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 380 | IdentifierInfo &II, |
| 381 | bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 382 | const CXXScopeSpec *SS, |
| 383 | bool isAddressOfOperand) { |
| 384 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 385 | isAddressOfOperand); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 388 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 389 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 390 | /// nested-name-specifier. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 391 | DeclRefExpr * |
| 392 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 393 | bool TypeDependent, bool ValueDependent, |
| 394 | const CXXScopeSpec *SS) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 395 | if (SS && !SS->isEmpty()) |
| 396 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 397 | ValueDependent, SS->getRange().getBegin()); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 398 | else |
| 399 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 402 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 403 | /// variable corresponding to the anonymous union or struct whose type |
| 404 | /// is Record. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 405 | static Decl *getObjectForAnonymousRecordDecl(RecordDecl *Record) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 406 | assert(Record->isAnonymousStructOrUnion() && |
| 407 | "Record must be an anonymous struct or union!"); |
| 408 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 409 | // FIXME: Once Decls are directly linked together, this will |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 410 | // be an O(1) operation rather than a slow walk through DeclContext's |
| 411 | // vector (which itself will be eliminated). DeclGroups might make |
| 412 | // this even better. |
| 413 | DeclContext *Ctx = Record->getDeclContext(); |
| 414 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 415 | DEnd = Ctx->decls_end(); |
| 416 | D != DEnd; ++D) { |
| 417 | if (*D == Record) { |
| 418 | // The object for the anonymous struct/union directly |
| 419 | // follows its type in the list of declarations. |
| 420 | ++D; |
| 421 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 422 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 423 | return *D; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | assert(false && "Missing object for anonymous record"); |
| 428 | return 0; |
| 429 | } |
| 430 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 431 | Sema::OwningExprResult |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 432 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 433 | FieldDecl *Field, |
| 434 | Expr *BaseObjectExpr, |
| 435 | SourceLocation OpLoc) { |
| 436 | assert(Field->getDeclContext()->isRecord() && |
| 437 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 438 | && "Field must be stored inside an anonymous struct or union"); |
| 439 | |
| 440 | // Construct the sequence of field member references |
| 441 | // we'll have to perform to get to the field in the anonymous |
| 442 | // union/struct. The list of members is built from the field |
| 443 | // outward, so traverse it backwards to go from an object in |
| 444 | // the current context to the field we found. |
| 445 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 446 | AnonFields.push_back(Field); |
| 447 | VarDecl *BaseObject = 0; |
| 448 | DeclContext *Ctx = Field->getDeclContext(); |
| 449 | do { |
| 450 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 451 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 452 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
| 453 | AnonFields.push_back(AnonField); |
| 454 | else { |
| 455 | BaseObject = cast<VarDecl>(AnonObject); |
| 456 | break; |
| 457 | } |
| 458 | Ctx = Ctx->getParent(); |
| 459 | } while (Ctx->isRecord() && |
| 460 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
| 461 | |
| 462 | // Build the expression that refers to the base object, from |
| 463 | // which we will build a sequence of member references to each |
| 464 | // of the anonymous union objects and, eventually, the field we |
| 465 | // found via name lookup. |
| 466 | bool BaseObjectIsPointer = false; |
| 467 | unsigned ExtraQuals = 0; |
| 468 | if (BaseObject) { |
| 469 | // BaseObject is an anonymous struct/union variable (and is, |
| 470 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 471 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 472 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 473 | SourceLocation()); |
| 474 | ExtraQuals |
| 475 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 476 | } else if (BaseObjectExpr) { |
| 477 | // The caller provided the base object expression. Determine |
| 478 | // whether its a pointer and whether it adds any qualifiers to the |
| 479 | // anonymous struct/union fields we're looking into. |
| 480 | QualType ObjectType = BaseObjectExpr->getType(); |
| 481 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 482 | BaseObjectIsPointer = true; |
| 483 | ObjectType = ObjectPtr->getPointeeType(); |
| 484 | } |
| 485 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 486 | } else { |
| 487 | // We've found a member of an anonymous struct/union that is |
| 488 | // inside a non-anonymous struct/union, so in a well-formed |
| 489 | // program our base object expression is "this". |
| 490 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 491 | if (!MD->isStatic()) { |
| 492 | QualType AnonFieldType |
| 493 | = Context.getTagDeclType( |
| 494 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 495 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 496 | if ((Context.getCanonicalType(AnonFieldType) |
| 497 | == Context.getCanonicalType(ThisType)) || |
| 498 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 499 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 500 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 501 | MD->getThisType(Context)); |
| 502 | BaseObjectIsPointer = true; |
| 503 | } |
| 504 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 505 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 506 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 507 | } |
| 508 | ExtraQuals = MD->getTypeQualifiers(); |
| 509 | } |
| 510 | |
| 511 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 512 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 513 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Build the implicit member references to the field of the |
| 517 | // anonymous struct/union. |
| 518 | Expr *Result = BaseObjectExpr; |
| 519 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 520 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 521 | FI != FIEnd; ++FI) { |
| 522 | QualType MemberType = (*FI)->getType(); |
| 523 | if (!(*FI)->isMutable()) { |
| 524 | unsigned combinedQualifiers |
| 525 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 526 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 527 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 528 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 529 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 530 | BaseObjectIsPointer = false; |
| 531 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
| 532 | OpLoc = SourceLocation(); |
| 533 | } |
| 534 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 535 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 538 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 539 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 540 | /// performs lookup on that name and returns an expression that refers |
| 541 | /// to that name. This routine isn't directly called from the parser, |
| 542 | /// because the parser doesn't know about DeclarationName. Rather, |
| 543 | /// this routine is called by ActOnIdentifierExpr, |
| 544 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 545 | /// which form the DeclarationName from the corresponding syntactic |
| 546 | /// forms. |
| 547 | /// |
| 548 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 549 | /// function call context. LookupCtx is only used for a C++ |
| 550 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 551 | /// the identifier must be a member of. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 552 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 553 | /// isAddressOfOperand means that this expression is the direct operand |
| 554 | /// of an address-of operator. This matters because this is the only |
| 555 | /// situation where a qualified name referencing a non-static member may |
| 556 | /// appear outside a member function of this class. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 557 | Sema::OwningExprResult |
| 558 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 559 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 560 | const CXXScopeSpec *SS, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 561 | bool isAddressOfOperand) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 562 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 563 | if (SS && SS->isInvalid()) |
| 564 | return ExprError(); |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 565 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 566 | false, true, Loc); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 568 | if (getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 569 | HasTrailingLParen && Lookup.getKind() == LookupResult::NotFound) { |
| 570 | // We've seen something of the form |
| 571 | // |
| 572 | // identifier( |
| 573 | // |
| 574 | // and we did not find any entity by the name |
| 575 | // "identifier". However, this identifier is still subject to |
| 576 | // argument-dependent lookup, so keep track of the name. |
| 577 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 578 | Context.OverloadTy, |
| 579 | Loc)); |
| 580 | } |
| 581 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 582 | NamedDecl *D = 0; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 583 | if (Lookup.isAmbiguous()) { |
| 584 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 585 | SS && SS->isSet() ? SS->getRange() |
| 586 | : SourceRange()); |
| 587 | return ExprError(); |
| 588 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 589 | D = Lookup.getAsDecl(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 591 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 592 | // well. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 593 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 594 | if (II && getCurMethodDecl()) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 595 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 596 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 597 | // found a decl, but that decl is outside the current method (i.e. a global |
| 598 | // variable). In these two cases, we do a lookup for an ivar with this |
| 599 | // name, if the lookup suceeds, we replace it our current decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 600 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 601 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 602 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 603 | // Check if referencing a field with __attribute__((deprecated)). |
| 604 | DiagnoseUseOfDeprecatedDecl(IV, Loc); |
| 605 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 606 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 607 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 608 | IdentifierInfo &II = Context.Idents.get("self"); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 609 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 610 | ObjCIvarRefExpr *MRef = new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 611 | Loc, static_cast<Expr*>(SelfExpr.release()), |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 612 | true, true); |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 613 | Context.setFieldDecl(IFace, IV, MRef); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 614 | return Owned(MRef); |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Steve Naroff | 76de9d7 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 617 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 618 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 619 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 620 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 621 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 622 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 623 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | if (D == 0) { |
| 625 | // Otherwise, this could be an implicitly declared function reference (legal |
| 626 | // in C90, extension in C99). |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 627 | if (HasTrailingLParen && II && |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 628 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 629 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 630 | else { |
| 631 | // If this name wasn't predeclared and if this is not a function call, |
| 632 | // diagnose the problem. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 633 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 634 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 635 | << Name << SS->getRange()); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 636 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 637 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 638 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 639 | << Name.getAsString()); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 640 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 641 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 644 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 645 | // If this is an expression of the form &Class::member, don't build an |
| 646 | // implicit member ref, because we want a pointer to the member in general, |
| 647 | // not any specific instance's member. |
| 648 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 649 | DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 650 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 651 | QualType DType; |
| 652 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 653 | DType = FD->getType().getNonReferenceType(); |
| 654 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 655 | DType = Method->getType(); |
| 656 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 657 | DType = Context.OverloadTy; |
| 658 | } |
| 659 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 660 | if (!DType.isNull()) { |
| 661 | // The pointer is type- and value-dependent if it points into something |
| 662 | // dependent. |
| 663 | bool Dependent = false; |
| 664 | for (; DC; DC = DC->getParent()) { |
| 665 | // FIXME: could stop early at namespace scope. |
| 666 | if (DC->isRecord()) { |
| 667 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 668 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 669 | Dependent = true; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 674 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 679 | // We may have found a field within an anonymous union or struct |
| 680 | // (C++ [class.union]). |
| 681 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 682 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 683 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 685 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 686 | if (!MD->isStatic()) { |
| 687 | // C++ [class.mfct.nonstatic]p2: |
| 688 | // [...] if name lookup (3.4.1) resolves the name in the |
| 689 | // id-expression to a nonstatic nontype member of class X or of |
| 690 | // a base class of X, the id-expression is transformed into a |
| 691 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 692 | // as the postfix-expression to the left of the '.' operator. |
| 693 | DeclContext *Ctx = 0; |
| 694 | QualType MemberType; |
| 695 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 696 | Ctx = FD->getDeclContext(); |
| 697 | MemberType = FD->getType(); |
| 698 | |
| 699 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 700 | MemberType = RefType->getPointeeType(); |
| 701 | else if (!FD->isMutable()) { |
| 702 | unsigned combinedQualifiers |
| 703 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 704 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 705 | } |
| 706 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 707 | if (!Method->isStatic()) { |
| 708 | Ctx = Method->getParent(); |
| 709 | MemberType = Method->getType(); |
| 710 | } |
| 711 | } else if (OverloadedFunctionDecl *Ovl |
| 712 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 713 | for (OverloadedFunctionDecl::function_iterator |
| 714 | Func = Ovl->function_begin(), |
| 715 | FuncEnd = Ovl->function_end(); |
| 716 | Func != FuncEnd; ++Func) { |
| 717 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 718 | if (!DMethod->isStatic()) { |
| 719 | Ctx = Ovl->getDeclContext(); |
| 720 | MemberType = Context.OverloadTy; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 725 | |
| 726 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 727 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 728 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 729 | if ((Context.getCanonicalType(CtxType) |
| 730 | == Context.getCanonicalType(ThisType)) || |
| 731 | IsDerivedFrom(ThisType, CtxType)) { |
| 732 | // Build the implicit member access expression. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 733 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 734 | MD->getThisType(Context)); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 735 | return Owned(new (Context) MemberExpr(This, true, D, |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 736 | SourceLocation(), MemberType)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 742 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 743 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 744 | if (MD->isStatic()) |
| 745 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 746 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 747 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 750 | // Any other ways we could have found the field in a well-formed |
| 751 | // program would have been turned into implicit member expressions |
| 752 | // above. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 753 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 754 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 755 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 756 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 757 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 758 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 759 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 760 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 761 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 762 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 763 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 764 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 765 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 766 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 767 | false, false, SS)); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 768 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 769 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 770 | false, false, SS)); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 771 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 61a0f17 | 2009-02-15 01:38:09 +0000 | [diff] [blame] | 773 | // Check if referencing an identifier with __attribute__((deprecated)). |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 774 | DiagnoseUseOfDeprecatedDecl(VD, Loc); |
Chris Lattner | 61a0f17 | 2009-02-15 01:38:09 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 776 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 777 | // Warn about constructs like: |
| 778 | // if (void *X = foo()) { ... } else { X }. |
| 779 | // In the else block, the pointer is always false. |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 780 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 781 | Scope *CheckS = S; |
| 782 | while (CheckS) { |
| 783 | if (CheckS->isWithinElse() && |
| 784 | CheckS->getControlParent()->isDeclScope(Var)) { |
| 785 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 786 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 787 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 788 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 789 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 790 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 791 | break; |
| 792 | } |
| 793 | |
| 794 | // Move up one more control parent to check again. |
| 795 | CheckS = CheckS->getControlParent(); |
| 796 | if (CheckS) |
| 797 | CheckS = CheckS->getParent(); |
| 798 | } |
| 799 | } |
| 800 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 801 | |
| 802 | // Only create DeclRefExpr's for valid Decl's. |
| 803 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 804 | return ExprError(); |
| 805 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 806 | // If the identifier reference is inside a block, and it refers to a value |
| 807 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 808 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 809 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 810 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 811 | // We do not do this for things like enum constants, global variables, etc, |
| 812 | // as they do not get snapshotted. |
| 813 | // |
| 814 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 815 | // The BlocksAttr indicates the variable is bound by-reference. |
| 816 | if (VD->getAttr<BlocksAttr>()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 817 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 818 | VD->getType().getNonReferenceType(), Loc, true)); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 819 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 820 | // Variable will be bound by-copy, make it const within the closure. |
| 821 | VD->getType().addConst(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 822 | return Owned(new (Context) BlockDeclRefExpr(VD, |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 823 | VD->getType().getNonReferenceType(), Loc, false)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 824 | } |
| 825 | // If this reference is not in a block or if the referenced variable is |
| 826 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 828 | bool TypeDependent = false; |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 829 | bool ValueDependent = false; |
| 830 | if (getLangOptions().CPlusPlus) { |
| 831 | // C++ [temp.dep.expr]p3: |
| 832 | // An id-expression is type-dependent if it contains: |
| 833 | // - an identifier that was declared with a dependent type, |
| 834 | if (VD->getType()->isDependentType()) |
| 835 | TypeDependent = true; |
| 836 | // - FIXME: a template-id that is dependent, |
| 837 | // - a conversion-function-id that specifies a dependent type, |
| 838 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 839 | Name.getCXXNameType()->isDependentType()) |
| 840 | TypeDependent = true; |
| 841 | // - a nested-name-specifier that contains a class-name that |
| 842 | // names a dependent type. |
| 843 | else if (SS && !SS->isEmpty()) { |
| 844 | for (DeclContext *DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 845 | DC; DC = DC->getParent()) { |
| 846 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 847 | if (DC->isRecord()) { |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 848 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 849 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 850 | TypeDependent = true; |
| 851 | break; |
| 852 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 857 | // C++ [temp.dep.constexpr]p2: |
| 858 | // |
| 859 | // An identifier is value-dependent if it is: |
| 860 | // - a name declared with a dependent type, |
| 861 | if (TypeDependent) |
| 862 | ValueDependent = true; |
| 863 | // - the name of a non-type template parameter, |
| 864 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 865 | ValueDependent = true; |
| 866 | // - a constant with integral or enumeration type and is |
| 867 | // initialized with an expression that is value-dependent |
| 868 | // (FIXME!). |
| 869 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 870 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 871 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 872 | TypeDependent, ValueDependent, SS)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 875 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 876 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 877 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 878 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 879 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 880 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 881 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 882 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 883 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 885 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 886 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 887 | // string. |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 888 | unsigned Length; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 889 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 890 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 891 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 892 | Length = MD->getSynthesizedMethodSize(); |
| 893 | else { |
| 894 | Diag(Loc, diag::ext_predef_outside_function); |
| 895 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 896 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 897 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 898 | |
| 899 | |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 900 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 901 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 902 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 903 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 906 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | llvm::SmallString<16> CharBuffer; |
| 908 | CharBuffer.resize(Tok.getLength()); |
| 909 | const char *ThisTokBegin = &CharBuffer[0]; |
| 910 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 911 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 913 | Tok.getLocation(), PP); |
| 914 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 915 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 916 | |
| 917 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 918 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 919 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 920 | Literal.isWide(), |
| 921 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 924 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 925 | // 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] | 926 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 927 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 928 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 929 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 930 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 931 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 932 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 933 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 935 | // Add padding so that NumericLiteralParser can overread by one character. |
| 936 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 937 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 940 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 943 | Tok.getLocation(), PP); |
| 944 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 945 | return ExprError(); |
| 946 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 947 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 948 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 949 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 950 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 951 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 952 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 953 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 954 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 955 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 956 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 957 | |
| 958 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 959 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 960 | // isExact will be set by GetFloatValue(). |
| 961 | bool isExact = false; |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 962 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 963 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 964 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 965 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 966 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 967 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 968 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 970 | // long long is a C99 feature. |
| 971 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 972 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 973 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 975 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 976 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 977 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | if (Literal.GetIntegerValue(ResultVal)) { |
| 979 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 980 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 981 | Ty = Context.UnsignedLongLongTy; |
| 982 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 983 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | } else { |
| 985 | // If this value fits into a ULL, try to figure out what else it fits into |
| 986 | // according to the rules of C99 6.4.4.1p5. |
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 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 989 | // be an unsigned int. |
| 990 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 991 | |
| 992 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 993 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 994 | if (!Literal.isLong && !Literal.isLongLong) { |
| 995 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 996 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 998 | // Does it fit in a unsigned int? |
| 999 | if (ResultVal.isIntN(IntSize)) { |
| 1000 | // Does it fit in a signed int? |
| 1001 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1002 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1004 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1005 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1006 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1008 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1010 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1011 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1012 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1013 | // Does it fit in a unsigned long? |
| 1014 | if (ResultVal.isIntN(LongSize)) { |
| 1015 | // Does it fit in a signed long? |
| 1016 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1017 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1019 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1020 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1021 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1024 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1025 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1026 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1027 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | // Does it fit in a unsigned long long? |
| 1029 | if (ResultVal.isIntN(LongLongSize)) { |
| 1030 | // Does it fit in a signed long long? |
| 1031 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1032 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1034 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1035 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1038 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1039 | // If we still couldn't decide a type, we probably have something that |
| 1040 | // 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] | 1041 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1043 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1044 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1047 | if (ResultVal.getBitWidth() != Width) |
| 1048 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1049 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1050 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1051 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1053 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1054 | if (Literal.isImaginary) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1055 | Res = new (Context) ImaginaryLiteral(Res, |
| 1056 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1057 | |
| 1058 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1061 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1062 | SourceLocation R, ExprArg Val) { |
| 1063 | Expr *E = (Expr *)Val.release(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1064 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1065 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1069 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1070 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 1071 | SourceLocation OpLoc, |
| 1072 | const SourceRange &ExprRange, |
| 1073 | bool isSizeof) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | // C99 6.5.3.4p1: |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1075 | if (isa<FunctionType>(exprType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | // alignof(function) is allowed. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1077 | if (isSizeof) |
| 1078 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1083 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1084 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1085 | return false; |
| 1086 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1088 | return DiagnoseIncompleteType(OpLoc, exprType, |
| 1089 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1090 | diag::err_alignof_incomplete_type, |
| 1091 | ExprRange); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1094 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1095 | const SourceRange &ExprRange) { |
| 1096 | E = E->IgnoreParens(); |
| 1097 | |
| 1098 | // alignof decl is always ok. |
| 1099 | if (isa<DeclRefExpr>(E)) |
| 1100 | return false; |
| 1101 | |
| 1102 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1103 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1104 | if (FD->isBitField()) { |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1105 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | // Other fields are ok. |
| 1109 | return false; |
| 1110 | } |
| 1111 | } |
| 1112 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1113 | } |
| 1114 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1115 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1116 | /// the same for @c alignof and @c __alignof |
| 1117 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1118 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1119 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1120 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1122 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1124 | QualType ArgTy; |
| 1125 | SourceRange Range; |
| 1126 | if (isType) { |
| 1127 | ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1128 | Range = ArgRange; |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Verify that the operand is valid. |
| 1131 | if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) |
| 1132 | return ExprError(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1133 | } else { |
| 1134 | // Get the end location. |
| 1135 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1136 | Range = ArgEx->getSourceRange(); |
| 1137 | ArgTy = ArgEx->getType(); |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Verify that the operand is valid. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1140 | bool isInvalid; |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1141 | if (!isSizeof) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1142 | isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range); |
Chris Lattner | da02747 | 2009-01-24 21:29:22 +0000 | [diff] [blame] | 1143 | } else if (ArgEx->isBitField()) { // C99 6.5.3.4p1. |
| 1144 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1145 | isInvalid = true; |
| 1146 | } else { |
| 1147 | isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true); |
| 1148 | } |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1149 | |
| 1150 | if (isInvalid) { |
Chris Lattner | 694b1e4 | 2009-01-24 19:49:13 +0000 | [diff] [blame] | 1151 | DeleteExpr(ArgEx); |
| 1152 | return ExprError(); |
| 1153 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1156 | // 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] | 1157 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeof, isType, TyOrEx, |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1158 | Context.getSizeType(), OpLoc, |
| 1159 | Range.getEnd())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 1162 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) { |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1163 | DefaultFunctionArrayConversion(V); |
| 1164 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1165 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1166 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1167 | return CT->getElementType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Otherwise they pass through real integer and floating point types here. |
| 1170 | if (V->getType()->isArithmeticType()) |
| 1171 | return V->getType(); |
| 1172 | |
| 1173 | // Reject anything else. |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1174 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1175 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1180 | Action::OwningExprResult |
| 1181 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1182 | tok::TokenKind Kind, ExprArg Input) { |
| 1183 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1185 | UnaryOperator::Opcode Opc; |
| 1186 | switch (Kind) { |
| 1187 | default: assert(0 && "Unknown unary op!"); |
| 1188 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1189 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1190 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1192 | if (getLangOptions().CPlusPlus && |
| 1193 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1194 | // Which overloaded operator? |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1195 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1196 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1197 | |
| 1198 | // C++ [over.inc]p1: |
| 1199 | // |
| 1200 | // [...] If the function is a member function with one |
| 1201 | // parameter (which shall be of type int) or a non-member |
| 1202 | // function with two parameters (the second of which shall be |
| 1203 | // of type int), it defines the postfix increment operator ++ |
| 1204 | // for objects of that type. When the postfix increment is |
| 1205 | // called as a result of using the ++ operator, the int |
| 1206 | // argument will have value zero. |
| 1207 | Expr *Args[2] = { |
| 1208 | Arg, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1209 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1210 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1211 | }; |
| 1212 | |
| 1213 | // Build the candidate set for overloading |
| 1214 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1215 | if (AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet)) |
| 1216 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Perform overload resolution. |
| 1219 | OverloadCandidateSet::iterator Best; |
| 1220 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1221 | case OR_Success: { |
| 1222 | // We found a built-in operator or an overloaded operator. |
| 1223 | FunctionDecl *FnDecl = Best->Function; |
| 1224 | |
| 1225 | if (FnDecl) { |
| 1226 | // We matched an overloaded operator. Build a call to that |
| 1227 | // operator. |
| 1228 | |
| 1229 | // Convert the arguments. |
| 1230 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1231 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1232 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1233 | } else { |
| 1234 | // Convert the arguments. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1235 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1236 | FnDecl->getParamDecl(0)->getType(), |
| 1237 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1238 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1242 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1243 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1244 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1246 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1247 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1248 | SourceLocation()); |
| 1249 | UsualUnaryConversions(FnExpr); |
| 1250 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1251 | Input.release(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1252 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
| 1253 | ResultTy, OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1254 | } else { |
| 1255 | // We matched a built-in operator. Convert the arguments, then |
| 1256 | // break out so that we will build the appropriate built-in |
| 1257 | // operator node. |
| 1258 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1259 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1260 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1261 | |
| 1262 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1263 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | case OR_No_Viable_Function: |
| 1267 | // No viable function; fall through to handling this as a |
| 1268 | // built-in operator, which will produce an error message for us. |
| 1269 | break; |
| 1270 | |
| 1271 | case OR_Ambiguous: |
| 1272 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1273 | << UnaryOperator::getOpcodeStr(Opc) |
| 1274 | << Arg->getSourceRange(); |
| 1275 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1276 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // Either we found no viable overloaded operator or we matched a |
| 1280 | // built-in operator. In either case, fall through to trying to |
| 1281 | // build a built-in operation. |
| 1282 | } |
| 1283 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1284 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1285 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | if (result.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1287 | return ExprError(); |
| 1288 | Input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1289 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1292 | Action::OwningExprResult |
| 1293 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1294 | ExprArg Idx, SourceLocation RLoc) { |
| 1295 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1296 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1297 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1298 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1299 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1300 | LHSExp->getType()->isEnumeralType() || |
| 1301 | RHSExp->getType()->isRecordType() || |
| 1302 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1303 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1304 | // to the candidate set. |
| 1305 | OverloadCandidateSet CandidateSet; |
| 1306 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 1307 | if (AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1308 | SourceRange(LLoc, RLoc))) |
| 1309 | return ExprError(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1311 | // Perform overload resolution. |
| 1312 | OverloadCandidateSet::iterator Best; |
| 1313 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1314 | case OR_Success: { |
| 1315 | // We found a built-in operator or an overloaded operator. |
| 1316 | FunctionDecl *FnDecl = Best->Function; |
| 1317 | |
| 1318 | if (FnDecl) { |
| 1319 | // We matched an overloaded operator. Build a call to that |
| 1320 | // operator. |
| 1321 | |
| 1322 | // Convert the arguments. |
| 1323 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1324 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1325 | PerformCopyInitialization(RHSExp, |
| 1326 | FnDecl->getParamDecl(0)->getType(), |
| 1327 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1328 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1329 | } else { |
| 1330 | // Convert the arguments. |
| 1331 | if (PerformCopyInitialization(LHSExp, |
| 1332 | FnDecl->getParamDecl(0)->getType(), |
| 1333 | "passing") || |
| 1334 | PerformCopyInitialization(RHSExp, |
| 1335 | FnDecl->getParamDecl(1)->getType(), |
| 1336 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1337 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1341 | QualType ResultTy |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1342 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1343 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1345 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1346 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1347 | SourceLocation()); |
| 1348 | UsualUnaryConversions(FnExpr); |
| 1349 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1350 | Base.release(); |
| 1351 | Idx.release(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1352 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1353 | ResultTy, LLoc)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1354 | } else { |
| 1355 | // We matched a built-in operator. Convert the arguments, then |
| 1356 | // break out so that we will build the appropriate built-in |
| 1357 | // operator node. |
| 1358 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1359 | "passing") || |
| 1360 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1361 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1362 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1363 | |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | case OR_No_Viable_Function: |
| 1369 | // No viable function; fall through to handling this as a |
| 1370 | // built-in operator, which will produce an error message for us. |
| 1371 | break; |
| 1372 | |
| 1373 | case OR_Ambiguous: |
| 1374 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1375 | << "[]" |
| 1376 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1377 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1378 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | // Either we found no viable overloaded operator or we matched a |
| 1382 | // built-in operator. In either case, fall through to trying to |
| 1383 | // build a built-in operation. |
| 1384 | } |
| 1385 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1386 | // Perform default conversions. |
| 1387 | DefaultFunctionArrayConversion(LHSExp); |
| 1388 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1390 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1392 | // 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] | 1393 | // 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] | 1394 | // in the subscript position. As a result, we need to derive the array base |
| 1395 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1396 | Expr *BaseExpr, *IndexExpr; |
| 1397 | QualType ResultType; |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1398 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1399 | BaseExpr = LHSExp; |
| 1400 | IndexExpr = RHSExp; |
| 1401 | // FIXME: need to deal with const... |
| 1402 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1403 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1404 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1405 | BaseExpr = RHSExp; |
| 1406 | IndexExpr = LHSExp; |
| 1407 | // FIXME: need to deal with const... |
| 1408 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1409 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1410 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1411 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1413 | // FIXME: need to deal with const... |
| 1414 | ResultType = VTy->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1416 | return ExprError(Diag(LHSExp->getLocStart(), |
| 1417 | diag::err_typecheck_subscript_value) << RHSExp->getSourceRange()); |
| 1418 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1419 | // C99 6.5.2.1p1 |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1420 | if (!IndexExpr->getType()->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1421 | return ExprError(Diag(IndexExpr->getLocStart(), |
| 1422 | diag::err_typecheck_subscript) << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1424 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 1425 | // 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] | 1426 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 1427 | // objects in C99. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1428 | if (!ResultType->isObjectType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1429 | return ExprError(Diag(BaseExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1430 | diag::err_typecheck_subscript_not_object) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1431 | << BaseExpr->getType() << BaseExpr->getSourceRange()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1432 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1433 | Base.release(); |
| 1434 | Idx.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1435 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1436 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1439 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1440 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1441 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1442 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1443 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1444 | // The vector accessor can't exceed the number of elements. |
| 1445 | const char *compStr = CompName.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1446 | |
| 1447 | // This flag determines whether or not the component is one of the four |
| 1448 | // special names that indicate a subset of exactly half the elements are |
| 1449 | // to be selected. |
| 1450 | bool HalvingSwizzle = false; |
| 1451 | |
| 1452 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1453 | // indicating that it is a string of hex values to be used as vector indices. |
| 1454 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Check that we've found one of the special components, or that the component |
| 1457 | // names must come from the same set. |
| 1458 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1459 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1460 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1461 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1462 | do |
| 1463 | compStr++; |
| 1464 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1465 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1466 | do |
| 1467 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1468 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1469 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1470 | |
| 1471 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1472 | // We didn't get to the end of the string. This means the component names |
| 1473 | // 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] | 1474 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1475 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1476 | return QualType(); |
| 1477 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1478 | |
| 1479 | // Ensure no component accessor exceeds the width of the vector type it |
| 1480 | // operates on. |
| 1481 | if (!HalvingSwizzle) { |
| 1482 | compStr = CompName.getName(); |
| 1483 | |
| 1484 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1485 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1486 | |
| 1487 | while (*compStr) { |
| 1488 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1489 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1490 | << baseType << SourceRange(CompLoc); |
| 1491 | return QualType(); |
| 1492 | } |
| 1493 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1494 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1495 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1496 | // If this is a halving swizzle, verify that the base type has an even |
| 1497 | // number of elements. |
| 1498 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1499 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1500 | << baseType << SourceRange(CompLoc); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1501 | return QualType(); |
| 1502 | } |
| 1503 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1504 | // The component accessor looks fine - now we need to compute the actual type. |
| 1505 | // The vector type is implied by the component accessor. For example, |
| 1506 | // 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] | 1507 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1508 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1509 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1510 | : CompName.getLength(); |
| 1511 | if (HexSwizzle) |
| 1512 | CompSize--; |
| 1513 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1514 | if (CompSize == 1) |
| 1515 | return vecType->getElementType(); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1516 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1517 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1518 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1519 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1520 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1521 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1522 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1523 | } |
| 1524 | 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] | 1525 | } |
| 1526 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1527 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 1528 | /// constructSetterName - Return the setter name for the given |
| 1529 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 1530 | /// has been capitalized. |
| 1531 | // FIXME: Merge with same routine in Parser. But where should this |
| 1532 | // live? |
| 1533 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 1534 | const IdentifierInfo *Name) { |
| 1535 | llvm::SmallString<100> SelectorName; |
| 1536 | SelectorName = "set"; |
| 1537 | SelectorName.append(Name->getName(), Name->getName()+Name->getLength()); |
| 1538 | SelectorName[3] = toupper(SelectorName[3]); |
| 1539 | return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]); |
| 1540 | } |
| 1541 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1542 | Action::OwningExprResult |
| 1543 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1544 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 1545 | IdentifierInfo &Member) { |
| 1546 | Expr *BaseExpr = static_cast<Expr *>(Base.release()); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1547 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Perform default conversions. |
| 1550 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1551 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1552 | QualType BaseType = BaseExpr->getType(); |
| 1553 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1555 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1556 | // must have pointer type, and the accessed type is the pointee. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1557 | if (OpKind == tok::arrow) { |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1558 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1559 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 1560 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1561 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 1562 | MemberLoc, Member)); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1563 | else |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1564 | return ExprError(Diag(MemberLoc, |
| 1565 | diag::err_typecheck_member_reference_arrow) |
| 1566 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1567 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1569 | // Handle field access to simple records. This also handles access to fields |
| 1570 | // of the ObjC 'id' struct. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1571 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1572 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1573 | if (DiagnoseIncompleteType(OpLoc, BaseType, |
| 1574 | diag::err_typecheck_incomplete_tag, |
| 1575 | BaseExpr->getSourceRange())) |
| 1576 | return ExprError(); |
| 1577 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1578 | // The record definition is complete, now make sure the member is valid. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1579 | // FIXME: Qualified name lookup for C++ is a bit more complicated |
| 1580 | // than this. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1581 | LookupResult Result |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1582 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1583 | LookupMemberName, false); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1585 | NamedDecl *MemberDecl = 0; |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1586 | if (!Result) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1587 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 1588 | << &Member << BaseExpr->getSourceRange()); |
| 1589 | else if (Result.isAmbiguous()) { |
| 1590 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 1591 | MemberLoc, BaseExpr->getSourceRange()); |
| 1592 | return ExprError(); |
| 1593 | } else |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 1594 | MemberDecl = Result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1596 | // If the decl being referenced had an error, return an error for this |
| 1597 | // sub-expr without emitting another error, in order to avoid cascading |
| 1598 | // error cases. |
| 1599 | if (MemberDecl->isInvalidDecl()) |
| 1600 | return ExprError(); |
Chris Lattner | cfdff38 | 2009-02-16 17:07:21 +0000 | [diff] [blame] | 1601 | |
| 1602 | // Check if referencing a field with __attribute__((deprecated)). |
| 1603 | DiagnoseUseOfDeprecatedDecl(MemberDecl, MemberLoc); |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1605 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1606 | // We may have found a field within an anonymous union or struct |
| 1607 | // (C++ [class.union]). |
| 1608 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1609 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1610 | BaseExpr, OpLoc); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1612 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1613 | // FIXME: Handle address space modifiers |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1614 | QualType MemberType = FD->getType(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1615 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 1616 | MemberType = Ref->getPointeeType(); |
| 1617 | else { |
| 1618 | unsigned combinedQualifiers = |
| 1619 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1620 | if (FD->isMutable()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1621 | combinedQualifiers &= ~QualType::Const; |
| 1622 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1623 | } |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1624 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1625 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 1626 | MemberLoc, MemberType)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1627 | } else if (CXXClassVarDecl *Var = dyn_cast<CXXClassVarDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1628 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1629 | Var, MemberLoc, |
| 1630 | Var->getType().getNonReferenceType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1631 | else if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1632 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 1633 | MemberFn, MemberLoc, MemberFn->getType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1634 | else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1635 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1636 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1637 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1638 | else if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1639 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Enum, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1640 | MemberLoc, Enum->getType())); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1641 | else if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1642 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 1643 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 1644 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1645 | // We found a declaration kind that we didn't expect. This is a |
| 1646 | // generic error message that tells the user that she can't refer |
| 1647 | // to this member with '.' or '->'. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1648 | return ExprError(Diag(MemberLoc, |
| 1649 | diag::err_typecheck_member_reference_unknown) |
| 1650 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1651 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1653 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 1654 | // (*Obj).ivar. |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1655 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1656 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member)) { |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 1657 | // If the decl being referenced had an error, return an error for this |
| 1658 | // sub-expr without emitting another error, in order to avoid cascading |
| 1659 | // error cases. |
| 1660 | if (IV->isInvalidDecl()) |
| 1661 | return ExprError(); |
| 1662 | |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 1663 | // Check if referencing a field with __attribute__((deprecated)). |
| 1664 | DiagnoseUseOfDeprecatedDecl(IV, MemberLoc); |
| 1665 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1666 | ObjCIvarRefExpr *MRef= new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 1667 | MemberLoc, BaseExpr, |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 1668 | OpKind == tok::arrow); |
| 1669 | Context.setFieldDecl(IFTy->getDecl(), IV, MRef); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1670 | return Owned(MRef); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 1671 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1672 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1673 | << IFTy->getDecl()->getDeclName() << &Member |
| 1674 | << BaseExpr->getSourceRange()); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1675 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1676 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 1677 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 1678 | // pointer to a (potentially qualified) interface type. |
| 1679 | const PointerType *PTy; |
| 1680 | const ObjCInterfaceType *IFTy; |
| 1681 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 1682 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 1683 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1684 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1685 | // Search for a declared property first. |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1686 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) { |
| 1687 | // Check if referencing a property with __attribute__((deprecated)). |
| 1688 | DiagnoseUseOfDeprecatedDecl(PD, MemberLoc); |
| 1689 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1690 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1691 | MemberLoc, BaseExpr)); |
| 1692 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1693 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1694 | // Check protocols on qualified interfaces. |
Chris Lattner | 9baefc2 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 1695 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 1696 | E = IFTy->qual_end(); I != E; ++I) |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1697 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
| 1698 | // Check if referencing a property with __attribute__((deprecated)). |
| 1699 | DiagnoseUseOfDeprecatedDecl(PD, MemberLoc); |
| 1700 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1701 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1702 | MemberLoc, BaseExpr)); |
| 1703 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1704 | |
| 1705 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1706 | // selector is implemented. |
| 1707 | |
| 1708 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1709 | // shared with the code in ActOnInstanceMessage. |
| 1710 | |
| 1711 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1712 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1713 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1714 | // If this reference is in an @implementation, check for 'private' methods. |
| 1715 | if (!Getter) |
| 1716 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1717 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1718 | if (ObjCImplementationDecl *ImpDecl = |
| 1719 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1720 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 1721 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 1722 | // Look through local category implementations associated with the class. |
| 1723 | if (!Getter) { |
| 1724 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 1725 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1726 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 1727 | } |
| 1728 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1729 | if (Getter) { |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1730 | // Check if referencing a property with __attribute__((deprecated)). |
| 1731 | DiagnoseUseOfDeprecatedDecl(Getter, MemberLoc); |
| 1732 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1733 | // 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] | 1734 | // will look for the matching setter, in case it is needed. |
| 1735 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 1736 | &Member); |
| 1737 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 1738 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 1739 | if (!Setter) { |
| 1740 | // If this reference is in an @implementation, also check for 'private' |
| 1741 | // methods. |
| 1742 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1743 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 1744 | if (ObjCImplementationDecl *ImpDecl = |
| 1745 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 1746 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 1747 | } |
| 1748 | // Look through local category implementations associated with the class. |
| 1749 | if (!Setter) { |
| 1750 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 1751 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 1752 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(SetterSel); |
| 1753 | } |
| 1754 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1756 | if (Setter) |
| 1757 | // Check if referencing a property with __attribute__((deprecated)). |
| 1758 | DiagnoseUseOfDeprecatedDecl(Setter, MemberLoc); |
| 1759 | |
| 1760 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1761 | // FIXME: we must check that the setter has property type. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1762 | return Owned(new (Context) ObjCKVCRefExpr(Getter, Getter->getResultType(), |
| 1763 | Setter, MemberLoc, BaseExpr)); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 1764 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1765 | |
| 1766 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1767 | << &Member << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1768 | } |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 1769 | // Handle properties on qualified "id" protocols. |
| 1770 | const ObjCQualifiedIdType *QIdTy; |
| 1771 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 1772 | // Check protocols on qualified interfaces. |
| 1773 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1774 | E = QIdTy->qual_end(); I != E; ++I) { |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1775 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
| 1776 | // Check if referencing a property with __attribute__((deprecated)). |
| 1777 | DiagnoseUseOfDeprecatedDecl(PD, MemberLoc); |
| 1778 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1779 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1780 | MemberLoc, BaseExpr)); |
| 1781 | } |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1782 | // Also must look for a getter name which uses property syntax. |
| 1783 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 1784 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame^] | 1785 | // Check if referencing a property with __attribute__((deprecated)). |
| 1786 | DiagnoseUseOfDeprecatedDecl(OMD, MemberLoc); |
| 1787 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1788 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
| 1789 | OMD->getResultType(), OMD, OpLoc, MemberLoc, NULL, 0)); |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1792 | |
| 1793 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1794 | << &Member << BaseType); |
| 1795 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1796 | // Handle 'field access' to vectors, such as 'V.xx'. |
| 1797 | if (BaseType->isExtVectorType() && OpKind == tok::period) { |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1798 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1799 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1800 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1801 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
| 1802 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1803 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1804 | |
| 1805 | return ExprError(Diag(MemberLoc, |
| 1806 | diag::err_typecheck_member_reference_struct_union) |
| 1807 | << BaseType << BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1810 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 1811 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 1812 | /// function prototype Proto. Call is the call expression itself, and |
| 1813 | /// Fn is the function expression. For a C++ member function, this |
| 1814 | /// routine does not attempt to convert the object argument. Returns |
| 1815 | /// true if the call is ill-formed. |
| 1816 | bool |
| 1817 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
| 1818 | FunctionDecl *FDecl, |
| 1819 | const FunctionTypeProto *Proto, |
| 1820 | Expr **Args, unsigned NumArgs, |
| 1821 | SourceLocation RParenLoc) { |
| 1822 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 1823 | // assignment, to the types of the corresponding parameter, ... |
| 1824 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1825 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1826 | bool Invalid = false; |
| 1827 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1828 | // If too few arguments are available (and we don't have default |
| 1829 | // arguments for the remaining parameters), don't make the call. |
| 1830 | if (NumArgs < NumArgsInProto) { |
| 1831 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 1832 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 1833 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 1834 | // Use default arguments for missing arguments |
| 1835 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1836 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | // If too many are passed and not variadic, error on the extras and drop |
| 1840 | // them. |
| 1841 | if (NumArgs > NumArgsInProto) { |
| 1842 | if (!Proto->isVariadic()) { |
| 1843 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 1844 | diag::err_typecheck_call_too_many_args) |
| 1845 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 1846 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 1847 | Args[NumArgs-1]->getLocEnd()); |
| 1848 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1849 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1850 | Invalid = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1851 | } |
| 1852 | NumArgsToCheck = NumArgsInProto; |
| 1853 | } |
| 1854 | |
| 1855 | // Continue to check argument types (even if we have too few/many args). |
| 1856 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 1857 | QualType ProtoArgType = Proto->getArgType(i); |
| 1858 | |
| 1859 | Expr *Arg; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1860 | if (i < NumArgs) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1861 | Arg = Args[i]; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1862 | |
| 1863 | // Pass the argument. |
| 1864 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 1865 | return true; |
| 1866 | } else |
| 1867 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1868 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1869 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1871 | Call->setArg(i, Arg); |
| 1872 | } |
| 1873 | |
| 1874 | // If this is a variadic call, handle args passed through "...". |
| 1875 | if (Proto->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1876 | VariadicCallType CallType = VariadicFunction; |
| 1877 | if (Fn->getType()->isBlockPointerType()) |
| 1878 | CallType = VariadicBlock; // Block |
| 1879 | else if (isa<MemberExpr>(Fn)) |
| 1880 | CallType = VariadicMethod; |
| 1881 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1882 | // Promote the arguments (C99 6.5.2.2p7). |
| 1883 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 1884 | Expr *Arg = Args[i]; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 1885 | DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1886 | Call->setArg(i, Arg); |
| 1887 | } |
| 1888 | } |
| 1889 | |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 1890 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1893 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1894 | /// This provides the location of the left/right parens and a list of comma |
| 1895 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1896 | Action::OwningExprResult |
| 1897 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 1898 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1899 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1900 | unsigned NumArgs = args.size(); |
| 1901 | Expr *Fn = static_cast<Expr *>(fn.release()); |
| 1902 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 1903 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1904 | FunctionDecl *FDecl = NULL; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1905 | DeclarationName UnqualifiedName; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1906 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1907 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1908 | // Determine whether this is a dependent call inside a C++ template, |
| 1909 | // in which case we won't do any semantic analysis now. |
| 1910 | // FIXME: Will need to cache the results of name lookup (including ADL) in Fn. |
| 1911 | bool Dependent = false; |
| 1912 | if (Fn->isTypeDependent()) |
| 1913 | Dependent = true; |
| 1914 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 1915 | Dependent = true; |
| 1916 | |
| 1917 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1918 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1919 | Context.DependentTy, RParenLoc)); |
| 1920 | |
| 1921 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 1922 | if (Fn->getType()->isRecordType()) |
| 1923 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 1924 | CommaLocs, RParenLoc)); |
| 1925 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1926 | // Determine whether this is a call to a member function. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1927 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 1928 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 1929 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1930 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 1931 | CommaLocs, RParenLoc)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1934 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1935 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1936 | Expr *FnExpr = Fn; |
| 1937 | bool ADL = true; |
| 1938 | while (true) { |
| 1939 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 1940 | FnExpr = IcExpr->getSubExpr(); |
| 1941 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1942 | // Parentheses around a function disable ADL |
| 1943 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1944 | ADL = false; |
| 1945 | FnExpr = PExpr->getSubExpr(); |
| 1946 | } else if (isa<UnaryOperator>(FnExpr) && |
| 1947 | cast<UnaryOperator>(FnExpr)->getOpcode() |
| 1948 | == UnaryOperator::AddrOf) { |
| 1949 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 1950 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 1951 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 1952 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 1953 | break; |
| 1954 | } else if (UnresolvedFunctionNameExpr *DepName |
| 1955 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 1956 | UnqualifiedName = DepName->getName(); |
| 1957 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1958 | } else { |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 1959 | // Any kind of name that does not refer to a declaration (or |
| 1960 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 1961 | ADL = false; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1962 | break; |
| 1963 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1964 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1966 | OverloadedFunctionDecl *Ovl = 0; |
| 1967 | if (DRExpr) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1968 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1969 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
| 1970 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1972 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1973 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1974 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1975 | ADL = false; |
| 1976 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1977 | // We don't perform ADL in C. |
| 1978 | if (!getLangOptions().CPlusPlus) |
| 1979 | ADL = false; |
| 1980 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1981 | if (Ovl || ADL) { |
| 1982 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 1983 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1984 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 1985 | if (!FDecl) |
| 1986 | return ExprError(); |
| 1987 | |
| 1988 | // Update Fn to refer to the actual function selected. |
| 1989 | Expr *NewFn = 0; |
| 1990 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 1991 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 1992 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 1993 | QDRExpr->getLocation(), |
| 1994 | false, false, |
| 1995 | QDRExpr->getSourceRange().getBegin()); |
| 1996 | else |
| 1997 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
| 1998 | Fn->getSourceRange().getBegin()); |
| 1999 | Fn->Destroy(Context); |
| 2000 | Fn = NewFn; |
| 2001 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2002 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2003 | |
| 2004 | // Promote the function operand. |
| 2005 | UsualUnaryConversions(Fn); |
| 2006 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2007 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2008 | // of arguments and function on error. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2009 | // FIXME: Except that llvm::OwningPtr uses delete, when it really must be |
| 2010 | // Destroy(), or nothing gets cleaned up. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2011 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2012 | Args, NumArgs, |
| 2013 | Context.BoolTy, |
| 2014 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2015 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2016 | const FunctionType *FuncT; |
| 2017 | if (!Fn->getType()->isBlockPointerType()) { |
| 2018 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2019 | // have type pointer to function". |
| 2020 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2021 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2022 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2023 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2024 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2025 | } else { // This is a block call. |
| 2026 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2027 | getAsFunctionType(); |
| 2028 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2029 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2030 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2031 | << Fn->getType() << Fn->getSourceRange()); |
| 2032 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2033 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2034 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2036 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2037 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
| 2038 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2039 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2040 | } else { |
| 2041 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2042 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2043 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2044 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2045 | Expr *Arg = Args[i]; |
| 2046 | DefaultArgumentPromotion(Arg); |
| 2047 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2048 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2049 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2050 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2051 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2052 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2053 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2054 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2056 | // Do special checking on direct calls to functions. |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2057 | if (FDecl) |
| 2058 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2059 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2060 | return Owned(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2063 | Action::OwningExprResult |
| 2064 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2065 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2066 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2067 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 2068 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2069 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2070 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2071 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2072 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2073 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2074 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2075 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2076 | } else if (DiagnoseIncompleteType(LParenLoc, literalType, |
| 2077 | diag::err_typecheck_decl_incomplete_type, |
| 2078 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2079 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2080 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2081 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2082 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2083 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2085 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2086 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2087 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2088 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2089 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2090 | InitExpr.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2091 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
| 2092 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2095 | Action::OwningExprResult |
| 2096 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| 2097 | InitListDesignations &Designators, |
| 2098 | SourceLocation RBraceLoc) { |
| 2099 | unsigned NumInit = initlist.size(); |
| 2100 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2101 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2102 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 2103 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2104 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2105 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2106 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2107 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2108 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2111 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 58d5ebb | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2112 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2113 | UsualUnaryConversions(castExpr); |
| 2114 | |
| 2115 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2116 | // type needs to be scalar. |
| 2117 | if (castType->isVoidType()) { |
| 2118 | // Cast to void allows any expr type. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2119 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2120 | // We can't check any more until template instantiation time. |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2121 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2122 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2123 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2124 | (castType->isStructureType() || castType->isUnionType())) { |
| 2125 | // GCC struct/union extension: allow cast to self. |
| 2126 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2127 | << castType << castExpr->getSourceRange(); |
| 2128 | } else if (castType->isUnionType()) { |
| 2129 | // GCC cast to union extension |
| 2130 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2131 | RecordDecl::field_iterator Field, FieldEnd; |
| 2132 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| 2133 | Field != FieldEnd; ++Field) { |
| 2134 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2135 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2136 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2137 | << castExpr->getSourceRange(); |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | if (Field == FieldEnd) |
| 2142 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2143 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2144 | } else { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2145 | // Reject any other conversions to non-scalar types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2146 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2147 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2148 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2149 | } else if (!castExpr->getType()->isScalarType() && |
| 2150 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2151 | return Diag(castExpr->getLocStart(), |
| 2152 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2153 | << castExpr->getType() << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2154 | } else if (castExpr->getType()->isVectorType()) { |
| 2155 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2156 | return true; |
| 2157 | } else if (castType->isVectorType()) { |
| 2158 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2159 | return true; |
| 2160 | } |
| 2161 | return false; |
| 2162 | } |
| 2163 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2164 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2165 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 2166 | |
| 2167 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2168 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2169 | return Diag(R.getBegin(), |
| 2170 | Ty->isVectorType() ? |
| 2171 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2172 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2173 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2174 | } else |
| 2175 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2176 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2177 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2178 | |
| 2179 | return false; |
| 2180 | } |
| 2181 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2182 | Action::OwningExprResult |
| 2183 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2184 | SourceLocation RParenLoc, ExprArg Op) { |
| 2185 | assert((Ty != 0) && (Op.get() != 0) && |
| 2186 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2187 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2188 | Expr *castExpr = static_cast<Expr*>(Op.release()); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2189 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2190 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2191 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2192 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2193 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2194 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2197 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 2198 | /// In that case, lex = cond. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2199 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2200 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2201 | UsualUnaryConversions(cond); |
| 2202 | UsualUnaryConversions(lex); |
| 2203 | UsualUnaryConversions(rex); |
| 2204 | QualType condT = cond->getType(); |
| 2205 | QualType lexT = lex->getType(); |
| 2206 | QualType rexT = rex->getType(); |
| 2207 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2208 | // first, check the condition. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2209 | if (!cond->isTypeDependent()) { |
| 2210 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 2211 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) << condT; |
| 2212 | return QualType(); |
| 2213 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2214 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Now check the two expressions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2217 | if ((lex && lex->isTypeDependent()) || (rex && rex->isTypeDependent())) |
| 2218 | return Context.DependentTy; |
| 2219 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2220 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2221 | // to find a common type: C99 6.5.15p3,5. |
| 2222 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2223 | UsualArithmeticConversions(lex, rex); |
| 2224 | return lex->getType(); |
| 2225 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2226 | |
| 2227 | // If both operands are the same structure or union type, the result is that |
| 2228 | // type. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2229 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2230 | if (const RecordType *RHSRT = rexT->getAsRecordType()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2231 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2232 | // "If both the operands have structure or union type, the result has |
| 2233 | // that type." This implies that CV qualifiers are dropped. |
| 2234 | return lexT.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2235 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2236 | |
| 2237 | // 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] | 2238 | // The following || allows only one side to be void (a GCC-ism). |
| 2239 | if (lexT->isVoidType() || rexT->isVoidType()) { |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2240 | if (!lexT->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2241 | Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2242 | << rex->getSourceRange(); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2243 | if (!rexT->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 2244 | Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2245 | << lex->getSourceRange(); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2246 | ImpCastExprToType(lex, Context.VoidTy); |
| 2247 | ImpCastExprToType(rex, Context.VoidTy); |
| 2248 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2249 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2250 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2251 | // the type of the other operand." |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2252 | if ((lexT->isPointerType() || lexT->isBlockPointerType() || |
| 2253 | Context.isObjCObjectPointerType(lexT)) && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 2254 | rex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2255 | ImpCastExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2256 | return lexT; |
| 2257 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2258 | if ((rexT->isPointerType() || rexT->isBlockPointerType() || |
| 2259 | Context.isObjCObjectPointerType(rexT)) && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 2260 | lex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2261 | ImpCastExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2262 | return rexT; |
| 2263 | } |
Chris Lattner | bd57d36 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2264 | // Handle the case where both operands are pointers before we handle null |
| 2265 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2266 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 2267 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 2268 | // get the "pointed to" types |
| 2269 | QualType lhptee = LHSPT->getPointeeType(); |
| 2270 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2271 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2272 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2273 | if (lhptee->isVoidType() && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2274 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2275 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2276 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2277 | QualType destType = Context.getPointerType(destPointee); |
| 2278 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 2279 | ImpCastExprToType(rex, destType); // promote to void* |
| 2280 | return destType; |
| 2281 | } |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2282 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2283 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2284 | QualType destType = Context.getPointerType(destPointee); |
| 2285 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 2286 | ImpCastExprToType(rex, destType); // promote to void* |
| 2287 | return destType; |
| 2288 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2289 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2290 | QualType compositeType = lexT; |
| 2291 | |
| 2292 | // If either type is an Objective-C object type then check |
| 2293 | // compatibility according to Objective-C. |
| 2294 | if (Context.isObjCObjectPointerType(lexT) || |
| 2295 | Context.isObjCObjectPointerType(rexT)) { |
| 2296 | // If both operands are interfaces and either operand can be |
| 2297 | // assigned to the other, use that type as the composite |
| 2298 | // type. This allows |
| 2299 | // xxx ? (A*) a : (B*) b |
| 2300 | // where B is a subclass of A. |
| 2301 | // |
| 2302 | // Additionally, as for assignment, if either type is 'id' |
| 2303 | // allow silent coercion. Finally, if the types are |
| 2304 | // incompatible then make sure to use 'id' as the composite |
| 2305 | // type so the result is acceptable for sending messages to. |
| 2306 | |
Steve Naroff | 1fd0361 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2307 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 2308 | // It could return the composite type. |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2309 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2310 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2311 | if (LHSIface && RHSIface && |
| 2312 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 2313 | compositeType = lexT; |
| 2314 | } else if (LHSIface && RHSIface && |
Douglas Gregor | 7ffd0de | 2008-11-26 06:43:45 +0000 | [diff] [blame] | 2315 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2316 | compositeType = rexT; |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2317 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2318 | Context.isObjCIdStructType(rhptee)) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2319 | compositeType = Context.getObjCIdType(); |
| 2320 | } else { |
Steve Naroff | 1fd0361 | 2009-02-12 19:05:07 +0000 | [diff] [blame] | 2321 | Diag(questionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 2322 | << lexT << rexT |
| 2323 | << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2324 | QualType incompatTy = Context.getObjCIdType(); |
| 2325 | ImpCastExprToType(lex, incompatTy); |
| 2326 | ImpCastExprToType(rex, incompatTy); |
| 2327 | return incompatTy; |
| 2328 | } |
| 2329 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2330 | rhptee.getUnqualifiedType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2331 | Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2332 | << lexT << rexT << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2333 | // In this situation, we assume void* type. No especially good |
| 2334 | // reason, but this is what gcc does, and we do have to pick |
| 2335 | // to get a consistent AST. |
| 2336 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 2337 | ImpCastExprToType(lex, incompatTy); |
| 2338 | ImpCastExprToType(rex, incompatTy); |
| 2339 | return incompatTy; |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2340 | } |
| 2341 | // The pointer types are compatible. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2342 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 2343 | // differently qualified versions of compatible types, the result type is |
| 2344 | // a pointer to an appropriately qualified version of the *composite* |
| 2345 | // type. |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2346 | // FIXME: Need to calculate the composite type. |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 2347 | // FIXME: Need to add qualifiers |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 2348 | ImpCastExprToType(lex, compositeType); |
| 2349 | ImpCastExprToType(rex, compositeType); |
| 2350 | return compositeType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2351 | } |
| 2352 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2353 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 2354 | // evaluates to "struct objc_object *" (and is handled above when comparing |
| 2355 | // id with statically typed objects). |
| 2356 | if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) { |
| 2357 | // GCC allows qualified id and any Objective-C type to devolve to |
| 2358 | // id. Currently localizing to here until clear this should be |
| 2359 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 2360 | if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true) || |
| 2361 | (lexT->isObjCQualifiedIdType() && |
| 2362 | Context.isObjCObjectPointerType(rexT)) || |
| 2363 | (rexT->isObjCQualifiedIdType() && |
| 2364 | Context.isObjCObjectPointerType(lexT))) { |
| 2365 | // FIXME: This is not the correct composite type. This only |
| 2366 | // happens to work because id can more or less be used anywhere, |
| 2367 | // however this may change the type of method sends. |
| 2368 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 2369 | // (confusing) incompatible comparison warnings in some |
| 2370 | // cases. Investigate. |
| 2371 | QualType compositeType = Context.getObjCIdType(); |
| 2372 | ImpCastExprToType(lex, compositeType); |
| 2373 | ImpCastExprToType(rex, compositeType); |
| 2374 | return compositeType; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 2378 | // Selection between block pointer types is ok as long as they are the same. |
| 2379 | if (lexT->isBlockPointerType() && rexT->isBlockPointerType() && |
| 2380 | Context.getCanonicalType(lexT) == Context.getCanonicalType(rexT)) |
| 2381 | return lexT; |
| 2382 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2383 | // Otherwise, the operands are not compatible. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2384 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2385 | << lexT << rexT << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2386 | return QualType(); |
| 2387 | } |
| 2388 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2389 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2390 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2391 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 2392 | SourceLocation ColonLoc, |
| 2393 | ExprArg Cond, ExprArg LHS, |
| 2394 | ExprArg RHS) { |
| 2395 | Expr *CondExpr = (Expr *) Cond.get(); |
| 2396 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2397 | |
| 2398 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 2399 | // was the condition. |
| 2400 | bool isLHSNull = LHSExpr == 0; |
| 2401 | if (isLHSNull) |
| 2402 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2403 | |
| 2404 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 2405 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2406 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2407 | return ExprError(); |
| 2408 | |
| 2409 | Cond.release(); |
| 2410 | LHS.release(); |
| 2411 | RHS.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2412 | return Owned(new (Context) ConditionalOperator(CondExpr, |
| 2413 | isLHSNull ? 0 : LHSExpr, |
| 2414 | RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2417 | |
| 2418 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 2419 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 2420 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 2421 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 2422 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2423 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2424 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 2425 | QualType lhptee, rhptee; |
| 2426 | |
| 2427 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 2428 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 2429 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2430 | |
| 2431 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2432 | lhptee = Context.getCanonicalType(lhptee); |
| 2433 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2434 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2435 | AssignConvertType ConvTy = Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2436 | |
| 2437 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 2438 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 2439 | // qualifiers of the type *pointed to* by the right; |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2440 | // FIXME: Handle ASQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2441 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2442 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2443 | |
| 2444 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 2445 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 2446 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2447 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2448 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2449 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2450 | |
| 2451 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2452 | assert(rhptee->isFunctionType()); |
| 2453 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2457 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2458 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2459 | |
| 2460 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 2461 | assert(lhptee->isFunctionType()); |
| 2462 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2463 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Check for ObjC interfaces |
| 2466 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2467 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2468 | if (LHSIface && RHSIface && |
| 2469 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 2470 | return ConvTy; |
| 2471 | |
| 2472 | // ID acts sort of like void* for ObjC interfaces |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2473 | if (LHSIface && Context.isObjCIdStructType(rhptee)) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2474 | return ConvTy; |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2475 | if (RHSIface && Context.isObjCIdStructType(lhptee)) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2476 | return ConvTy; |
| 2477 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2478 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 2479 | // unqualified versions of compatible types, ... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 2480 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 2481 | rhptee.getUnqualifiedType())) |
| 2482 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2483 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2486 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 2487 | /// block pointer types are compatible or whether a block and normal pointer |
| 2488 | /// are compatible. It is more restrict than comparing two function pointer |
| 2489 | // types. |
| 2490 | Sema::AssignConvertType |
| 2491 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
| 2492 | QualType rhsType) { |
| 2493 | QualType lhptee, rhptee; |
| 2494 | |
| 2495 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 2496 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
| 2497 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 2498 | |
| 2499 | // make sure we operate on the canonical type |
| 2500 | lhptee = Context.getCanonicalType(lhptee); |
| 2501 | rhptee = Context.getCanonicalType(rhptee); |
| 2502 | |
| 2503 | AssignConvertType ConvTy = Compatible; |
| 2504 | |
| 2505 | // For blocks we enforce that qualifiers are identical. |
| 2506 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 2507 | ConvTy = CompatiblePointerDiscardsQualifiers; |
| 2508 | |
| 2509 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
| 2510 | return IncompatibleBlockPointer; |
| 2511 | return ConvTy; |
| 2512 | } |
| 2513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 2515 | /// has code to accommodate several GCC extensions when type checking |
| 2516 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 2517 | /// |
| 2518 | /// int a, *pint; |
| 2519 | /// short *pshort; |
| 2520 | /// struct foo *pfoo; |
| 2521 | /// |
| 2522 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 2523 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 2524 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 2525 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 2526 | /// |
| 2527 | /// As a result, the code for dealing with pointers is more complex than the |
| 2528 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2529 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2530 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2531 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2532 | // Get canonical types. We're not formatting these types, just comparing |
| 2533 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2534 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 2535 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2536 | |
| 2537 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 2538 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 2539 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2540 | // If the left-hand side is a reference type, then we are in a |
| 2541 | // (rare!) case where we've allowed the use of references in C, |
| 2542 | // e.g., as a parameter type in a built-in function. In this case, |
| 2543 | // just make sure that the type referenced is compatible with the |
| 2544 | // right-hand side type. The caller is responsible for adjusting |
| 2545 | // lhsType so that the resulting expression does not have reference |
| 2546 | // type. |
| 2547 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 2548 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 2549 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2550 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2551 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2553 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 2554 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2555 | return Compatible; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2556 | // Relax integer conversions like we do for pointers below. |
| 2557 | if (rhsType->isIntegerType()) |
| 2558 | return IntToPointer; |
| 2559 | if (lhsType->isIntegerType()) |
| 2560 | return PointerToInt; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 2561 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 2562 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2563 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2564 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2565 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2566 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 2567 | if (LV->getElementType() == rhsType) |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2568 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2569 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2570 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 2571 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 2572 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2573 | if (getLangOptions().LaxVectorConversions && |
| 2574 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2575 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2576 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2577 | } |
| 2578 | return Incompatible; |
| 2579 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 2581 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2582 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2583 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2584 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2585 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2586 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2588 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2589 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2590 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2591 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2592 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2593 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2594 | |
| 2595 | // Treat block pointers as objects. |
| 2596 | if (getLangOptions().ObjC1 && |
| 2597 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2598 | return Compatible; |
| 2599 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2600 | return Incompatible; |
| 2601 | } |
| 2602 | |
| 2603 | if (isa<BlockPointerType>(lhsType)) { |
| 2604 | if (rhsType->isIntegerType()) |
| 2605 | return IntToPointer; |
| 2606 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 2607 | // Treat block pointers as objects. |
| 2608 | if (getLangOptions().ObjC1 && |
| 2609 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 2610 | return Compatible; |
| 2611 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2612 | if (rhsType->isBlockPointerType()) |
| 2613 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
| 2614 | |
| 2615 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 2616 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2617 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2618 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2619 | return Incompatible; |
| 2620 | } |
| 2621 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2622 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2623 | // 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] | 2624 | if (lhsType == Context.BoolTy) |
| 2625 | return Compatible; |
| 2626 | |
| 2627 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2628 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2630 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2632 | |
| 2633 | if (isa<BlockPointerType>(lhsType) && |
| 2634 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 2635 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2636 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2637 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 2639 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 2640 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2641 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2642 | } |
| 2643 | return Incompatible; |
| 2644 | } |
| 2645 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2646 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2647 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2648 | if (getLangOptions().CPlusPlus) { |
| 2649 | if (!lhsType->isRecordType()) { |
| 2650 | // C++ 5.17p3: If the left operand is not of class type, the |
| 2651 | // expression is implicitly converted (C++ 4) to the |
| 2652 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2653 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 2654 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2655 | return Incompatible; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2656 | else |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2657 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 2661 | // structures. |
| 2662 | } |
| 2663 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2664 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 2665 | // a null pointer constant. |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2666 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType() || |
| 2667 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 2668 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2669 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2670 | return Compatible; |
| 2671 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2672 | |
| 2673 | // We don't allow conversion of non-null-pointer constants to integers. |
| 2674 | if (lhsType->isBlockPointerType() && rExpr->getType()->isIntegerType()) |
| 2675 | return IntToBlockPointer; |
| 2676 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2677 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2678 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2679 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2680 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2681 | // |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2682 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 2683 | if (!lhsType->isReferenceType()) |
| 2684 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2685 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2686 | Sema::AssignConvertType result = |
| 2687 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2688 | |
| 2689 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 2690 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2691 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 2692 | // so that we can use references in built-in functions even in C. |
| 2693 | // The getNonReferenceType() call makes sure that the resulting expression |
| 2694 | // does not have reference type. |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2695 | if (rExpr->getType() != lhsType) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 2696 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2697 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2700 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2701 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 2702 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 2703 | } |
| 2704 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2705 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2706 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +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(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2709 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2712 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2713 | Expr *&rex) { |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2714 | // For conversion purposes, we ignore any qualifiers. |
| 2715 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2716 | QualType lhsType = |
| 2717 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 2718 | QualType rhsType = |
| 2719 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2720 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2721 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 2722 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2723 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2724 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2725 | // Handle the case of a vector & extvector type of the same size and element |
| 2726 | // 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] | 2727 | if (getLangOptions().LaxVectorConversions) { |
| 2728 | // FIXME: Should we warn here? |
| 2729 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2730 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 2731 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2732 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2733 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2738 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 2739 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2740 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2741 | QualType eltType = V->getElementType(); |
| 2742 | |
| 2743 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
| 2744 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 2745 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2746 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2747 | return lhsType; |
| 2748 | } |
| 2749 | } |
| 2750 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2751 | // 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] | 2752 | // promote the lhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2753 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2754 | QualType eltType = V->getElementType(); |
| 2755 | |
| 2756 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
| 2757 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 2758 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2759 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 2760 | return rhsType; |
| 2761 | } |
| 2762 | } |
| 2763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2764 | // You cannot convert between vector values of different size. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2765 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2766 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2767 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2768 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2771 | inline QualType Sema::CheckMultiplyDivideOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2772 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2773 | { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 2774 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2775 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2776 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2777 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2778 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2779 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2780 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2781 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | inline QualType Sema::CheckRemainderOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2785 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 2787 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 2788 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 2789 | return CheckVectorOperands(Loc, lex, rex); |
| 2790 | return InvalidOperands(Loc, lex, rex); |
| 2791 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2792 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2793 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2794 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2795 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2796 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2797 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2801 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2802 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2803 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2804 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2805 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2806 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2807 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2808 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2809 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2810 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2811 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2812 | // Put any potential pointer into PExp |
| 2813 | Expr* PExp = lex, *IExp = rex; |
| 2814 | if (IExp->getType()->isPointerType()) |
| 2815 | std::swap(PExp, IExp); |
| 2816 | |
| 2817 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 2818 | if (IExp->getType()->isIntegerType()) { |
| 2819 | // Check for arithmetic on pointers to incomplete types |
| 2820 | if (!PTy->getPointeeType()->isObjectType()) { |
| 2821 | if (PTy->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2822 | if (getLangOptions().CPlusPlus) { |
| 2823 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 2824 | << lex->getSourceRange() << rex->getSourceRange(); |
| 2825 | return QualType(); |
| 2826 | } |
| 2827 | |
| 2828 | // GNU extension: arithmetic on pointer to void |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2829 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2830 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2831 | } else if (PTy->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2832 | if (getLangOptions().CPlusPlus) { |
| 2833 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2834 | << lex->getType() << lex->getSourceRange(); |
| 2835 | return QualType(); |
| 2836 | } |
| 2837 | |
| 2838 | // GNU extension: arithmetic on pointer to function |
| 2839 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2840 | << lex->getType() << lex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2841 | } else { |
| 2842 | DiagnoseIncompleteType(Loc, PTy->getPointeeType(), |
| 2843 | diag::err_typecheck_arithmetic_incomplete_type, |
| 2844 | lex->getSourceRange(), SourceRange(), |
| 2845 | lex->getType()); |
| 2846 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | return PExp->getType(); |
| 2850 | } |
| 2851 | } |
| 2852 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2853 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2856 | // C99 6.5.6 |
| 2857 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2858 | SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2859 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2860 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 2861 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2862 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2863 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2864 | // Enforce type constraints: C99 6.5.6p3. |
| 2865 | |
| 2866 | // Handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2867 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2868 | return compType; |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Either ptr - int or ptr - ptr. |
| 2871 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2872 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2874 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2875 | if (!lpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2876 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2877 | if (lpointee->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2878 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2879 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 2880 | } else if (lpointee->isFunctionType()) { |
| 2881 | if (getLangOptions().CPlusPlus) { |
| 2882 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2883 | << lex->getType() << lex->getSourceRange(); |
| 2884 | return QualType(); |
| 2885 | } |
| 2886 | |
| 2887 | // GNU extension: arithmetic on pointer to function |
| 2888 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 2889 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2890 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2891 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2892 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2893 | return QualType(); |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | // The result type of a pointer-int computation is the pointer type. |
| 2898 | if (rex->getType()->isIntegerType()) |
| 2899 | return lex->getType(); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 2900 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2901 | // Handle pointer-pointer subtractions. |
| 2902 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2903 | QualType rpointee = RHSPTy->getPointeeType(); |
| 2904 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2905 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2906 | if (!rpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2907 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 2908 | if (rpointee->isVoidType()) { |
| 2909 | if (!lpointee->isVoidType()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2910 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 2911 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 0804888 | 2009-01-23 19:03:35 +0000 | [diff] [blame] | 2912 | } else if (rpointee->isFunctionType()) { |
| 2913 | if (getLangOptions().CPlusPlus) { |
| 2914 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 2915 | << rex->getType() << rex->getSourceRange(); |
| 2916 | return QualType(); |
| 2917 | } |
| 2918 | |
| 2919 | // GNU extension: arithmetic on pointer to function |
| 2920 | if (!lpointee->isFunctionType()) |
| 2921 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 2922 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2923 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2924 | Diag(Loc, diag::err_typecheck_sub_ptr_object) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2925 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2926 | return QualType(); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | // Pointee types must be compatible. |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 2931 | if (!Context.typesAreCompatible( |
| 2932 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 2933 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2934 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2935 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 2936 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 2937 | return QualType(); |
| 2938 | } |
| 2939 | |
| 2940 | return Context.getPointerDiffType(); |
| 2941 | } |
| 2942 | } |
| 2943 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2944 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2947 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2948 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2949 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2950 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 2951 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2952 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2953 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2954 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 2955 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 2956 | if (!isCompAssign) |
| 2957 | UsualUnaryConversions(lex); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2958 | UsualUnaryConversions(rex); |
| 2959 | |
| 2960 | // "The type of the result is that of the promoted left operand." |
| 2961 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2964 | // C99 6.5.8 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2965 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2966 | bool isRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2967 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2968 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2970 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 2971 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 2972 | UsualArithmeticConversions(lex, rex); |
| 2973 | else { |
| 2974 | UsualUnaryConversions(lex); |
| 2975 | UsualUnaryConversions(rex); |
| 2976 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2977 | QualType lType = lex->getType(); |
| 2978 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2979 | |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2980 | // For non-floating point types, check for self-comparisons of the form |
| 2981 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 2982 | // often indicate logic errors in the program. |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2983 | if (!lType->isFloatingType()) { |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2984 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 2985 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2986 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 2987 | Diag(Loc, diag::warn_selfcomparison); |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2990 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| 2991 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy : Context.IntTy; |
| 2992 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2993 | if (isRelational) { |
| 2994 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 2995 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2996 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2997 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2998 | if (lType->isFloatingType()) { |
| 2999 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3000 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3003 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3004 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3005 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3006 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3007 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3008 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 3009 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3010 | // All of the following pointer related warnings are GCC extensions, except |
| 3011 | // when handling null pointer constants. One day, we can consider making them |
| 3012 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3013 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3014 | QualType LCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3015 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3016 | QualType RCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3017 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3018 | |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3019 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3020 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3021 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3022 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3023 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3024 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3025 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3026 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3027 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3028 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3029 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3030 | // Handle block pointer types. |
| 3031 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 3032 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3033 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
| 3034 | |
| 3035 | if (!LHSIsNull && !RHSIsNull && |
| 3036 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3037 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3038 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3039 | } |
| 3040 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3041 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3042 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3043 | // Allow block pointers to be compared with null pointer constants. |
| 3044 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 3045 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 3046 | if (!LHSIsNull && !RHSIsNull) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3047 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3048 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3049 | } |
| 3050 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3051 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3052 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3053 | |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3054 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3055 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 3056 | const PointerType *LPT = lType->getAsPointerType(); |
| 3057 | const PointerType *RPT = rType->getAsPointerType(); |
| 3058 | bool LPtrToVoid = LPT ? |
| 3059 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
| 3060 | bool RPtrToVoid = RPT ? |
| 3061 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
| 3062 | |
| 3063 | if (!LPtrToVoid && !RPtrToVoid && |
| 3064 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3065 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3066 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3067 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3068 | return ResultTy; |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 3069 | } |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3070 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3071 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 3072 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3073 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 3074 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3075 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3076 | } else { |
| 3077 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3078 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3079 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 3080 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3081 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3082 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3083 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 3084 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3085 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
| 3086 | rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3087 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3088 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3089 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3090 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3091 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3092 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3093 | if (lType->isIntegerType() && |
| 3094 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3095 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3096 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3097 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3098 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3099 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3100 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3101 | // Handle block pointers. |
| 3102 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 3103 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3104 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3105 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3106 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3107 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3108 | } |
| 3109 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 3110 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3111 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3112 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3113 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3114 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 3115 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3116 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3119 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| 3120 | /// operates on extended vector types. Instead of producing an IntTy result, |
| 3121 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 3122 | /// types. |
| 3123 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3124 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3125 | bool isRelational) { |
| 3126 | // Check to make sure we're operating on vectors of the same type and width, |
| 3127 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3128 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3129 | if (vType.isNull()) |
| 3130 | return vType; |
| 3131 | |
| 3132 | QualType lType = lex->getType(); |
| 3133 | QualType rType = rex->getType(); |
| 3134 | |
| 3135 | // For non-floating point types, check for self-comparisons of the form |
| 3136 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3137 | // often indicate logic errors in the program. |
| 3138 | if (!lType->isFloatingType()) { |
| 3139 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 3140 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 3141 | if (DRL->getDecl() == DRR->getDecl()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3142 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // Check for comparisons of floating point operands using != and ==. |
| 3146 | if (!isRelational && lType->isFloatingType()) { |
| 3147 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3148 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | // Return the type for the comparison, which is the same as vector type for |
| 3152 | // integer vectors, or an integer type of identical size and number of |
| 3153 | // elements for floating point vectors. |
| 3154 | if (lType->isIntegerType()) |
| 3155 | return lType; |
| 3156 | |
| 3157 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3158 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3159 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3160 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3161 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 3162 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 3163 | |
| 3164 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 3165 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3166 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 3167 | } |
| 3168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3169 | inline QualType Sema::CheckBitwiseOperands( |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3170 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3171 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 3172 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3173 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3174 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3175 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3176 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3177 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3178 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3179 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3183 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3184 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3185 | UsualUnaryConversions(lex); |
| 3186 | UsualUnaryConversions(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3187 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 3188 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3189 | return Context.IntTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3190 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3193 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 3194 | /// is a read-only property; return true if so. A readonly property expression |
| 3195 | /// depends on various declarations and thus must be treated specially. |
| 3196 | /// |
| 3197 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
| 3198 | { |
| 3199 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 3200 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 3201 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 3202 | QualType BaseType = PropExpr->getBase()->getType(); |
| 3203 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
| 3204 | if (const ObjCInterfaceType *IFTy = |
| 3205 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 3206 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 3207 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 3208 | return true; |
| 3209 | } |
| 3210 | } |
| 3211 | return false; |
| 3212 | } |
| 3213 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3214 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 3215 | /// emit an error and return true. If so, return false. |
| 3216 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 3217 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context); |
| 3218 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 3219 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3220 | if (IsLV == Expr::MLV_Valid) |
| 3221 | return false; |
| 3222 | |
| 3223 | unsigned Diag = 0; |
| 3224 | bool NeedType = false; |
| 3225 | switch (IsLV) { // C99 6.5.16p2 |
| 3226 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 3227 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3228 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3229 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 3230 | NeedType = true; |
| 3231 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3232 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3233 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 3234 | NeedType = true; |
| 3235 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 3236 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3237 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 3238 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3239 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3240 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 3241 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3242 | case Expr::MLV_IncompleteType: |
| 3243 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3244 | return S.DiagnoseIncompleteType(Loc, E->getType(), |
| 3245 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 3246 | E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3247 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3248 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 3249 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 3250 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3251 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 3252 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 3253 | case Expr::MLV_ReadonlyProperty: |
| 3254 | Diag = diag::error_readonly_property_assignment; |
| 3255 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 3256 | case Expr::MLV_NoSetterProperty: |
| 3257 | Diag = diag::error_nosetter_property_assignment; |
| 3258 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3259 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3260 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3261 | if (NeedType) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3262 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3263 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3264 | S.Diag(Loc, Diag) << E->getSourceRange(); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3265 | return true; |
| 3266 | } |
| 3267 | |
| 3268 | |
| 3269 | |
| 3270 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3271 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 3272 | SourceLocation Loc, |
| 3273 | QualType CompoundType) { |
| 3274 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 3275 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3276 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3277 | |
| 3278 | QualType LHSType = LHS->getType(); |
| 3279 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3281 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3282 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3283 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3284 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3285 | // Special case of NSObject attributes on c-style pointer types. |
| 3286 | if (ConvTy == IncompatiblePointer && |
| 3287 | ((Context.isObjCNSObjectType(LHSType) && |
| 3288 | Context.isObjCObjectPointerType(RHSType)) || |
| 3289 | (Context.isObjCNSObjectType(RHSType) && |
| 3290 | Context.isObjCObjectPointerType(LHSType)))) |
| 3291 | ConvTy = Compatible; |
| 3292 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3293 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 3294 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 3295 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3296 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3297 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 3298 | RHSCheck = ICE->getSubExpr(); |
| 3299 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 3300 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 3301 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3302 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3303 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3304 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3305 | Diag(Loc, diag::warn_not_compound_assign) |
| 3306 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 3307 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } else { |
| 3310 | // Compound assignment "x += y" |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3311 | ConvTy = CheckCompoundAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 3312 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3313 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3314 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 3315 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3316 | return QualType(); |
| 3317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3318 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 3319 | // left operand unless the left operand has qualified type, in which case |
| 3320 | // it is the unqualified version of the type of the left operand. |
| 3321 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 3322 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3323 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 3324 | // oprdu. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3325 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3328 | // C99 6.5.17 |
| 3329 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
| 3330 | // FIXME: what is required for LHS? |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 3331 | |
| 3332 | // 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] | 3333 | DefaultFunctionArrayConversion(RHS); |
| 3334 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3337 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 3338 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3339 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 3340 | bool isInc) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3341 | QualType ResType = Op->getType(); |
| 3342 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3343 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3344 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 3345 | // Decrement of bool is not allowed. |
| 3346 | if (!isInc) { |
| 3347 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 3348 | return QualType(); |
| 3349 | } |
| 3350 | // Increment of bool sets it to true, but is deprecated. |
| 3351 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 3352 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3353 | // OK! |
| 3354 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 3355 | // C99 6.5.2.4p2, 6.5.6p2 |
| 3356 | if (PT->getPointeeType()->isObjectType()) { |
| 3357 | // Pointer to object is ok! |
| 3358 | } else if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3359 | if (getLangOptions().CPlusPlus) { |
| 3360 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 3361 | << Op->getSourceRange(); |
| 3362 | return QualType(); |
| 3363 | } |
| 3364 | |
| 3365 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3366 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3367 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3368 | if (getLangOptions().CPlusPlus) { |
| 3369 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 3370 | << Op->getType() << Op->getSourceRange(); |
| 3371 | return QualType(); |
| 3372 | } |
| 3373 | |
| 3374 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3375 | << ResType << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3376 | return QualType(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3377 | } else { |
| 3378 | DiagnoseIncompleteType(OpLoc, PT->getPointeeType(), |
| 3379 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3380 | Op->getSourceRange(), SourceRange(), |
| 3381 | ResType); |
| 3382 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3383 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3384 | } else if (ResType->isComplexType()) { |
| 3385 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 3386 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3387 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3388 | } else { |
| 3389 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3390 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3391 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3392 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 3393 | // At this point, we know we have a real, complex or pointer type. |
| 3394 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3395 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3396 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 3397 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3400 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3401 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3402 | /// where the declaration is needed for type checking. We only need to |
| 3403 | /// handle cases when the expression references a function designator |
| 3404 | /// or is an lvalue. Here are some examples: |
| 3405 | /// - &(x) => x |
| 3406 | /// - &*****f => f for f a function designator. |
| 3407 | /// - &s.xx => s |
| 3408 | /// - &s.zz[1].yy -> s, if zz is an array |
| 3409 | /// - *(x + 1) -> x, if x is an array |
| 3410 | /// - &"123"[2] -> 0 |
| 3411 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3412 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3413 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3414 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 3415 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3416 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3417 | case Stmt::MemberExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3418 | // Fields cannot be declared with a 'register' storage class. |
| 3419 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3420 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3421 | return 0; |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3422 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3423 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3424 | // &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] | 3425 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3426 | NamedDecl *D = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Daniel Dunbar | 48d04ae | 2008-10-21 21:22:32 +0000 | [diff] [blame] | 3427 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); |
Anders Carlsson | f2a4b84 | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 3428 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 3429 | return 0; |
| 3430 | else |
| 3431 | return VD; |
| 3432 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3433 | case Stmt::UnaryOperatorClass: { |
| 3434 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| 3435 | |
| 3436 | switch(UO->getOpcode()) { |
| 3437 | case UnaryOperator::Deref: { |
| 3438 | // *(X + 1) refers to X if X is not a pointer. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3439 | if (NamedDecl *D = getPrimaryDecl(UO->getSubExpr())) { |
| 3440 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3441 | if (!VD || VD->getType()->isPointerType()) |
| 3442 | return 0; |
| 3443 | return VD; |
| 3444 | } |
| 3445 | return 0; |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 3446 | } |
| 3447 | case UnaryOperator::Real: |
| 3448 | case UnaryOperator::Imag: |
| 3449 | case UnaryOperator::Extension: |
| 3450 | return getPrimaryDecl(UO->getSubExpr()); |
| 3451 | default: |
| 3452 | return 0; |
| 3453 | } |
| 3454 | } |
| 3455 | case Stmt::BinaryOperatorClass: { |
| 3456 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 3457 | |
| 3458 | // Handle cases involving pointer arithmetic. The result of an |
| 3459 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 3460 | |
| 3461 | // (x + n) or (n + x) => x |
| 3462 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 3463 | if (BO->getLHS()->getType()->isPointerType()) { |
| 3464 | return getPrimaryDecl(BO->getLHS()); |
| 3465 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 3466 | return getPrimaryDecl(BO->getRHS()); |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | return 0; |
| 3471 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3472 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3473 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3474 | case Stmt::ImplicitCastExprClass: |
| 3475 | // &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] | 3476 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3477 | default: |
| 3478 | return 0; |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 3483 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 3484 | /// object cannot be declared with storage class register or be a bit field. |
| 3485 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 3486 | /// 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] | 3487 | /// In C++, the operand might be an overloaded function name, in which case |
| 3488 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3489 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 3490 | if (op->isTypeDependent()) |
| 3491 | return Context.DependentTy; |
| 3492 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3493 | if (getLangOptions().C99) { |
| 3494 | // Implement C99-only parts of addressof rules. |
| 3495 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 3496 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 3497 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 3498 | // (assuming the deref expression is valid). |
| 3499 | return uOp->getSubExpr()->getType(); |
| 3500 | } |
| 3501 | // Technically, there should be a check for array subscript |
| 3502 | // expressions here, but the result of one is always an lvalue anyway. |
| 3503 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3504 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 3505 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 3506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3507 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 3508 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 3509 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3510 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 3511 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3512 | return QualType(); |
| 3513 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3514 | } 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] | 3515 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemExpr->getMemberDecl())) { |
| 3516 | if (Field->isBitField()) { |
| 3517 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3518 | << "bit-field" << op->getSourceRange(); |
| 3519 | return QualType(); |
| 3520 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3521 | } |
| 3522 | // Check for Apple extension for accessing vector components. |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3523 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 3524 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3525 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 3526 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 3527 | return QualType(); |
| 3528 | } else if (dcl) { // C99 6.5.3.2p1 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3529 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 3530 | // with the register storage-class specifier. |
| 3531 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 3532 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3533 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 3534 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3535 | return QualType(); |
| 3536 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3537 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3538 | return Context.OverloadTy; |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3539 | } else if (isa<FieldDecl>(dcl)) { |
| 3540 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 3541 | // Could be a pointer to member, though, if there is an explicit |
| 3542 | // scope qualifier for the class. |
| 3543 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3544 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3545 | if (Ctx && Ctx->isRecord()) |
| 3546 | return Context.getMemberPointerType(op->getType(), |
| 3547 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3548 | } |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3549 | } else if (isa<FunctionDecl>(dcl)) { |
| 3550 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3551 | // As above. |
| 3552 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 3553 | DeclContext *Ctx = dcl->getDeclContext(); |
| 3554 | if (Ctx && Ctx->isRecord()) |
| 3555 | return Context.getMemberPointerType(op->getType(), |
| 3556 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 3557 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 3558 | } |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 3559 | else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3560 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3561 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3562 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3563 | // If the operand has type "type", the result has type "pointer to type". |
| 3564 | return Context.getPointerType(op->getType()); |
| 3565 | } |
| 3566 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3567 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
| 3568 | UsualUnaryConversions(Op); |
| 3569 | QualType Ty = Op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3570 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3571 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 3572 | // incomplete type or void. It would be possible to warn about dereferencing |
| 3573 | // a void pointer, but it's completely well-defined, and such a warning is |
| 3574 | // unlikely to catch any mistakes. |
| 3575 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 3576 | return PT->getPointeeType(); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3577 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 3578 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3579 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3580 | return QualType(); |
| 3581 | } |
| 3582 | |
| 3583 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 3584 | tok::TokenKind Kind) { |
| 3585 | BinaryOperator::Opcode Opc; |
| 3586 | switch (Kind) { |
| 3587 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3588 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 3589 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3590 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 3591 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 3592 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 3593 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 3594 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 3595 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 3596 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 3597 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 3598 | case tok::less: Opc = BinaryOperator::LT; break; |
| 3599 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 3600 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 3601 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 3602 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 3603 | case tok::amp: Opc = BinaryOperator::And; break; |
| 3604 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 3605 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 3606 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 3607 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 3608 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 3609 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 3610 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 3611 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 3612 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 3613 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 3614 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 3615 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 3616 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 3617 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 3618 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 3619 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 3620 | } |
| 3621 | return Opc; |
| 3622 | } |
| 3623 | |
| 3624 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 3625 | tok::TokenKind Kind) { |
| 3626 | UnaryOperator::Opcode Opc; |
| 3627 | switch (Kind) { |
| 3628 | default: assert(0 && "Unknown unary op!"); |
| 3629 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 3630 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 3631 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 3632 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 3633 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 3634 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 3635 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 3636 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3637 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 3638 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 3639 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 3640 | } |
| 3641 | return Opc; |
| 3642 | } |
| 3643 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3644 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 3645 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 3646 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3647 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 3648 | unsigned Op, |
| 3649 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3650 | QualType ResultTy; // Result type of the binary operator. |
| 3651 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 3652 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
| 3653 | |
| 3654 | switch (Opc) { |
| 3655 | default: |
| 3656 | assert(0 && "Unknown binary expr!"); |
| 3657 | case BinaryOperator::Assign: |
| 3658 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 3659 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3660 | case BinaryOperator::PtrMemD: |
| 3661 | case BinaryOperator::PtrMemI: |
| 3662 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 3663 | Opc == BinaryOperator::PtrMemI); |
| 3664 | break; |
| 3665 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3666 | case BinaryOperator::Div: |
| 3667 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 3668 | break; |
| 3669 | case BinaryOperator::Rem: |
| 3670 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 3671 | break; |
| 3672 | case BinaryOperator::Add: |
| 3673 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 3674 | break; |
| 3675 | case BinaryOperator::Sub: |
| 3676 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 3677 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3678 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3679 | case BinaryOperator::Shr: |
| 3680 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 3681 | break; |
| 3682 | case BinaryOperator::LE: |
| 3683 | case BinaryOperator::LT: |
| 3684 | case BinaryOperator::GE: |
| 3685 | case BinaryOperator::GT: |
| 3686 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, true); |
| 3687 | break; |
| 3688 | case BinaryOperator::EQ: |
| 3689 | case BinaryOperator::NE: |
| 3690 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, false); |
| 3691 | break; |
| 3692 | case BinaryOperator::And: |
| 3693 | case BinaryOperator::Xor: |
| 3694 | case BinaryOperator::Or: |
| 3695 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 3696 | break; |
| 3697 | case BinaryOperator::LAnd: |
| 3698 | case BinaryOperator::LOr: |
| 3699 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 3700 | break; |
| 3701 | case BinaryOperator::MulAssign: |
| 3702 | case BinaryOperator::DivAssign: |
| 3703 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 3704 | if (!CompTy.isNull()) |
| 3705 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3706 | break; |
| 3707 | case BinaryOperator::RemAssign: |
| 3708 | CompTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 3709 | if (!CompTy.isNull()) |
| 3710 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3711 | break; |
| 3712 | case BinaryOperator::AddAssign: |
| 3713 | CompTy = CheckAdditionOperands(lhs, rhs, OpLoc, true); |
| 3714 | if (!CompTy.isNull()) |
| 3715 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3716 | break; |
| 3717 | case BinaryOperator::SubAssign: |
| 3718 | CompTy = CheckSubtractionOperands(lhs, rhs, OpLoc, true); |
| 3719 | if (!CompTy.isNull()) |
| 3720 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3721 | break; |
| 3722 | case BinaryOperator::ShlAssign: |
| 3723 | case BinaryOperator::ShrAssign: |
| 3724 | CompTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 3725 | if (!CompTy.isNull()) |
| 3726 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3727 | break; |
| 3728 | case BinaryOperator::AndAssign: |
| 3729 | case BinaryOperator::XorAssign: |
| 3730 | case BinaryOperator::OrAssign: |
| 3731 | CompTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 3732 | if (!CompTy.isNull()) |
| 3733 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompTy); |
| 3734 | break; |
| 3735 | case BinaryOperator::Comma: |
| 3736 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 3737 | break; |
| 3738 | } |
| 3739 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3740 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3741 | if (CompTy.isNull()) |
| 3742 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 3743 | else |
| 3744 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Steve Naroff | 9e0b600 | 2009-01-20 21:06:31 +0000 | [diff] [blame] | 3745 | CompTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3748 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3749 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 3750 | tok::TokenKind Kind, |
| 3751 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3752 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3753 | Expr *lhs = (Expr *)LHS.release(), *rhs = (Expr*)RHS.release(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3754 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3755 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 3756 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3757 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3758 | // If either expression is type-dependent, just build the AST. |
| 3759 | // FIXME: We'll need to perform some caching of the result of name |
| 3760 | // lookup for operator+. |
| 3761 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3762 | if (Opc > BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) |
| 3763 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3764 | Context.DependentTy, |
| 3765 | Context.DependentTy, TokLoc)); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3766 | else |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3767 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, |
| 3768 | Context.DependentTy, TokLoc)); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3771 | if (getLangOptions().CPlusPlus && Opc != BinaryOperator::PtrMemD && |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3772 | (lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType() || |
| 3773 | rhs->getType()->isRecordType() || rhs->getType()->isEnumeralType())) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3774 | // If this is one of the assignment operators, we only perform |
| 3775 | // overload resolution if the left-hand side is a class or |
| 3776 | // enumeration type (C++ [expr.ass]p3). |
| 3777 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
| 3778 | !(lhs->getType()->isRecordType() || lhs->getType()->isEnumeralType())) { |
| 3779 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
| 3780 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3781 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3782 | // Determine which overloaded operator we're dealing with. |
| 3783 | static const OverloadedOperatorKind OverOps[] = { |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3784 | // Overloading .* is not possible. |
| 3785 | static_cast<OverloadedOperatorKind>(0), OO_ArrowStar, |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3786 | OO_Star, OO_Slash, OO_Percent, |
| 3787 | OO_Plus, OO_Minus, |
| 3788 | OO_LessLess, OO_GreaterGreater, |
| 3789 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 3790 | OO_EqualEqual, OO_ExclaimEqual, |
| 3791 | OO_Amp, |
| 3792 | OO_Caret, |
| 3793 | OO_Pipe, |
| 3794 | OO_AmpAmp, |
| 3795 | OO_PipePipe, |
| 3796 | OO_Equal, OO_StarEqual, |
| 3797 | OO_SlashEqual, OO_PercentEqual, |
| 3798 | OO_PlusEqual, OO_MinusEqual, |
| 3799 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 3800 | OO_AmpEqual, OO_CaretEqual, |
| 3801 | OO_PipeEqual, |
| 3802 | OO_Comma |
| 3803 | }; |
| 3804 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3805 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3806 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 3807 | // to the candidate set. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3808 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3809 | Expr *Args[2] = { lhs, rhs }; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3810 | if (AddOperatorCandidates(OverOp, S, TokLoc, Args, 2, CandidateSet)) |
| 3811 | return ExprError(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3812 | |
| 3813 | // Perform overload resolution. |
| 3814 | OverloadCandidateSet::iterator Best; |
| 3815 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3816 | case OR_Success: { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3817 | // We found a built-in operator or an overloaded operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3818 | FunctionDecl *FnDecl = Best->Function; |
| 3819 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3820 | if (FnDecl) { |
| 3821 | // We matched an overloaded operator. Build a call to that |
| 3822 | // operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3823 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3824 | // Convert the arguments. |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3825 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3826 | if (PerformObjectArgumentInitialization(lhs, Method) || |
| 3827 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(0)->getType(), |
| 3828 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3829 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3830 | } else { |
| 3831 | // Convert the arguments. |
| 3832 | if (PerformCopyInitialization(lhs, FnDecl->getParamDecl(0)->getType(), |
| 3833 | "passing") || |
| 3834 | PerformCopyInitialization(rhs, FnDecl->getParamDecl(1)->getType(), |
| 3835 | "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3836 | return ExprError(); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 3837 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3838 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3839 | // Determine the result type |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3840 | QualType ResultTy |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3841 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3842 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3844 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3845 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3846 | SourceLocation()); |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 3847 | UsualUnaryConversions(FnExpr); |
| 3848 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3849 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3850 | ResultTy, TokLoc)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3851 | } else { |
| 3852 | // We matched a built-in operator. Convert the arguments, then |
| 3853 | // break out so that we will build the appropriate built-in |
| 3854 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3855 | if (PerformImplicitConversion(lhs, Best->BuiltinTypes.ParamTypes[0], |
| 3856 | Best->Conversions[0], "passing") || |
| 3857 | PerformImplicitConversion(rhs, Best->BuiltinTypes.ParamTypes[1], |
| 3858 | Best->Conversions[1], "passing")) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3859 | return ExprError(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3860 | |
| 3861 | break; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3862 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | case OR_No_Viable_Function: |
| 3866 | // No viable function; fall through to handling this as a |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3867 | // built-in operator, which will produce an error message for us. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3868 | break; |
| 3869 | |
| 3870 | case OR_Ambiguous: |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3871 | Diag(TokLoc, diag::err_ovl_ambiguous_oper) |
| 3872 | << BinaryOperator::getOpcodeStr(Opc) |
| 3873 | << lhs->getSourceRange() << rhs->getSourceRange(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3874 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3875 | return ExprError(); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3878 | // Either we found no viable overloaded operator or we matched a |
| 3879 | // built-in operator. In either case, fall through to trying to |
| 3880 | // build a built-in operation. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 3883 | // Build a built-in binary operation. |
| 3884 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | // Unary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3888 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 3889 | tok::TokenKind Op, ExprArg input) { |
| 3890 | // FIXME: Input is modified later, but smart pointer not reassigned. |
| 3891 | Expr *Input = (Expr*)input.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3892 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3893 | |
| 3894 | if (getLangOptions().CPlusPlus && |
| 3895 | (Input->getType()->isRecordType() |
| 3896 | || Input->getType()->isEnumeralType())) { |
| 3897 | // Determine which overloaded operator we're dealing with. |
| 3898 | static const OverloadedOperatorKind OverOps[] = { |
| 3899 | OO_None, OO_None, |
| 3900 | OO_PlusPlus, OO_MinusMinus, |
| 3901 | OO_Amp, OO_Star, |
| 3902 | OO_Plus, OO_Minus, |
| 3903 | OO_Tilde, OO_Exclaim, |
| 3904 | OO_None, OO_None, |
| 3905 | OO_None, |
| 3906 | OO_None |
| 3907 | }; |
| 3908 | OverloadedOperatorKind OverOp = OverOps[Opc]; |
| 3909 | |
| 3910 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 3911 | // to the candidate set. |
| 3912 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 3913 | if (OverOp != OO_None && |
| 3914 | AddOperatorCandidates(OverOp, S, OpLoc, &Input, 1, CandidateSet)) |
| 3915 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3916 | |
| 3917 | // Perform overload resolution. |
| 3918 | OverloadCandidateSet::iterator Best; |
| 3919 | switch (BestViableFunction(CandidateSet, Best)) { |
| 3920 | case OR_Success: { |
| 3921 | // We found a built-in operator or an overloaded operator. |
| 3922 | FunctionDecl *FnDecl = Best->Function; |
| 3923 | |
| 3924 | if (FnDecl) { |
| 3925 | // We matched an overloaded operator. Build a call to that |
| 3926 | // operator. |
| 3927 | |
| 3928 | // Convert the arguments. |
| 3929 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 3930 | if (PerformObjectArgumentInitialization(Input, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3931 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3932 | } else { |
| 3933 | // Convert the arguments. |
| 3934 | if (PerformCopyInitialization(Input, |
| 3935 | FnDecl->getParamDecl(0)->getType(), |
| 3936 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3937 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3941 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3942 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 3943 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3945 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3946 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 3947 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3948 | UsualUnaryConversions(FnExpr); |
| 3949 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3950 | input.release(); |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3951 | return Owned(new (Context) CXXOperatorCallExpr(Context, FnExpr, &Input, 1, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3952 | ResultTy, OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3953 | } else { |
| 3954 | // We matched a built-in operator. Convert the arguments, then |
| 3955 | // break out so that we will build the appropriate built-in |
| 3956 | // operator node. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3957 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 3958 | Best->Conversions[0], "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3959 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3960 | |
| 3961 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3962 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | case OR_No_Viable_Function: |
| 3966 | // No viable function; fall through to handling this as a |
| 3967 | // built-in operator, which will produce an error message for us. |
| 3968 | break; |
| 3969 | |
| 3970 | case OR_Ambiguous: |
| 3971 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 3972 | << UnaryOperator::getOpcodeStr(Opc) |
| 3973 | << Input->getSourceRange(); |
| 3974 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3975 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | // Either we found no viable overloaded operator or we matched a |
| 3979 | // built-in operator. In either case, fall through to trying to |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3980 | // build a built-in operation. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3983 | QualType resultType; |
| 3984 | switch (Opc) { |
| 3985 | default: |
| 3986 | assert(0 && "Unimplemented unary expr!"); |
| 3987 | case UnaryOperator::PreInc: |
| 3988 | case UnaryOperator::PreDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 3989 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 3990 | Opc == UnaryOperator::PreInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3991 | break; |
| 3992 | case UnaryOperator::AddrOf: |
| 3993 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 3994 | break; |
| 3995 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 3996 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3997 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 3998 | break; |
| 3999 | case UnaryOperator::Plus: |
| 4000 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4001 | UsualUnaryConversions(Input); |
| 4002 | resultType = Input->getType(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4003 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4004 | break; |
| 4005 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4006 | resultType->isEnumeralType()) |
| 4007 | break; |
| 4008 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4009 | Opc == UnaryOperator::Plus && |
| 4010 | resultType->isPointerType()) |
| 4011 | break; |
| 4012 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4013 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4014 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4015 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4016 | UsualUnaryConversions(Input); |
| 4017 | resultType = Input->getType(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4018 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4019 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4020 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4021 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4022 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4023 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4024 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4025 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4026 | break; |
| 4027 | case UnaryOperator::LNot: // logical negation |
| 4028 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4029 | DefaultFunctionArrayConversion(Input); |
| 4030 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4031 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4032 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4033 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4034 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4035 | // In C++, it's bool. C++ 5.3.1p8 |
| 4036 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4037 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4038 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4039 | case UnaryOperator::Imag: |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 4040 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4041 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4043 | resultType = Input->getType(); |
| 4044 | break; |
| 4045 | } |
| 4046 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4047 | return ExprError(); |
| 4048 | input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4049 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4052 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 4053 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4054 | SourceLocation LabLoc, |
| 4055 | IdentifierInfo *LabelII) { |
| 4056 | // Look up the record for this label identifier. |
| 4057 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 4058 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4059 | // If we haven't seen this label yet, create a forward reference. It |
| 4060 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4061 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4062 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4063 | |
| 4064 | // 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] | 4065 | return new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4066 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4069 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4070 | SourceLocation RPLoc) { // "({..})" |
| 4071 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 4072 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4073 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4074 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4075 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| 4076 | if (isFileScope) { |
| 4077 | return Diag(LPLoc, diag::err_stmtexpr_file_scope); |
| 4078 | } |
| 4079 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4080 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4081 | // example, it is not possible to goto into a stmt expression apparently. |
| 4082 | // More semantic analysis is needed. |
| 4083 | |
| 4084 | // FIXME: the last statement in the compount stmt has its value used. We |
| 4085 | // should not warn about it being unused. |
| 4086 | |
| 4087 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4088 | // as the type of the stmtexpr. |
| 4089 | QualType Ty = Context.VoidTy; |
| 4090 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4091 | if (!Compound->body_empty()) { |
| 4092 | Stmt *LastStmt = Compound->body_back(); |
| 4093 | // If LastStmt is a label, skip down through into the body. |
| 4094 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4095 | LastStmt = Label->getSubStmt(); |
| 4096 | |
| 4097 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4098 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4099 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4100 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4101 | return new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4102 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4103 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4104 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4105 | SourceLocation BuiltinLoc, |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4106 | SourceLocation TypeLoc, |
| 4107 | TypeTy *argty, |
| 4108 | OffsetOfComponent *CompPtr, |
| 4109 | unsigned NumComponents, |
| 4110 | SourceLocation RPLoc) { |
| 4111 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4112 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 4113 | |
| 4114 | // We must have at least one component that refers to the type, and the first |
| 4115 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4116 | // a struct/union/class. |
| 4117 | if (!ArgTy->isRecordType()) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4118 | return Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4119 | |
| 4120 | // Otherwise, create a compound literal expression as the base, and |
| 4121 | // iteratively process the offsetof designators. |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4122 | InitListExpr *IList = |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 4123 | new (Context) InitListExpr(SourceLocation(), 0, 0, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4124 | IList->setType(ArgTy); |
| 4125 | Expr *Res = |
| 4126 | new (Context) CompoundLiteralExpr(SourceLocation(), ArgTy, IList, false); |
| 4127 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4128 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4129 | // GCC extension, diagnose them. |
| 4130 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4131 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4132 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4133 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4134 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4135 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4136 | if (OC.isBrackets) { |
| 4137 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4138 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4139 | if (!AT) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4140 | Res->Destroy(Context); |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4141 | return Diag(OC.LocEnd, diag::err_offsetof_array_type) << Res->getType(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4144 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4145 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4146 | // C99 6.5.2.1p1 |
| 4147 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 4148 | if (!Idx->getType()->isIntegerType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4149 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript) |
| 4150 | << Idx->getSourceRange(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4151 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4152 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4153 | OC.LocEnd); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4154 | continue; |
| 4155 | } |
| 4156 | |
| 4157 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4158 | if (!RC) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4159 | Res->Destroy(Context); |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4160 | return Diag(OC.LocEnd, diag::err_offsetof_record_type) << Res->getType(); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | // Get the decl corresponding to this. |
| 4164 | RecordDecl *RD = RC->getDecl(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4165 | FieldDecl *MemberDecl |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 4166 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4167 | LookupMemberName) |
| 4168 | .getAsDecl()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4169 | if (!MemberDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 4170 | return Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4171 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd); |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4172 | |
| 4173 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 4174 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 4175 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 4176 | // matter here. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4177 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 4178 | MemberDecl->getType().getNonReferenceType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4181 | return new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 4182 | Context.getSizeType(), BuiltinLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4186 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4187 | TypeTy *arg1, TypeTy *arg2, |
| 4188 | SourceLocation RPLoc) { |
| 4189 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 4190 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 4191 | |
| 4192 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 4193 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4194 | return new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, |
| 4195 | argT2, RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4198 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4199 | ExprTy *expr1, ExprTy *expr2, |
| 4200 | SourceLocation RPLoc) { |
| 4201 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 4202 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 4203 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 4204 | |
| 4205 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 4206 | |
| 4207 | // The conditional expression is required to be a constant expression. |
| 4208 | llvm::APSInt condEval(32); |
| 4209 | SourceLocation ExpLoc; |
| 4210 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4211 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant) |
| 4212 | << CondExpr->getSourceRange(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4213 | |
| 4214 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 4215 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 4216 | RHSExpr->getType(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4217 | return new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 4218 | resType, RPLoc); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4221 | //===----------------------------------------------------------------------===// |
| 4222 | // Clang Extensions. |
| 4223 | //===----------------------------------------------------------------------===// |
| 4224 | |
| 4225 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4226 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4227 | // Analyze block parameters. |
| 4228 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
| 4229 | |
| 4230 | // Add BSI to CurBlock. |
| 4231 | BSI->PrevBlockInfo = CurBlock; |
| 4232 | CurBlock = BSI; |
| 4233 | |
| 4234 | BSI->ReturnType = 0; |
| 4235 | BSI->TheScope = BlockScope; |
| 4236 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4237 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4238 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4241 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| 4242 | assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); |
| 4243 | |
| 4244 | if (ParamInfo.getNumTypeObjects() == 0 |
| 4245 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
| 4246 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 4247 | |
| 4248 | // The type is entirely optional as well, if none, use DependentTy. |
| 4249 | if (T.isNull()) |
| 4250 | T = Context.DependentTy; |
| 4251 | |
| 4252 | // The parameter list is optional, if there was none, assume (). |
| 4253 | if (!T->isFunctionType()) |
| 4254 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 4255 | |
| 4256 | CurBlock->hasPrototype = true; |
| 4257 | CurBlock->isVariadic = false; |
| 4258 | Type *RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4259 | .getTypePtr(); |
| 4260 | |
| 4261 | if (!RetTy->isDependentType()) |
| 4262 | CurBlock->ReturnType = RetTy; |
| 4263 | return; |
| 4264 | } |
| 4265 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4266 | // Analyze arguments to block. |
| 4267 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 4268 | "Not a function declarator!"); |
| 4269 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4270 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4271 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 4272 | CurBlock->isVariadic = true; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4273 | |
| 4274 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 4275 | // no arguments, not a function that takes a single void argument. |
| 4276 | if (FTI.hasPrototype && |
| 4277 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4278 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 4279 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 4280 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4281 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4282 | } else if (FTI.hasPrototype) { |
| 4283 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4284 | CurBlock->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 4285 | CurBlock->isVariadic = FTI.isVariadic; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 4286 | QualType T = GetTypeForDeclarator (ParamInfo, CurScope); |
| 4287 | |
| 4288 | Type* RetTy = T.getTypePtr()->getAsFunctionType()->getResultType() |
| 4289 | .getTypePtr(); |
| 4290 | |
| 4291 | if (!RetTy->isDependentType()) |
| 4292 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4293 | } |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4294 | CurBlock->TheDecl->setArgs(&CurBlock->Params[0], CurBlock->Params.size()); |
| 4295 | |
| 4296 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 4297 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 4298 | // If this has an identifier, add it to the scope stack. |
| 4299 | if ((*AI)->getIdentifier()) |
| 4300 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 4304 | /// is invoked to pop the information about the block from the action impl. |
| 4305 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 4306 | // Ensure that CurBlock is deleted. |
| 4307 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
| 4308 | |
| 4309 | // Pop off CurBlock, handle nested blocks. |
| 4310 | CurBlock = CurBlock->PrevBlockInfo; |
| 4311 | |
| 4312 | // FIXME: Delete the ParmVarDecl objects as well??? |
| 4313 | |
| 4314 | } |
| 4315 | |
| 4316 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 4317 | /// literal was successfully completed. ^(int x){...} |
| 4318 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 4319 | Scope *CurScope) { |
| 4320 | // Ensure that CurBlock is deleted. |
| 4321 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4322 | ExprOwningPtr<CompoundStmt> Body(this, static_cast<CompoundStmt*>(body)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4323 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 4324 | PopDeclContext(); |
| 4325 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4326 | // Pop off CurBlock, handle nested blocks. |
| 4327 | CurBlock = CurBlock->PrevBlockInfo; |
| 4328 | |
| 4329 | QualType RetTy = Context.VoidTy; |
| 4330 | if (BSI->ReturnType) |
| 4331 | RetTy = QualType(BSI->ReturnType, 0); |
| 4332 | |
| 4333 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 4334 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 4335 | ArgTypes.push_back(BSI->Params[i]->getType()); |
| 4336 | |
| 4337 | QualType BlockTy; |
| 4338 | if (!BSI->hasPrototype) |
| 4339 | BlockTy = Context.getFunctionTypeNoProto(RetTy); |
| 4340 | else |
| 4341 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4342 | BSI->isVariadic, 0); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4343 | |
| 4344 | BlockTy = Context.getBlockPointerType(BlockTy); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4345 | |
Steve Naroff | 1c90bfc | 2008-10-08 18:44:00 +0000 | [diff] [blame] | 4346 | BSI->TheDecl->setBody(Body.take()); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4347 | return new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4350 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4351 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4352 | /// The number of arguments has already been validated to match the number of |
| 4353 | /// arguments in FnType. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4354 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType, |
| 4355 | ASTContext &Context) { |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4356 | unsigned NumParams = FnType->getNumArgs(); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4357 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4358 | QualType ExprTy = Context.getCanonicalType(Args[i]->getType()); |
| 4359 | QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i)); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4360 | |
| 4361 | if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType()) |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4362 | return false; |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 4363 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4364 | return true; |
| 4365 | } |
| 4366 | |
| 4367 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 4368 | SourceLocation *CommaLocs, |
| 4369 | SourceLocation BuiltinLoc, |
| 4370 | SourceLocation RParenLoc) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4371 | // __builtin_overload requires at least 2 arguments |
| 4372 | if (NumArgs < 2) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4373 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4374 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4375 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4376 | // The first argument is required to be a constant expression. It tells us |
| 4377 | // 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] | 4378 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4379 | Expr *NParamsExpr = Args[0]; |
| 4380 | llvm::APSInt constEval(32); |
| 4381 | SourceLocation ExpLoc; |
| 4382 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4383 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4384 | << NParamsExpr->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4385 | |
| 4386 | // Verify that the number of parameters is > 0 |
| 4387 | unsigned NumParams = constEval.getZExtValue(); |
| 4388 | if (NumParams == 0) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4389 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant) |
| 4390 | << NParamsExpr->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4391 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 4392 | if ((NumParams + 1) > NumArgs) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4393 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 4394 | << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4395 | |
| 4396 | // 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] | 4397 | // listed after the parameters. |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4398 | OverloadExpr *OE = 0; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4399 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 4400 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 4401 | // pointer to function. |
| 4402 | Expr *Fn = UsualUnaryConversions(Args[i]); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4403 | const FunctionTypeProto *FnType = 0; |
| 4404 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) |
| 4405 | FnType = PT->getPointeeType()->getAsFunctionTypeProto(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4406 | |
| 4407 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 4408 | // parameters, and the number of parameters must match the value passed to |
| 4409 | // the builtin. |
| 4410 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4411 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype) |
| 4412 | << Fn->getSourceRange(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4413 | |
| 4414 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4415 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4416 | // If they match, return a new OverloadExpr. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4417 | if (ExprsMatchFnType(Args+1, FnType, Context)) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4418 | if (OE) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4419 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match) |
| 4420 | << OE->getFn()->getSourceRange(); |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4421 | // Remember our match, and continue processing the remaining arguments |
| 4422 | // to catch any errors. |
Ted Kremenek | fb7413f | 2009-02-09 17:08:14 +0000 | [diff] [blame] | 4423 | OE = new (Context) OverloadExpr(Context, Args, NumArgs, i, |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4424 | FnType->getResultType().getNonReferenceType(), |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4425 | BuiltinLoc, RParenLoc); |
| 4426 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4427 | } |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 4428 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 4429 | // exactly one of the candidate functions. |
| 4430 | if (OE) |
| 4431 | return OE; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4432 | |
| 4433 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 4434 | // the return an error. |
| 4435 | std::string typeNames; |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 4436 | for (unsigned i = 0; i != NumParams; ++i) { |
| 4437 | if (i != 0) typeNames += ", "; |
| 4438 | typeNames += Args[i+1]->getType().getAsString(); |
| 4439 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4440 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4441 | return Diag(BuiltinLoc, diag::err_overload_no_match) |
| 4442 | << typeNames << SourceRange(BuiltinLoc, RParenLoc); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4445 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 4446 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4447 | SourceLocation RPLoc) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4448 | Expr *E = static_cast<Expr*>(expr); |
| 4449 | QualType T = QualType::getFromOpaquePtr(type); |
| 4450 | |
| 4451 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4452 | |
| 4453 | // Get the va_list type |
| 4454 | QualType VaListType = Context.getBuiltinVaListType(); |
| 4455 | // Deal with implicit array decay; for example, on x86-64, |
| 4456 | // va_list is an array, but it's supposed to decay to |
| 4457 | // a pointer for va_arg. |
| 4458 | if (VaListType->isArrayType()) |
| 4459 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | efbe85c | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 4460 | // Make sure the input expression also decays appropriately. |
| 4461 | UsualUnaryConversions(E); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 4462 | |
| 4463 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4464 | return Diag(E->getLocStart(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4465 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4466 | << E->getType() << E->getSourceRange(); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4467 | |
| 4468 | // FIXME: Warn if a non-POD type is passed in. |
| 4469 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4470 | return new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), RPLoc); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4473 | Sema::ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| 4474 | // The type of __null will be int or long, depending on the size of |
| 4475 | // pointers on the target. |
| 4476 | QualType Ty; |
| 4477 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 4478 | Ty = Context.IntTy; |
| 4479 | else |
| 4480 | Ty = Context.LongTy; |
| 4481 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4482 | return new (Context) GNUNullExpr(Ty, TokenLoc); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4485 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 4486 | SourceLocation Loc, |
| 4487 | QualType DstType, QualType SrcType, |
| 4488 | Expr *SrcExpr, const char *Flavor) { |
| 4489 | // Decode the result (notice that AST's are still created for extensions). |
| 4490 | bool isInvalid = false; |
| 4491 | unsigned DiagKind; |
| 4492 | switch (ConvTy) { |
| 4493 | default: assert(0 && "Unknown conversion type"); |
| 4494 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4495 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4496 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 4497 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4498 | case IntToPointer: |
| 4499 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 4500 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4501 | case IncompatiblePointer: |
| 4502 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 4503 | break; |
| 4504 | case FunctionVoidPointer: |
| 4505 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 4506 | break; |
| 4507 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 4508 | // If the qualifiers lost were because we were applying the |
| 4509 | // (deprecated) C++ conversion from a string literal to a char* |
| 4510 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 4511 | // Ideally, this check would be performed in |
| 4512 | // CheckPointerTypesForAssignment. However, that would require a |
| 4513 | // bit of refactoring (so that the second argument is an |
| 4514 | // expression, rather than a type), which should be done as part |
| 4515 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 4516 | // C++ semantics. |
| 4517 | if (getLangOptions().CPlusPlus && |
| 4518 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 4519 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4520 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 4521 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4522 | case IntToBlockPointer: |
| 4523 | DiagKind = diag::err_int_to_block_pointer; |
| 4524 | break; |
| 4525 | case IncompatibleBlockPointer: |
Steve Naroff | ba80c9a | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 4526 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4527 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4528 | case IncompatibleObjCQualifiedId: |
| 4529 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
| 4530 | // it can give a more specific diagnostic. |
| 4531 | DiagKind = diag::warn_incompatible_qualified_id; |
| 4532 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4533 | case IncompatibleVectors: |
| 4534 | DiagKind = diag::warn_incompatible_vectors; |
| 4535 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4536 | case Incompatible: |
| 4537 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 4538 | isInvalid = true; |
| 4539 | break; |
| 4540 | } |
| 4541 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4542 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 4543 | << SrcExpr->getSourceRange(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4544 | return isInvalid; |
| 4545 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 4546 | |
| 4547 | bool Sema::VerifyIntegerConstantExpression(const Expr* E, llvm::APSInt *Result) |
| 4548 | { |
| 4549 | Expr::EvalResult EvalResult; |
| 4550 | |
| 4551 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
| 4552 | EvalResult.HasSideEffects) { |
| 4553 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 4554 | |
| 4555 | if (EvalResult.Diag) { |
| 4556 | // We only show the note if it's not the usual "invalid subexpression" |
| 4557 | // or if it's actually in a subexpression. |
| 4558 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 4559 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 4560 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4561 | } |
| 4562 | |
| 4563 | return true; |
| 4564 | } |
| 4565 | |
| 4566 | if (EvalResult.Diag) { |
| 4567 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 4568 | E->getSourceRange(); |
| 4569 | |
| 4570 | // Print the reason it's not a constant. |
| 4571 | if (Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 4572 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 4573 | } |
| 4574 | |
| 4575 | if (Result) |
| 4576 | *Result = EvalResult.Val.getInt(); |
| 4577 | return false; |
| 4578 | } |