Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Lex/LiteralSupport.h" |
Daniel Dunbar | cc7b160 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
| 25 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Standard Promotions and Conversions |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 32 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 33 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 34 | QualType Ty = E->getType(); |
| 35 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 36 | |
| 37 | if (const ReferenceType *ref = Ty->getAsReferenceType()) { |
| 38 | ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr] |
| 39 | Ty = E->getType(); |
| 40 | } |
| 41 | if (Ty->isFunctionType()) |
| 42 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 43 | else if (Ty->isArrayType()) { |
| 44 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 45 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 46 | // type 'array of type' is converted to an expression that has type 'pointer |
| 47 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 48 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 49 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 50 | // |
| 51 | // C++ 4.2p1: |
| 52 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 53 | // T" can be converted to an rvalue of type "pointer to T". |
| 54 | // |
| 55 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 56 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 57 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 58 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 62 | /// operators (C99 6.3). The conversions of array and function types are |
| 63 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 64 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 65 | /// In these instances, this routine should *not* be called. |
| 66 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 67 | QualType Ty = Expr->getType(); |
| 68 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 69 | |
| 70 | if (const ReferenceType *Ref = Ty->getAsReferenceType()) { |
| 71 | ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr] |
| 72 | Ty = Expr->getType(); |
| 73 | } |
| 74 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 75 | ImpCastExprToType(Expr, Context.IntTy); |
| 76 | else |
| 77 | DefaultFunctionArrayConversion(Expr); |
| 78 | |
| 79 | return Expr; |
| 80 | } |
| 81 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 82 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 83 | /// do not have a prototype. Arguments that have type float are promoted to |
| 84 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 85 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 86 | QualType Ty = Expr->getType(); |
| 87 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 88 | |
| 89 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 90 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 91 | if (BT->getKind() == BuiltinType::Float) |
| 92 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 93 | |
| 94 | UsualUnaryConversions(Expr); |
| 95 | } |
| 96 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 97 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 98 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 99 | /// routine returns the first non-arithmetic type found. The client is |
| 100 | /// responsible for emitting appropriate error diagnostics. |
| 101 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 102 | /// GCC. |
| 103 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 104 | bool isCompAssign) { |
| 105 | if (!isCompAssign) { |
| 106 | UsualUnaryConversions(lhsExpr); |
| 107 | UsualUnaryConversions(rhsExpr); |
| 108 | } |
| 109 | // For conversion purposes, we ignore any qualifiers. |
| 110 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 111 | QualType lhs = |
| 112 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 113 | QualType rhs = |
| 114 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 115 | |
| 116 | // If both types are identical, no conversion is needed. |
| 117 | if (lhs == rhs) |
| 118 | return lhs; |
| 119 | |
| 120 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 121 | // The caller can deal with this (e.g. pointer + int). |
| 122 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 123 | return lhs; |
| 124 | |
| 125 | // At this point, we have two different arithmetic types. |
| 126 | |
| 127 | // Handle complex types first (C99 6.3.1.8p1). |
| 128 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 129 | // if we have an integer operand, the result is the complex type. |
| 130 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 131 | // convert the rhs to the lhs complex type. |
| 132 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 133 | return lhs; |
| 134 | } |
| 135 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 136 | // convert the lhs to the rhs complex type. |
| 137 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
| 138 | return rhs; |
| 139 | } |
| 140 | // This handles complex/complex, complex/float, or float/complex. |
| 141 | // When both operands are complex, the shorter operand is converted to the |
| 142 | // type of the longer, and that is the type of the result. This corresponds |
| 143 | // to what is done when combining two real floating-point operands. |
| 144 | // The fun begins when size promotion occur across type domains. |
| 145 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 146 | // floating-point type, the less precise type is converted, within it's |
| 147 | // real or complex domain, to the precision of the other type. For example, |
| 148 | // when combining a "long double" with a "double _Complex", the |
| 149 | // "double _Complex" is promoted to "long double _Complex". |
| 150 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 151 | |
| 152 | if (result > 0) { // The left side is bigger, convert rhs. |
| 153 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
| 154 | if (!isCompAssign) |
| 155 | ImpCastExprToType(rhsExpr, rhs); |
| 156 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 157 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
| 158 | if (!isCompAssign) |
| 159 | ImpCastExprToType(lhsExpr, lhs); |
| 160 | } |
| 161 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 162 | // domains match. This is a requirement for our implementation, C99 |
| 163 | // does not require this promotion. |
| 164 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 165 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 166 | if (!isCompAssign) |
| 167 | ImpCastExprToType(lhsExpr, rhs); |
| 168 | return rhs; |
| 169 | } else { // handle "_Complex double, double". |
| 170 | if (!isCompAssign) |
| 171 | ImpCastExprToType(rhsExpr, lhs); |
| 172 | return lhs; |
| 173 | } |
| 174 | } |
| 175 | return lhs; // The domain/size match exactly. |
| 176 | } |
| 177 | // Now handle "real" floating types (i.e. float, double, long double). |
| 178 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 179 | // if we have an integer operand, the result is the real floating type. |
| 180 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 181 | // convert rhs to the lhs floating point type. |
| 182 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 183 | return lhs; |
| 184 | } |
| 185 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 186 | // convert lhs to the rhs floating point type. |
| 187 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
| 188 | return rhs; |
| 189 | } |
| 190 | // We have two real floating types, float/complex combos were handled above. |
| 191 | // Convert the smaller operand to the bigger result. |
| 192 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 193 | |
| 194 | if (result > 0) { // convert the rhs |
| 195 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 196 | return lhs; |
| 197 | } |
| 198 | if (result < 0) { // convert the lhs |
| 199 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs |
| 200 | return rhs; |
| 201 | } |
| 202 | assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison"); |
| 203 | } |
| 204 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 205 | // Handle GCC complex int extension. |
| 206 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 207 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 208 | |
| 209 | if (lhsComplexInt && rhsComplexInt) { |
| 210 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 211 | rhsComplexInt->getElementType()) >= 0) { |
| 212 | // convert the rhs |
| 213 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 214 | return lhs; |
| 215 | } |
| 216 | if (!isCompAssign) |
| 217 | ImpCastExprToType(lhsExpr, rhs); // convert the lhs |
| 218 | return rhs; |
| 219 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 220 | // convert the rhs to the lhs complex type. |
| 221 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 222 | return lhs; |
| 223 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 224 | // convert the lhs to the rhs complex type. |
| 225 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
| 226 | return rhs; |
| 227 | } |
| 228 | } |
| 229 | // Finally, we have two differing integer types. |
| 230 | // The rules for this case are in C99 6.3.1.8 |
| 231 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 232 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 233 | rhsSigned = rhs->isSignedIntegerType(); |
| 234 | QualType destType; |
| 235 | if (lhsSigned == rhsSigned) { |
| 236 | // Same signedness; use the higher-ranked type |
| 237 | destType = compare >= 0 ? lhs : rhs; |
| 238 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 239 | // The unsigned type has greater than or equal rank to the |
| 240 | // signed type, so use the unsigned type |
| 241 | destType = lhsSigned ? rhs : lhs; |
| 242 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 243 | // The two types are different widths; if we are here, that |
| 244 | // means the signed type is larger than the unsigned type, so |
| 245 | // use the signed type. |
| 246 | destType = lhsSigned ? lhs : rhs; |
| 247 | } else { |
| 248 | // The signed type is higher-ranked than the unsigned type, |
| 249 | // but isn't actually any bigger (like unsigned int and long |
| 250 | // on most 32-bit systems). Use the unsigned type corresponding |
| 251 | // to the signed type. |
| 252 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 253 | } |
| 254 | if (!isCompAssign) { |
| 255 | ImpCastExprToType(lhsExpr, destType); |
| 256 | ImpCastExprToType(rhsExpr, destType); |
| 257 | } |
| 258 | return destType; |
| 259 | } |
| 260 | |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | // Semantic Analysis for various Expression Types |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
| 265 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 266 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 267 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 268 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 269 | /// multiple tokens. However, the common case is that StringToks points to one |
| 270 | /// string. |
| 271 | /// |
| 272 | Action::ExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 273 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | assert(NumStringToks && "Must have at least one string!"); |
| 275 | |
| 276 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 277 | if (Literal.hadError) |
| 278 | return ExprResult(true); |
| 279 | |
| 280 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 281 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 282 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 283 | |
| 284 | // Verify that pascal strings aren't too large. |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 285 | if (Literal.Pascal && Literal.GetStringLength() > 256) |
| 286 | return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long, |
| 287 | SourceRange(StringToks[0].getLocation(), |
| 288 | StringToks[NumStringToks-1].getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 289 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 290 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 291 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 292 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 293 | |
| 294 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 295 | if (getLangOptions().CPlusPlus) |
| 296 | StrTy.addConst(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 297 | |
| 298 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 299 | // the nul terminator character as well as the string length for pascal |
| 300 | // strings. |
| 301 | StrTy = Context.getConstantArrayType(StrTy, |
| 302 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 303 | ArrayType::Normal, 0); |
| 304 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 305 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| 306 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 307 | Literal.AnyWide, StrTy, |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 308 | StringToks[0].getLocation(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 309 | StringToks[NumStringToks-1].getLocation()); |
| 310 | } |
| 311 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 312 | /// DeclDefinedWithinScope - Return true if the specified decl is defined at or |
| 313 | /// within the 'Within' scope. The current Scope is CurScope. |
| 314 | /// |
Steve Naroff | 32fe5a9 | 2008-09-28 21:07:52 +0000 | [diff] [blame] | 315 | /// FIXME: This method is extremely inefficient (linear scan), this should not |
| 316 | /// be used in common cases. Replace with the more modern DeclContext. We need |
| 317 | /// to make sure both assignments below produce an error. |
| 318 | /// |
| 319 | /// int main(int argc) { |
| 320 | /// int xx; |
| 321 | /// ^(int X) { |
| 322 | /// xx = 4; // error (variable is not assignable) |
| 323 | /// argc = 3; // no error. |
| 324 | /// }; |
| 325 | /// } |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 326 | /// |
| 327 | static bool DeclDefinedWithinScope(ScopedDecl *D, Scope *Within, |
| 328 | Scope *CurScope) { |
| 329 | while (1) { |
| 330 | assert(CurScope && "CurScope not nested within 'Within'?"); |
| 331 | |
| 332 | // Check this scope for the decl. |
| 333 | if (CurScope->isDeclScope(D)) return true; |
| 334 | |
| 335 | if (CurScope == Within) return false; |
| 336 | CurScope = CurScope->getParent(); |
| 337 | } |
| 338 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 339 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 340 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 341 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 342 | /// identifier is used in a function call context. |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 343 | Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 344 | IdentifierInfo &II, |
| 345 | bool HasTrailingLParen) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 346 | // Could be enum-constant, value decl, instance variable, etc. |
Steve Naroff | 6384a01 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 347 | Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 348 | |
| 349 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 350 | // well. |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 351 | if (getCurMethodDecl()) { |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 352 | ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 353 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 354 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 355 | // found a decl, but that decl is outside the current method (i.e. a global |
| 356 | // variable). In these two cases, we do a lookup for an ivar with this |
| 357 | // name, if the lookup suceeds, we replace it our current decl. |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 358 | if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 359 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Chris Lattner | ed94f76 | 2008-07-21 04:44:44 +0000 | [diff] [blame] | 360 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II)) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 361 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 362 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 363 | IdentifierInfo &II = Context.Idents.get("self"); |
| 364 | ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
| 365 | return new ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 366 | static_cast<Expr*>(SelfExpr.Val), true, true); |
| 367 | } |
| 368 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 369 | // Needed to implement property "super.method" notation. |
Daniel Dunbar | 4837ae7 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 370 | if (SD == 0 && &II == SuperID) { |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 371 | QualType T = Context.getPointerType(Context.getObjCInterfaceType( |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 372 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 373 | return new PredefinedExpr(Loc, T, PredefinedExpr::ObjCSuper); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 374 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 375 | } |
Steve Naroff | 4d1b93d | 2008-09-10 18:33:00 +0000 | [diff] [blame] | 376 | // If we are parsing a block, check the block parameter list. |
| 377 | if (CurBlock) { |
Steve Naroff | b6f2395 | 2008-09-28 00:13:36 +0000 | [diff] [blame] | 378 | BlockSemaInfo *BLK = CurBlock; |
| 379 | do { |
| 380 | for (unsigned i = 0, e = BLK->Params.size(); i != e && D == 0; ++i) |
| 381 | if (BLK->Params[i]->getIdentifier() == &II) |
| 382 | D = BLK->Params[i]; |
| 383 | if (D) |
| 384 | break; // Found! |
| 385 | } while ((BLK = BLK->PrevBlockInfo)); // Look through any enclosing blocks. |
Steve Naroff | 4d1b93d | 2008-09-10 18:33:00 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 387 | if (D == 0) { |
| 388 | // Otherwise, this could be an implicitly declared function reference (legal |
| 389 | // in C90, extension in C99). |
| 390 | if (HasTrailingLParen && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 391 | !getLangOptions().CPlusPlus) // Not in C++. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 392 | D = ImplicitlyDefineFunction(Loc, II, S); |
| 393 | else { |
| 394 | // If this name wasn't predeclared and if this is not a function call, |
| 395 | // diagnose the problem. |
| 396 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
| 397 | } |
| 398 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 399 | |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 400 | if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D)) { |
| 401 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 402 | if (MD->isStatic()) |
| 403 | // "invalid use of member 'x' in static member function" |
| 404 | return Diag(Loc, diag::err_invalid_member_use_in_static_method, |
| 405 | FD->getName()); |
| 406 | if (cast<CXXRecordDecl>(MD->getParent()) != FD->getParent()) |
| 407 | // "invalid use of nonstatic data member 'x'" |
| 408 | return Diag(Loc, diag::err_invalid_non_static_member_use, |
| 409 | FD->getName()); |
| 410 | |
| 411 | if (FD->isInvalidDecl()) |
| 412 | return true; |
| 413 | |
| 414 | // FIXME: Use DeclRefExpr or a new Expr for a direct CXXField reference. |
| 415 | ExprResult ThisExpr = ActOnCXXThis(SourceLocation()); |
| 416 | return new MemberExpr(static_cast<Expr*>(ThisExpr.Val), |
| 417 | true, FD, Loc, FD->getType()); |
| 418 | } |
| 419 | |
| 420 | return Diag(Loc, diag::err_invalid_non_static_member_use, FD->getName()); |
| 421 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 422 | if (isa<TypedefDecl>(D)) |
| 423 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 424 | if (isa<ObjCInterfaceDecl>(D)) |
Fariborz Jahanian | 3102df9 | 2007-12-05 18:16:33 +0000 | [diff] [blame] | 425 | return Diag(Loc, diag::err_unexpected_interface, II.getName()); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 426 | if (isa<NamespaceDecl>(D)) |
| 427 | return Diag(Loc, diag::err_unexpected_namespace, II.getName()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 428 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 429 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
| 430 | ValueDecl *VD = cast<ValueDecl>(D); |
| 431 | |
| 432 | // check if referencing an identifier with __attribute__((deprecated)). |
| 433 | if (VD->getAttr<DeprecatedAttr>()) |
| 434 | Diag(Loc, diag::warn_deprecated, VD->getName()); |
| 435 | |
| 436 | // Only create DeclRefExpr's for valid Decl's. |
| 437 | if (VD->isInvalidDecl()) |
| 438 | return true; |
| 439 | |
| 440 | // If this reference is not in a block or if the referenced variable is |
| 441 | // within the block, create a normal DeclRefExpr. |
| 442 | // |
| 443 | // FIXME: This will create BlockDeclRefExprs for global variables, |
Chris Lattner | 631b135 | 2008-09-28 05:30:26 +0000 | [diff] [blame] | 444 | // function references, etc which is suboptimal :) and breaks |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 445 | // things like "integer constant expression" tests. |
| 446 | // |
Chris Lattner | 631b135 | 2008-09-28 05:30:26 +0000 | [diff] [blame] | 447 | if (!CurBlock || DeclDefinedWithinScope(VD, CurBlock->TheScope, S) || |
Steve Naroff | 91de754 | 2008-09-28 14:02:55 +0000 | [diff] [blame] | 448 | isa<EnumConstantDecl>(VD) || isa<ParmVarDecl>(VD)) |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 449 | return new DeclRefExpr(VD, VD->getType(), Loc); |
| 450 | |
| 451 | // If we are in a block and the variable is outside the current block, |
| 452 | // bind the variable reference with a BlockDeclRefExpr. |
| 453 | |
Steve Naroff | cb6ad60 | 2008-09-22 15:31:56 +0000 | [diff] [blame] | 454 | // The BlocksAttr indicates the variable is bound by-reference. |
| 455 | if (VD->getAttr<BlocksAttr>()) |
| 456 | return new BlockDeclRefExpr(VD, VD->getType(), Loc, true); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 457 | |
Steve Naroff | cb6ad60 | 2008-09-22 15:31:56 +0000 | [diff] [blame] | 458 | // Variable will be bound by-copy, make it const within the closure. |
| 459 | VD->getType().addConst(); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 460 | return new BlockDeclRefExpr(VD, VD->getType(), Loc, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 463 | Sema::ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 464 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 465 | PredefinedExpr::IdentType IT; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 466 | |
| 467 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 468 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 469 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 470 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 471 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 472 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 473 | |
| 474 | // Verify that this is in a function context. |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 475 | if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0) |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 476 | return Diag(Loc, diag::err_predef_outside_function); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 478 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 479 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 480 | unsigned Length; |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 481 | if (getCurFunctionDecl()) |
| 482 | Length = getCurFunctionDecl()->getIdentifier()->getLength(); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 483 | else |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 484 | Length = getCurMethodDecl()->getSynthesizedMethodSize(); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 485 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 486 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 487 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 488 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 489 | return new PredefinedExpr(Loc, ResTy, IT); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 492 | Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 493 | llvm::SmallString<16> CharBuffer; |
| 494 | CharBuffer.resize(Tok.getLength()); |
| 495 | const char *ThisTokBegin = &CharBuffer[0]; |
| 496 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 497 | |
| 498 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 499 | Tok.getLocation(), PP); |
| 500 | if (Literal.hadError()) |
| 501 | return ExprResult(true); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 502 | |
| 503 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 504 | |
Chris Lattner | 1aaf71c | 2008-06-07 22:35:38 +0000 | [diff] [blame] | 505 | return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type, |
| 506 | Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 509 | Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 510 | // fast path for a single digit (which is quite common). A single digit |
| 511 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 512 | if (Tok.getLength() == 1) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 513 | const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 515 | unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy)); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 516 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 517 | Context.IntTy, |
| 518 | Tok.getLocation())); |
| 519 | } |
| 520 | llvm::SmallString<512> IntegerBuffer; |
| 521 | IntegerBuffer.resize(Tok.getLength()); |
| 522 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 523 | |
| 524 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 525 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 526 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 527 | Tok.getLocation(), PP); |
| 528 | if (Literal.hadError) |
| 529 | return ExprResult(true); |
| 530 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 531 | Expr *Res; |
| 532 | |
| 533 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 534 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 535 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 536 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 537 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 538 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 539 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 540 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 541 | |
| 542 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 543 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 544 | // isExact will be set by GetFloatValue(). |
| 545 | bool isExact = false; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 546 | Res = new FloatingLiteral(Literal.GetFloatValue(Format, &isExact), &isExact, |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 547 | Ty, Tok.getLocation()); |
| 548 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 549 | } else if (!Literal.isIntegerLiteral()) { |
| 550 | return ExprResult(true); |
| 551 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 552 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 553 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 554 | // long long is a C99 feature. |
| 555 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 556 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 557 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 558 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 559 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 560 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 561 | |
| 562 | if (Literal.GetIntegerValue(ResultVal)) { |
| 563 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 564 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 565 | Ty = Context.UnsignedLongLongTy; |
| 566 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 567 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 568 | } else { |
| 569 | // If this value fits into a ULL, try to figure out what else it fits into |
| 570 | // according to the rules of C99 6.4.4.1p5. |
| 571 | |
| 572 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 573 | // be an unsigned int. |
| 574 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 575 | |
| 576 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 577 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 578 | if (!Literal.isLong && !Literal.isLongLong) { |
| 579 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 580 | unsigned IntSize = Context.Target.getIntWidth(); |
| 581 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 582 | // Does it fit in a unsigned int? |
| 583 | if (ResultVal.isIntN(IntSize)) { |
| 584 | // Does it fit in a signed int? |
| 585 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 586 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 587 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 588 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 589 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 594 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 595 | unsigned LongSize = Context.Target.getLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 596 | |
| 597 | // Does it fit in a unsigned long? |
| 598 | if (ResultVal.isIntN(LongSize)) { |
| 599 | // Does it fit in a signed long? |
| 600 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 601 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 602 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 603 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 604 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 609 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 610 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 611 | |
| 612 | // Does it fit in a unsigned long long? |
| 613 | if (ResultVal.isIntN(LongLongSize)) { |
| 614 | // Does it fit in a signed long long? |
| 615 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 616 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 617 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 618 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 619 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | // If we still couldn't decide a type, we probably have something that |
| 624 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 625 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 626 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 627 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 628 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 629 | } |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 630 | |
| 631 | if (ResultVal.getBitWidth() != Width) |
| 632 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 635 | Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 636 | } |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 637 | |
| 638 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 639 | if (Literal.isImaginary) |
| 640 | Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType())); |
| 641 | |
| 642 | return Res; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 645 | Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 646 | ExprTy *Val) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 647 | Expr *E = (Expr *)Val; |
| 648 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
| 649 | return new ParenExpr(L, R, E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 653 | /// See C99 6.3.2.1p[2-4] for more details. |
| 654 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 655 | SourceLocation OpLoc, |
| 656 | const SourceRange &ExprRange, |
| 657 | bool isSizeof) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 658 | // C99 6.5.3.4p1: |
| 659 | if (isa<FunctionType>(exprType) && isSizeof) |
| 660 | // alignof(function) is allowed. |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 661 | Diag(OpLoc, diag::ext_sizeof_function_type, ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 662 | else if (exprType->isVoidType()) |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 663 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof", |
| 664 | ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 665 | else if (exprType->isIncompleteType()) { |
| 666 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 667 | diag::err_alignof_incomplete_type, |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 668 | exprType.getAsString(), ExprRange); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 669 | return QualType(); // error |
| 670 | } |
| 671 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 672 | return Context.getSizeType(); |
| 673 | } |
| 674 | |
| 675 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 676 | ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 677 | SourceLocation LPLoc, TypeTy *Ty, |
| 678 | SourceLocation RPLoc) { |
| 679 | // If error parsing type, ignore. |
| 680 | if (Ty == 0) return true; |
| 681 | |
| 682 | // Verify that this is a valid expression. |
| 683 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
| 684 | |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 685 | QualType resultType = |
| 686 | CheckSizeOfAlignOfOperand(ArgTy, OpLoc, SourceRange(LPLoc, RPLoc),isSizeof); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 687 | |
| 688 | if (resultType.isNull()) |
| 689 | return true; |
| 690 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
| 691 | } |
| 692 | |
Chris Lattner | 5110ad5 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 693 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) { |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 694 | DefaultFunctionArrayConversion(V); |
| 695 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 696 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 697 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 698 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 699 | |
| 700 | // Otherwise they pass through real integer and floating point types here. |
| 701 | if (V->getType()->isArithmeticType()) |
| 702 | return V->getType(); |
| 703 | |
| 704 | // Reject anything else. |
| 705 | Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString()); |
| 706 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 710 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 711 | Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 712 | tok::TokenKind Kind, |
| 713 | ExprTy *Input) { |
| 714 | UnaryOperator::Opcode Opc; |
| 715 | switch (Kind) { |
| 716 | default: assert(0 && "Unknown unary op!"); |
| 717 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 718 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 719 | } |
| 720 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
| 721 | if (result.isNull()) |
| 722 | return true; |
| 723 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
| 724 | } |
| 725 | |
| 726 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 727 | ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 728 | ExprTy *Idx, SourceLocation RLoc) { |
| 729 | Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx); |
| 730 | |
| 731 | // Perform default conversions. |
| 732 | DefaultFunctionArrayConversion(LHSExp); |
| 733 | DefaultFunctionArrayConversion(RHSExp); |
| 734 | |
| 735 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 736 | |
| 737 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 738 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 739 | // in the subscript position. As a result, we need to derive the array base |
| 740 | // and index from the expression types. |
| 741 | Expr *BaseExpr, *IndexExpr; |
| 742 | QualType ResultType; |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 743 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 744 | BaseExpr = LHSExp; |
| 745 | IndexExpr = RHSExp; |
| 746 | // FIXME: need to deal with const... |
| 747 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 748 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 749 | // Handle the uncommon case of "123[Ptr]". |
| 750 | BaseExpr = RHSExp; |
| 751 | IndexExpr = LHSExp; |
| 752 | // FIXME: need to deal with const... |
| 753 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 754 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 755 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 756 | IndexExpr = RHSExp; |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 757 | |
| 758 | // Component access limited to variables (reject vec4.rg[1]). |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 759 | if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) && |
| 760 | !isa<ExtVectorElementExpr>(BaseExpr)) |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 761 | return Diag(LLoc, diag::err_ext_vector_component_access, |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 762 | SourceRange(LLoc, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 763 | // FIXME: need to deal with const... |
| 764 | ResultType = VTy->getElementType(); |
| 765 | } else { |
| 766 | return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value, |
| 767 | RHSExp->getSourceRange()); |
| 768 | } |
| 769 | // C99 6.5.2.1p1 |
| 770 | if (!IndexExpr->getType()->isIntegerType()) |
| 771 | return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 772 | IndexExpr->getSourceRange()); |
| 773 | |
| 774 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 775 | // the following check catches trying to index a pointer to a function (e.g. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 776 | // void (*)(int)) and pointers to incomplete types. Functions are not |
| 777 | // objects in C99. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 778 | if (!ResultType->isObjectType()) |
| 779 | return Diag(BaseExpr->getLocStart(), |
| 780 | diag::err_typecheck_subscript_not_object, |
| 781 | BaseExpr->getType().getAsString(), BaseExpr->getSourceRange()); |
| 782 | |
| 783 | return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc); |
| 784 | } |
| 785 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 786 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 787 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 788 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 789 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 790 | |
| 791 | // This flag determines whether or not the component is to be treated as a |
| 792 | // special name, or a regular GLSL-style component access. |
| 793 | bool SpecialComponent = false; |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 794 | |
| 795 | // The vector accessor can't exceed the number of elements. |
| 796 | const char *compStr = CompName.getName(); |
| 797 | if (strlen(compStr) > vecType->getNumElements()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 798 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 799 | baseType.getAsString(), SourceRange(CompLoc)); |
| 800 | return QualType(); |
| 801 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 802 | |
| 803 | // Check that we've found one of the special components, or that the component |
| 804 | // names must come from the same set. |
| 805 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 806 | !strcmp(compStr, "e") || !strcmp(compStr, "o")) { |
| 807 | SpecialComponent = true; |
| 808 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 809 | do |
| 810 | compStr++; |
| 811 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
| 812 | } else if (vecType->getColorAccessorIdx(*compStr) != -1) { |
| 813 | do |
| 814 | compStr++; |
| 815 | while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1); |
| 816 | } else if (vecType->getTextureAccessorIdx(*compStr) != -1) { |
| 817 | do |
| 818 | compStr++; |
| 819 | while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1); |
| 820 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 821 | |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 822 | if (!SpecialComponent && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 823 | // We didn't get to the end of the string. This means the component names |
| 824 | // didn't come from the same set *or* we encountered an illegal name. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 825 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 826 | std::string(compStr,compStr+1), SourceRange(CompLoc)); |
| 827 | return QualType(); |
| 828 | } |
| 829 | // Each component accessor can't exceed the vector type. |
| 830 | compStr = CompName.getName(); |
| 831 | while (*compStr) { |
| 832 | if (vecType->isAccessorWithinNumElements(*compStr)) |
| 833 | compStr++; |
| 834 | else |
| 835 | break; |
| 836 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 837 | if (!SpecialComponent && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 838 | // We didn't get to the end of the string. This means a component accessor |
| 839 | // exceeds the number of elements in the vector. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 840 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 841 | baseType.getAsString(), SourceRange(CompLoc)); |
| 842 | return QualType(); |
| 843 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 844 | |
| 845 | // If we have a special component name, verify that the current vector length |
| 846 | // is an even number, since all special component names return exactly half |
| 847 | // the elements. |
| 848 | if (SpecialComponent && (vecType->getNumElements() & 1U)) { |
Daniel Dunbar | 45a9180 | 2008-09-30 17:22:47 +0000 | [diff] [blame^] | 849 | Diag(OpLoc, diag::err_ext_vector_component_requires_even, |
| 850 | baseType.getAsString(), SourceRange(CompLoc)); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 851 | return QualType(); |
| 852 | } |
| 853 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 854 | // The component accessor looks fine - now we need to compute the actual type. |
| 855 | // The vector type is implied by the component accessor. For example, |
| 856 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 857 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
| 858 | unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2 |
| 859 | : strlen(CompName.getName()); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 860 | if (CompSize == 1) |
| 861 | return vecType->getElementType(); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 862 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 863 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 864 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 865 | // diagostics look bad. We want extended vector types to appear built-in. |
| 866 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 867 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 868 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 869 | } |
| 870 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 873 | /// constructSetterName - Return the setter name for the given |
| 874 | /// identifier, i.e. "set" + Name where the initial character of Name |
| 875 | /// has been capitalized. |
| 876 | // FIXME: Merge with same routine in Parser. But where should this |
| 877 | // live? |
| 878 | static IdentifierInfo *constructSetterName(IdentifierTable &Idents, |
| 879 | const IdentifierInfo *Name) { |
| 880 | unsigned N = Name->getLength(); |
| 881 | char *SelectorName = new char[3 + N]; |
| 882 | memcpy(SelectorName, "set", 3); |
| 883 | memcpy(&SelectorName[3], Name->getName(), N); |
| 884 | SelectorName[3] = toupper(SelectorName[3]); |
| 885 | |
| 886 | IdentifierInfo *Setter = |
| 887 | &Idents.get(SelectorName, &SelectorName[3 + N]); |
| 888 | delete[] SelectorName; |
| 889 | return Setter; |
| 890 | } |
| 891 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 892 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 893 | ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 894 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 895 | IdentifierInfo &Member) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 896 | Expr *BaseExpr = static_cast<Expr *>(Base); |
| 897 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 898 | |
| 899 | // Perform default conversions. |
| 900 | DefaultFunctionArrayConversion(BaseExpr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 901 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 902 | QualType BaseType = BaseExpr->getType(); |
| 903 | assert(!BaseType.isNull() && "no type for member expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 904 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 905 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 906 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 907 | if (OpKind == tok::arrow) { |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 908 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 909 | BaseType = PT->getPointeeType(); |
| 910 | else |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 911 | return Diag(MemberLoc, diag::err_typecheck_member_reference_arrow, |
| 912 | BaseType.getAsString(), BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 913 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 914 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 915 | // Handle field access to simple records. This also handles access to fields |
| 916 | // of the ObjC 'id' struct. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 917 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 918 | RecordDecl *RDecl = RTy->getDecl(); |
| 919 | if (RTy->isIncompleteType()) |
| 920 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(), |
| 921 | BaseExpr->getSourceRange()); |
| 922 | // The record definition is complete, now make sure the member is valid. |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 923 | FieldDecl *MemberDecl = RDecl->getMember(&Member); |
| 924 | if (!MemberDecl) |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 925 | return Diag(MemberLoc, diag::err_typecheck_no_member, Member.getName(), |
| 926 | BaseExpr->getSourceRange()); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 927 | |
| 928 | // Figure out the type of the member; see C99 6.5.2.3p3 |
Eli Friedman | aedabcf | 2008-02-07 05:24:51 +0000 | [diff] [blame] | 929 | // FIXME: Handle address space modifiers |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 930 | QualType MemberType = MemberDecl->getType(); |
| 931 | unsigned combinedQualifiers = |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 932 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 933 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 934 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 935 | return new MemberExpr(BaseExpr, OpKind == tok::arrow, MemberDecl, |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 936 | MemberLoc, MemberType); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 939 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 940 | // (*Obj).ivar. |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 941 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
| 942 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(&Member)) |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 943 | return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr, |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 944 | OpKind == tok::arrow); |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 945 | return Diag(MemberLoc, diag::err_typecheck_member_reference_ivar, |
Chris Lattner | 52292be | 2008-07-21 04:42:08 +0000 | [diff] [blame] | 946 | IFTy->getDecl()->getName(), Member.getName(), |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 947 | BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 950 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 951 | // pointer to a (potentially qualified) interface type. |
| 952 | const PointerType *PTy; |
| 953 | const ObjCInterfaceType *IFTy; |
| 954 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 955 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 956 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | dd85128 | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 957 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 958 | // Search for a declared property first. |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 959 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) |
| 960 | return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr); |
| 961 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 962 | // Check protocols on qualified interfaces. |
Chris Lattner | d5f8179 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 963 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 964 | E = IFTy->qual_end(); I != E; ++I) |
| 965 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) |
| 966 | return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 967 | |
| 968 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 969 | // selector is implemented. |
| 970 | |
| 971 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 972 | // shared with the code in ActOnInstanceMessage. |
| 973 | |
| 974 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 975 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
| 976 | |
| 977 | // If this reference is in an @implementation, check for 'private' methods. |
| 978 | if (!Getter) |
| 979 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 980 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 981 | if (ObjCImplementationDecl *ImpDecl = |
| 982 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 983 | Getter = ImpDecl->getInstanceMethod(Sel); |
| 984 | |
| 985 | if (Getter) { |
| 986 | // If we found a getter then this may be a valid dot-reference, we |
| 987 | // need to also look for the matching setter. |
| 988 | IdentifierInfo *SetterName = constructSetterName(PP.getIdentifierTable(), |
| 989 | &Member); |
| 990 | Selector SetterSel = PP.getSelectorTable().getUnarySelector(SetterName); |
| 991 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 992 | |
| 993 | if (!Setter) { |
| 994 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 995 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 996 | if (ObjCImplementationDecl *ImpDecl = |
| 997 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 998 | Setter = ImpDecl->getInstanceMethod(SetterSel); |
| 999 | } |
| 1000 | |
| 1001 | // FIXME: There are some issues here. First, we are not |
| 1002 | // diagnosing accesses to read-only properties because we do not |
| 1003 | // know if this is a getter or setter yet. Second, we are |
| 1004 | // checking that the type of the setter matches the type we |
| 1005 | // expect. |
| 1006 | return new ObjCPropertyRefExpr(Getter, Setter, Getter->getResultType(), |
| 1007 | MemberLoc, BaseExpr); |
| 1008 | } |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 1009 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Handle 'field access' to vectors, such as 'V.xx'. |
| 1012 | if (BaseType->isExtVectorType() && OpKind == tok::period) { |
| 1013 | // Component access limited to variables (reject vec4.rg.g). |
| 1014 | if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) && |
| 1015 | !isa<ExtVectorElementExpr>(BaseExpr)) |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 1016 | return Diag(MemberLoc, diag::err_ext_vector_component_access, |
| 1017 | BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 1018 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 1019 | if (ret.isNull()) |
| 1020 | return true; |
| 1021 | return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc); |
| 1022 | } |
| 1023 | |
Chris Lattner | 7d5a876 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 1024 | return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union, |
| 1025 | BaseType.getAsString(), BaseExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1028 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1029 | /// This provides the location of the left/right parens and a list of comma |
| 1030 | /// locations. |
| 1031 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1032 | ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1033 | ExprTy **args, unsigned NumArgs, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1034 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
| 1035 | Expr *Fn = static_cast<Expr *>(fn); |
| 1036 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 1037 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1038 | FunctionDecl *FDecl = NULL; |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Promote the function operand. |
| 1041 | UsualUnaryConversions(Fn); |
| 1042 | |
| 1043 | // If we're directly calling a function, get the declaration for |
| 1044 | // that function. |
| 1045 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 1046 | if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr())) |
| 1047 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
| 1048 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1049 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 1050 | // of arguments and function on error. |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1051 | llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs, |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1052 | Context.BoolTy, RParenLoc)); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1053 | const FunctionType *FuncT; |
| 1054 | if (!Fn->getType()->isBlockPointerType()) { |
| 1055 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 1056 | // have type pointer to function". |
| 1057 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 1058 | if (PT == 0) |
| 1059 | return Diag(LParenLoc, diag::err_typecheck_call_not_function, |
| 1060 | Fn->getSourceRange()); |
| 1061 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 1062 | } else { // This is a block call. |
| 1063 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 1064 | getAsFunctionType(); |
| 1065 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1066 | if (FuncT == 0) |
Chris Lattner | 61000b1 | 2008-08-14 04:33:24 +0000 | [diff] [blame] | 1067 | return Diag(LParenLoc, diag::err_typecheck_call_not_function, |
| 1068 | Fn->getSourceRange()); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1069 | |
| 1070 | // We know the result type of the call, set it. |
| 1071 | TheCall->setType(FuncT->getResultType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1073 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1074 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 1075 | // assignment, to the types of the corresponding parameter, ... |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1076 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 1077 | unsigned NumArgsToCheck = NumArgs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1078 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1079 | // If too few arguments are available (and we don't have default |
| 1080 | // arguments for the remaining parameters), don't make the call. |
| 1081 | if (NumArgs < NumArgsInProto) { |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1082 | if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1083 | // Use default arguments for missing arguments |
| 1084 | NumArgsToCheck = NumArgsInProto; |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1085 | TheCall->setNumArgs(NumArgsInProto); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1086 | } else |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1087 | return Diag(RParenLoc, |
| 1088 | !Fn->getType()->isBlockPointerType() |
| 1089 | ? diag::err_typecheck_call_too_few_args |
| 1090 | : diag::err_typecheck_block_too_few_args, |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1091 | Fn->getSourceRange()); |
| 1092 | } |
| 1093 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1094 | // If too many are passed and not variadic, error on the extras and drop |
| 1095 | // them. |
| 1096 | if (NumArgs > NumArgsInProto) { |
| 1097 | if (!Proto->isVariadic()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1098 | Diag(Args[NumArgsInProto]->getLocStart(), |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1099 | !Fn->getType()->isBlockPointerType() |
| 1100 | ? diag::err_typecheck_call_too_many_args |
| 1101 | : diag::err_typecheck_block_too_many_args, |
| 1102 | Fn->getSourceRange(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1103 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1104 | Args[NumArgs-1]->getLocEnd())); |
| 1105 | // This deletes the extra arguments. |
| 1106 | TheCall->setNumArgs(NumArgsInProto); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1107 | } |
| 1108 | NumArgsToCheck = NumArgsInProto; |
| 1109 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1111 | // Continue to check argument types (even if we have too few/many args). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1112 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1113 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1114 | |
| 1115 | Expr *Arg; |
| 1116 | if (i < NumArgs) |
| 1117 | Arg = Args[i]; |
| 1118 | else |
| 1119 | Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1120 | QualType ArgType = Arg->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1122 | // Compute implicit casts from the operand to the formal argument type. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1123 | AssignConvertType ConvTy = |
| 1124 | CheckSingleAssignmentConstraints(ProtoArgType, Arg); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1125 | TheCall->setArg(i, Arg); |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1127 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType, |
| 1128 | ArgType, Arg, "passing")) |
| 1129 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1130 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1131 | |
| 1132 | // If this is a variadic call, handle args passed through "...". |
| 1133 | if (Proto->isVariadic()) { |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1134 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1135 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 1136 | Expr *Arg = Args[i]; |
| 1137 | DefaultArgumentPromotion(Arg); |
| 1138 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1139 | } |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1140 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1141 | } else { |
| 1142 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
| 1143 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1144 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1145 | for (unsigned i = 0; i != NumArgs; i++) { |
| 1146 | Expr *Arg = Args[i]; |
| 1147 | DefaultArgumentPromotion(Arg); |
| 1148 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1149 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1150 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1152 | // Do special checking on direct calls to functions. |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1153 | if (FDecl) |
| 1154 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1156 | return TheCall.take(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1160 | ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1161 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1162 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1163 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 1164 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1165 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1166 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 1167 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1168 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1169 | if (literalType->isVariableArrayType()) |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1170 | return Diag(LParenLoc, |
| 1171 | diag::err_variable_object_no_init, |
| 1172 | SourceRange(LParenLoc, |
| 1173 | literalExpr->getSourceRange().getEnd())); |
| 1174 | } else if (literalType->isIncompleteType()) { |
| 1175 | return Diag(LParenLoc, |
| 1176 | diag::err_typecheck_decl_incomplete_type, |
| 1177 | literalType.getAsString(), |
| 1178 | SourceRange(LParenLoc, |
| 1179 | literalExpr->getSourceRange().getEnd())); |
| 1180 | } |
| 1181 | |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1182 | if (CheckInitializerTypes(literalExpr, literalType)) |
Steve Naroff | 92590f9 | 2008-01-09 20:58:06 +0000 | [diff] [blame] | 1183 | return true; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1184 | |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1185 | bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1186 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1187 | if (CheckForConstantInitializer(literalExpr, literalType)) |
| 1188 | return true; |
| 1189 | } |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1190 | return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1194 | ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1195 | SourceLocation RBraceLoc) { |
Steve Naroff | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1196 | Expr **InitList = reinterpret_cast<Expr**>(initlist); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1197 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1198 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | 1c9de71 | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 1199 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1201 | InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc); |
| 1202 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
| 1203 | return E; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1206 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 5ad49de | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 1207 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1208 | UsualUnaryConversions(castExpr); |
| 1209 | |
| 1210 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 1211 | // type needs to be scalar. |
| 1212 | if (castType->isVoidType()) { |
| 1213 | // Cast to void allows any expr type. |
| 1214 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
| 1215 | // GCC struct/union extension: allow cast to self. |
| 1216 | if (Context.getCanonicalType(castType) != |
| 1217 | Context.getCanonicalType(castExpr->getType()) || |
| 1218 | (!castType->isStructureType() && !castType->isUnionType())) { |
| 1219 | // Reject any other conversions to non-scalar types. |
| 1220 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar, |
| 1221 | castType.getAsString(), castExpr->getSourceRange()); |
| 1222 | } |
| 1223 | |
| 1224 | // accept this, but emit an ext-warn. |
| 1225 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar, |
| 1226 | castType.getAsString(), castExpr->getSourceRange()); |
| 1227 | } else if (!castExpr->getType()->isScalarType() && |
| 1228 | !castExpr->getType()->isVectorType()) { |
| 1229 | return Diag(castExpr->getLocStart(), |
| 1230 | diag::err_typecheck_expect_scalar_operand, |
| 1231 | castExpr->getType().getAsString(),castExpr->getSourceRange()); |
| 1232 | } else if (castExpr->getType()->isVectorType()) { |
| 1233 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 1234 | return true; |
| 1235 | } else if (castType->isVectorType()) { |
| 1236 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 1237 | return true; |
| 1238 | } |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 1242 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 1243 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 1244 | |
| 1245 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1246 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 1247 | return Diag(R.getBegin(), |
| 1248 | Ty->isVectorType() ? |
| 1249 | diag::err_invalid_conversion_between_vectors : |
| 1250 | diag::err_invalid_conversion_between_vector_and_integer, |
| 1251 | VectorTy.getAsString().c_str(), |
| 1252 | Ty.getAsString().c_str(), R); |
| 1253 | } else |
| 1254 | return Diag(R.getBegin(), |
| 1255 | diag::err_invalid_conversion_between_vector_and_scalar, |
| 1256 | VectorTy.getAsString().c_str(), |
| 1257 | Ty.getAsString().c_str(), R); |
| 1258 | |
| 1259 | return false; |
| 1260 | } |
| 1261 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1262 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1263 | ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1264 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1265 | assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1266 | |
| 1267 | Expr *castExpr = static_cast<Expr*>(Op); |
| 1268 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 1269 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1270 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
| 1271 | return true; |
Argiris Kirtzidis | c45e2fb | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1272 | return new ExplicitCastExpr(castType, castExpr, LParenLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1275 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 1276 | /// In that case, lex = cond. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1277 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
| 1278 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
| 1279 | UsualUnaryConversions(cond); |
| 1280 | UsualUnaryConversions(lex); |
| 1281 | UsualUnaryConversions(rex); |
| 1282 | QualType condT = cond->getType(); |
| 1283 | QualType lexT = lex->getType(); |
| 1284 | QualType rexT = rex->getType(); |
| 1285 | |
| 1286 | // first, check the condition. |
| 1287 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 1288 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 1289 | condT.getAsString()); |
| 1290 | return QualType(); |
| 1291 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Now check the two expressions. |
| 1294 | |
| 1295 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 1296 | // to find a common type: C99 6.5.15p3,5. |
| 1297 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1298 | UsualArithmeticConversions(lex, rex); |
| 1299 | return lex->getType(); |
| 1300 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1301 | |
| 1302 | // If both operands are the same structure or union type, the result is that |
| 1303 | // type. |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1304 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1305 | if (const RecordType *RHSRT = rexT->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1306 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1307 | // "If both the operands have structure or union type, the result has |
| 1308 | // that type." This implies that CV qualifiers are dropped. |
| 1309 | return lexT.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1310 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1311 | |
| 1312 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 1313 | // The following || allows only one side to be void (a GCC-ism). |
| 1314 | if (lexT->isVoidType() || rexT->isVoidType()) { |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 1315 | if (!lexT->isVoidType()) |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 1316 | Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void, |
| 1317 | rex->getSourceRange()); |
| 1318 | if (!rexT->isVoidType()) |
| 1319 | Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void, |
Nuno Lopes | 4ba41fd | 2008-06-04 19:14:12 +0000 | [diff] [blame] | 1320 | lex->getSourceRange()); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 1321 | ImpCastExprToType(lex, Context.VoidTy); |
| 1322 | ImpCastExprToType(rex, Context.VoidTy); |
| 1323 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 1324 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1325 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 1326 | // the type of the other operand." |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 1327 | if ((lexT->isPointerType() || lexT->isBlockPointerType() || |
| 1328 | Context.isObjCObjectPointerType(lexT)) && |
Steve Naroff | 3eac769 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1329 | rex->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1330 | ImpCastExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1331 | return lexT; |
| 1332 | } |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 1333 | if ((rexT->isPointerType() || rexT->isBlockPointerType() || |
| 1334 | Context.isObjCObjectPointerType(rexT)) && |
Steve Naroff | 3eac769 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1335 | lex->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1336 | ImpCastExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1337 | return rexT; |
| 1338 | } |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 1339 | // Handle the case where both operands are pointers before we handle null |
| 1340 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1341 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 1342 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 1343 | // get the "pointed to" types |
| 1344 | QualType lhptee = LHSPT->getPointeeType(); |
| 1345 | QualType rhptee = RHSPT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1347 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 1348 | if (lhptee->isVoidType() && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1349 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1350 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 1351 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1352 | QualType destType = Context.getPointerType(destPointee); |
| 1353 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 1354 | ImpCastExprToType(rex, destType); // promote to void* |
| 1355 | return destType; |
| 1356 | } |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1357 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1358 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1359 | QualType destType = Context.getPointerType(destPointee); |
| 1360 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 1361 | ImpCastExprToType(rex, destType); // promote to void* |
| 1362 | return destType; |
| 1363 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1364 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 1365 | QualType compositeType = lexT; |
| 1366 | |
| 1367 | // If either type is an Objective-C object type then check |
| 1368 | // compatibility according to Objective-C. |
| 1369 | if (Context.isObjCObjectPointerType(lexT) || |
| 1370 | Context.isObjCObjectPointerType(rexT)) { |
| 1371 | // If both operands are interfaces and either operand can be |
| 1372 | // assigned to the other, use that type as the composite |
| 1373 | // type. This allows |
| 1374 | // xxx ? (A*) a : (B*) b |
| 1375 | // where B is a subclass of A. |
| 1376 | // |
| 1377 | // Additionally, as for assignment, if either type is 'id' |
| 1378 | // allow silent coercion. Finally, if the types are |
| 1379 | // incompatible then make sure to use 'id' as the composite |
| 1380 | // type so the result is acceptable for sending messages to. |
| 1381 | |
| 1382 | // FIXME: This code should not be localized to here. Also this |
| 1383 | // should use a compatible check instead of abusing the |
| 1384 | // canAssignObjCInterfaces code. |
| 1385 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 1386 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 1387 | if (LHSIface && RHSIface && |
| 1388 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 1389 | compositeType = lexT; |
| 1390 | } else if (LHSIface && RHSIface && |
| 1391 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 1392 | compositeType = rexT; |
| 1393 | } else if (Context.isObjCIdType(lhptee) || |
| 1394 | Context.isObjCIdType(rhptee)) { |
| 1395 | // FIXME: This code looks wrong, because isObjCIdType checks |
| 1396 | // the struct but getObjCIdType returns the pointer to |
| 1397 | // struct. This is horrible and should be fixed. |
| 1398 | compositeType = Context.getObjCIdType(); |
| 1399 | } else { |
| 1400 | QualType incompatTy = Context.getObjCIdType(); |
| 1401 | ImpCastExprToType(lex, incompatTy); |
| 1402 | ImpCastExprToType(rex, incompatTy); |
| 1403 | return incompatTy; |
| 1404 | } |
| 1405 | } else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1406 | rhptee.getUnqualifiedType())) { |
Steve Naroff | 232324e | 2008-02-01 22:44:48 +0000 | [diff] [blame] | 1407 | Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers, |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1408 | lexT.getAsString(), rexT.getAsString(), |
| 1409 | lex->getSourceRange(), rex->getSourceRange()); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 1410 | // In this situation, we assume void* type. No especially good |
| 1411 | // reason, but this is what gcc does, and we do have to pick |
| 1412 | // to get a consistent AST. |
| 1413 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Daniel Dunbar | cd23bb2 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 1414 | ImpCastExprToType(lex, incompatTy); |
| 1415 | ImpCastExprToType(rex, incompatTy); |
| 1416 | return incompatTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1417 | } |
| 1418 | // The pointer types are compatible. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1419 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 1420 | // differently qualified versions of compatible types, the result type is |
| 1421 | // a pointer to an appropriately qualified version of the *composite* |
| 1422 | // type. |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 1423 | // FIXME: Need to calculate the composite type. |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1424 | // FIXME: Need to add qualifiers |
Eli Friedman | e38150e | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 1425 | ImpCastExprToType(lex, compositeType); |
| 1426 | ImpCastExprToType(rex, compositeType); |
| 1427 | return compositeType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1428 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1429 | } |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 1430 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 1431 | // evaluates to "struct objc_object *" (and is handled above when comparing |
| 1432 | // id with statically typed objects). |
| 1433 | if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) { |
| 1434 | // GCC allows qualified id and any Objective-C type to devolve to |
| 1435 | // id. Currently localizing to here until clear this should be |
| 1436 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 1437 | if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true) || |
| 1438 | (lexT->isObjCQualifiedIdType() && |
| 1439 | Context.isObjCObjectPointerType(rexT)) || |
| 1440 | (rexT->isObjCQualifiedIdType() && |
| 1441 | Context.isObjCObjectPointerType(lexT))) { |
| 1442 | // FIXME: This is not the correct composite type. This only |
| 1443 | // happens to work because id can more or less be used anywhere, |
| 1444 | // however this may change the type of method sends. |
| 1445 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 1446 | // (confusing) incompatible comparison warnings in some |
| 1447 | // cases. Investigate. |
| 1448 | QualType compositeType = Context.getObjCIdType(); |
| 1449 | ImpCastExprToType(lex, compositeType); |
| 1450 | ImpCastExprToType(rex, compositeType); |
| 1451 | return compositeType; |
| 1452 | } |
| 1453 | } |
| 1454 | |
Steve Naroff | 3eac769 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 1455 | // Selection between block pointer types is ok as long as they are the same. |
| 1456 | if (lexT->isBlockPointerType() && rexT->isBlockPointerType() && |
| 1457 | Context.getCanonicalType(lexT) == Context.getCanonicalType(rexT)) |
| 1458 | return lexT; |
| 1459 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1460 | // Otherwise, the operands are not compatible. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1461 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
| 1462 | lexT.getAsString(), rexT.getAsString(), |
| 1463 | lex->getSourceRange(), rex->getSourceRange()); |
| 1464 | return QualType(); |
| 1465 | } |
| 1466 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1467 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1468 | /// in the case of a the GNU conditional expr extension. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1469 | Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1470 | SourceLocation ColonLoc, |
| 1471 | ExprTy *Cond, ExprTy *LHS, |
| 1472 | ExprTy *RHS) { |
| 1473 | Expr *CondExpr = (Expr *) Cond; |
| 1474 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1475 | |
| 1476 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 1477 | // was the condition. |
| 1478 | bool isLHSNull = LHSExpr == 0; |
| 1479 | if (isLHSNull) |
| 1480 | LHSExpr = CondExpr; |
| 1481 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1482 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 1483 | RHSExpr, QuestionLoc); |
| 1484 | if (result.isNull()) |
| 1485 | return true; |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1486 | return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr, |
| 1487 | RHSExpr, result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1490 | |
| 1491 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 1492 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 1493 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 1494 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 1495 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1496 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1497 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 1498 | QualType lhptee, rhptee; |
| 1499 | |
| 1500 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1501 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 1502 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1503 | |
| 1504 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1505 | lhptee = Context.getCanonicalType(lhptee); |
| 1506 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1508 | AssignConvertType ConvTy = Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1509 | |
| 1510 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 1511 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 1512 | // qualifiers of the type *pointed to* by the right; |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1513 | // FIXME: Handle ASQualType |
| 1514 | if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) != |
| 1515 | rhptee.getCVRQualifiers()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1516 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1517 | |
| 1518 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 1519 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 1520 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1521 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1522 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1523 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1524 | |
| 1525 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1526 | assert(rhptee->isFunctionType()); |
| 1527 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1531 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1532 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1533 | |
| 1534 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1535 | assert(lhptee->isFunctionType()); |
| 1536 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1537 | } |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1538 | |
| 1539 | // Check for ObjC interfaces |
| 1540 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 1541 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 1542 | if (LHSIface && RHSIface && |
| 1543 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 1544 | return ConvTy; |
| 1545 | |
| 1546 | // ID acts sort of like void* for ObjC interfaces |
| 1547 | if (LHSIface && Context.isObjCIdType(rhptee)) |
| 1548 | return ConvTy; |
| 1549 | if (RHSIface && Context.isObjCIdType(lhptee)) |
| 1550 | return ConvTy; |
| 1551 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1552 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 1553 | // unqualified versions of compatible types, ... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1554 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1555 | rhptee.getUnqualifiedType())) |
| 1556 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1557 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1560 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 1561 | /// block pointer types are compatible or whether a block and normal pointer |
| 1562 | /// are compatible. It is more restrict than comparing two function pointer |
| 1563 | // types. |
| 1564 | Sema::AssignConvertType |
| 1565 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
| 1566 | QualType rhsType) { |
| 1567 | QualType lhptee, rhptee; |
| 1568 | |
| 1569 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 1570 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
| 1571 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 1572 | |
| 1573 | // make sure we operate on the canonical type |
| 1574 | lhptee = Context.getCanonicalType(lhptee); |
| 1575 | rhptee = Context.getCanonicalType(rhptee); |
| 1576 | |
| 1577 | AssignConvertType ConvTy = Compatible; |
| 1578 | |
| 1579 | // For blocks we enforce that qualifiers are identical. |
| 1580 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 1581 | ConvTy = CompatiblePointerDiscardsQualifiers; |
| 1582 | |
| 1583 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
| 1584 | return IncompatibleBlockPointer; |
| 1585 | return ConvTy; |
| 1586 | } |
| 1587 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1588 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 1589 | /// has code to accommodate several GCC extensions when type checking |
| 1590 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 1591 | /// |
| 1592 | /// int a, *pint; |
| 1593 | /// short *pshort; |
| 1594 | /// struct foo *pfoo; |
| 1595 | /// |
| 1596 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 1597 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 1598 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 1599 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 1600 | /// |
| 1601 | /// As a result, the code for dealing with pointers is more complex than the |
| 1602 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1603 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1604 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1605 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1606 | // Get canonical types. We're not formatting these types, just comparing |
| 1607 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1608 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 1609 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1610 | |
| 1611 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 1612 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1613 | |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1614 | if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
Chris Lattner | e1577e2 | 2008-04-07 06:52:53 +0000 | [diff] [blame] | 1615 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1616 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1617 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1618 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1620 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 1621 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1622 | return Compatible; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 1623 | // Relax integer conversions like we do for pointers below. |
| 1624 | if (rhsType->isIntegerType()) |
| 1625 | return IntToPointer; |
| 1626 | if (lhsType->isIntegerType()) |
| 1627 | return PointerToInt; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1628 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1629 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1630 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1631 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1632 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1633 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 1634 | if (LV->getElementType() == rhsType) |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1635 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1636 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1637 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 1638 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 1639 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1640 | if (getLangOptions().LaxVectorConversions && |
| 1641 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1642 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
| 1643 | return Compatible; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1644 | } |
| 1645 | return Incompatible; |
| 1646 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1648 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1649 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1651 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1652 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1653 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1655 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1656 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1657 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 1658 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1659 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1660 | return BlockVoidPointer; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Treat block pointers as objects. |
| 1663 | if (getLangOptions().ObjC1 && |
| 1664 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 1665 | return Compatible; |
| 1666 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1667 | return Incompatible; |
| 1668 | } |
| 1669 | |
| 1670 | if (isa<BlockPointerType>(lhsType)) { |
| 1671 | if (rhsType->isIntegerType()) |
| 1672 | return IntToPointer; |
| 1673 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 1674 | // Treat block pointers as objects. |
| 1675 | if (getLangOptions().ObjC1 && |
| 1676 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 1677 | return Compatible; |
| 1678 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1679 | if (rhsType->isBlockPointerType()) |
| 1680 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
| 1681 | |
| 1682 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 1683 | if (RHSPT->getPointeeType()->isVoidType()) |
| 1684 | return BlockVoidPointer; |
| 1685 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1686 | return Incompatible; |
| 1687 | } |
| 1688 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1689 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1690 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1691 | if (lhsType == Context.BoolTy) |
| 1692 | return Compatible; |
| 1693 | |
| 1694 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1695 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1697 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1698 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (isa<BlockPointerType>(lhsType) && |
| 1701 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
| 1702 | return BlockVoidPointer; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1703 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1704 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1706 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1707 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1708 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1709 | } |
| 1710 | return Incompatible; |
| 1711 | } |
| 1712 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1713 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1714 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1715 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 1716 | // a null pointer constant. |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 1717 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType() || |
| 1718 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 1719 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1720 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1721 | return Compatible; |
| 1722 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 1723 | |
| 1724 | // We don't allow conversion of non-null-pointer constants to integers. |
| 1725 | if (lhsType->isBlockPointerType() && rExpr->getType()->isIntegerType()) |
| 1726 | return IntToBlockPointer; |
| 1727 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1728 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1729 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1730 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1731 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1732 | // |
| 1733 | // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references |
| 1734 | // are better understood. |
| 1735 | if (!lhsType->isReferenceType()) |
| 1736 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1738 | Sema::AssignConvertType result = |
| 1739 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1740 | |
| 1741 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 1742 | // type of the assignment expression. |
| 1743 | if (rExpr->getType() != lhsType) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1744 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1745 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1748 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1749 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 1750 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 1751 | } |
| 1752 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1753 | QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1754 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 1755 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1756 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1757 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 1761 | Expr *&rex) { |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 1762 | // For conversion purposes, we ignore any qualifiers. |
| 1763 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1764 | QualType lhsType = |
| 1765 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 1766 | QualType rhsType = |
| 1767 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1768 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1769 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 1770 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1771 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1772 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1773 | // Handle the case of a vector & extvector type of the same size and element |
| 1774 | // type. It would be nice if we only had one vector type someday. |
| 1775 | if (getLangOptions().LaxVectorConversions) |
| 1776 | if (const VectorType *LV = lhsType->getAsVectorType()) |
| 1777 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 1778 | if (LV->getElementType() == RV->getElementType() && |
| 1779 | LV->getNumElements() == RV->getNumElements()) |
| 1780 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
| 1781 | |
| 1782 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 1783 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1784 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1785 | QualType eltType = V->getElementType(); |
| 1786 | |
| 1787 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
| 1788 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 1789 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1790 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1791 | return lhsType; |
| 1792 | } |
| 1793 | } |
| 1794 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1795 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1796 | // promote the lhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1797 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1798 | QualType eltType = V->getElementType(); |
| 1799 | |
| 1800 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
| 1801 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 1802 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1803 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1804 | return rhsType; |
| 1805 | } |
| 1806 | } |
| 1807 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1808 | // You cannot convert between vector values of different size. |
| 1809 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 1810 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1811 | lex->getSourceRange(), rex->getSourceRange()); |
| 1812 | return QualType(); |
| 1813 | } |
| 1814 | |
| 1815 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1816 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1817 | { |
| 1818 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1819 | |
| 1820 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
| 1821 | return CheckVectorOperands(loc, lex, rex); |
| 1822 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1823 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1825 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1826 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1827 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1831 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1832 | { |
| 1833 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1834 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1835 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1837 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1838 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1839 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1843 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1844 | { |
| 1845 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1846 | return CheckVectorOperands(loc, lex, rex); |
| 1847 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1848 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1850 | // handle the common case first (both operands are arithmetic). |
| 1851 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1852 | return compType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1853 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 1854 | // Put any potential pointer into PExp |
| 1855 | Expr* PExp = lex, *IExp = rex; |
| 1856 | if (IExp->getType()->isPointerType()) |
| 1857 | std::swap(PExp, IExp); |
| 1858 | |
| 1859 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 1860 | if (IExp->getType()->isIntegerType()) { |
| 1861 | // Check for arithmetic on pointers to incomplete types |
| 1862 | if (!PTy->getPointeeType()->isObjectType()) { |
| 1863 | if (PTy->getPointeeType()->isVoidType()) { |
| 1864 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1865 | lex->getSourceRange(), rex->getSourceRange()); |
| 1866 | } else { |
| 1867 | Diag(loc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1868 | lex->getType().getAsString(), lex->getSourceRange()); |
| 1869 | return QualType(); |
| 1870 | } |
| 1871 | } |
| 1872 | return PExp->getType(); |
| 1873 | } |
| 1874 | } |
| 1875 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1876 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1879 | // C99 6.5.6 |
| 1880 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
| 1881 | SourceLocation loc, bool isCompAssign) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1882 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1883 | return CheckVectorOperands(loc, lex, rex); |
| 1884 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1885 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1887 | // Enforce type constraints: C99 6.5.6p3. |
| 1888 | |
| 1889 | // Handle the common case first (both operands are arithmetic). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1890 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1891 | return compType; |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Either ptr - int or ptr - ptr. |
| 1894 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1895 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1897 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1898 | if (!lpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1899 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1900 | if (lpointee->isVoidType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1901 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1902 | lex->getSourceRange(), rex->getSourceRange()); |
| 1903 | } else { |
| 1904 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1905 | lex->getType().getAsString(), lex->getSourceRange()); |
| 1906 | return QualType(); |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | // The result type of a pointer-int computation is the pointer type. |
| 1911 | if (rex->getType()->isIntegerType()) |
| 1912 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1914 | // Handle pointer-pointer subtractions. |
| 1915 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1916 | QualType rpointee = RHSPTy->getPointeeType(); |
| 1917 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1918 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1919 | if (!rpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1920 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1921 | if (rpointee->isVoidType()) { |
| 1922 | if (!lpointee->isVoidType()) |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1923 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1924 | lex->getSourceRange(), rex->getSourceRange()); |
| 1925 | } else { |
| 1926 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1927 | rex->getType().getAsString(), rex->getSourceRange()); |
| 1928 | return QualType(); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | // Pointee types must be compatible. |
Eli Friedman | 583c31e | 2008-09-02 05:09:35 +0000 | [diff] [blame] | 1933 | if (!Context.typesAreCompatible( |
| 1934 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 1935 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1936 | Diag(loc, diag::err_typecheck_sub_ptr_compatible, |
| 1937 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1938 | lex->getSourceRange(), rex->getSourceRange()); |
| 1939 | return QualType(); |
| 1940 | } |
| 1941 | |
| 1942 | return Context.getPointerDiffType(); |
| 1943 | } |
| 1944 | } |
| 1945 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1946 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1949 | // C99 6.5.7 |
| 1950 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc, |
| 1951 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1952 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 1953 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 1954 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1956 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 1957 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 1958 | if (!isCompAssign) |
| 1959 | UsualUnaryConversions(lex); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1960 | UsualUnaryConversions(rex); |
| 1961 | |
| 1962 | // "The type of the result is that of the promoted left operand." |
| 1963 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1966 | static bool areComparableObjCInterfaces(QualType LHS, QualType RHS, |
| 1967 | ASTContext& Context) { |
| 1968 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 1969 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 1970 | // ID acts sort of like void* for ObjC interfaces |
| 1971 | if (LHSIface && Context.isObjCIdType(RHS)) |
| 1972 | return true; |
| 1973 | if (RHSIface && Context.isObjCIdType(LHS)) |
| 1974 | return true; |
| 1975 | if (!LHSIface || !RHSIface) |
| 1976 | return false; |
| 1977 | return Context.canAssignObjCInterfaces(LHSIface, RHSIface) || |
| 1978 | Context.canAssignObjCInterfaces(RHSIface, LHSIface); |
| 1979 | } |
| 1980 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1981 | // C99 6.5.8 |
| 1982 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc, |
| 1983 | bool isRelational) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1984 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1985 | return CheckVectorCompareOperands(lex, rex, loc, isRelational); |
| 1986 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1987 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1988 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1989 | UsualArithmeticConversions(lex, rex); |
| 1990 | else { |
| 1991 | UsualUnaryConversions(lex); |
| 1992 | UsualUnaryConversions(rex); |
| 1993 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1994 | QualType lType = lex->getType(); |
| 1995 | QualType rType = rex->getType(); |
| 1996 | |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1997 | // For non-floating point types, check for self-comparisons of the form |
| 1998 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 1999 | // often indicate logic errors in the program. |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2000 | if (!lType->isFloatingType()) { |
Ted Kremenek | 87e30c5 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2001 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 2002 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 2003 | if (DRL->getDecl() == DRR->getDecl()) |
| 2004 | Diag(loc, diag::warn_selfcomparison); |
| 2005 | } |
| 2006 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2007 | if (isRelational) { |
| 2008 | if (lType->isRealType() && rType->isRealType()) |
| 2009 | return Context.IntTy; |
| 2010 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2011 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 2012 | if (lType->isFloatingType()) { |
| 2013 | assert (rType->isFloatingType()); |
Ted Kremenek | 30c6675 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2014 | CheckFloatComparison(loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2017 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 2018 | return Context.IntTy; |
| 2019 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2021 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 2022 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 2023 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2024 | // All of the following pointer related warnings are GCC extensions, except |
| 2025 | // when handling null pointer constants. One day, we can consider making them |
| 2026 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 2027 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2028 | QualType LCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2029 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2030 | QualType RCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2031 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 2032 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 2033 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 2034 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 2035 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2036 | RCanPointeeTy.getUnqualifiedType()) && |
| 2037 | !areComparableObjCInterfaces(LCanPointeeTy, RCanPointeeTy, Context)) { |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2038 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 2039 | lType.getAsString(), rType.getAsString(), |
| 2040 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2041 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2042 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2043 | return Context.IntTy; |
| 2044 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2045 | // Handle block pointer types. |
| 2046 | if (lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| 2047 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 2048 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
| 2049 | |
| 2050 | if (!LHSIsNull && !RHSIsNull && |
| 2051 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
| 2052 | Diag(loc, diag::err_typecheck_comparison_of_distinct_blocks, |
| 2053 | lType.getAsString(), rType.getAsString(), |
| 2054 | lex->getSourceRange(), rex->getSourceRange()); |
| 2055 | } |
| 2056 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
| 2057 | return Context.IntTy; |
| 2058 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 2059 | // Allow block pointers to be compared with null pointer constants. |
| 2060 | if ((lType->isBlockPointerType() && rType->isPointerType()) || |
| 2061 | (lType->isPointerType() && rType->isBlockPointerType())) { |
| 2062 | if (!LHSIsNull && !RHSIsNull) { |
| 2063 | Diag(loc, diag::err_typecheck_comparison_of_distinct_blocks, |
| 2064 | lType.getAsString(), rType.getAsString(), |
| 2065 | lex->getSourceRange(), rex->getSourceRange()); |
| 2066 | } |
| 2067 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
| 2068 | return Context.IntTy; |
| 2069 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 2070 | |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2071 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
| 2072 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 2073 | ImpCastExprToType(rex, lType); |
| 2074 | return Context.IntTy; |
| 2075 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 2076 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2077 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
| 2078 | rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2079 | if (!RHSIsNull) |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2080 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 2081 | lType.getAsString(), rType.getAsString(), |
| 2082 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2083 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2084 | return Context.IntTy; |
| 2085 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 2086 | if (lType->isIntegerType() && |
| 2087 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 2088 | if (!LHSIsNull) |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2089 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 2090 | lType.getAsString(), rType.getAsString(), |
| 2091 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 2092 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 2093 | return Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2094 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 2095 | // Handle block pointers. |
| 2096 | if (lType->isBlockPointerType() && rType->isIntegerType()) { |
| 2097 | if (!RHSIsNull) |
| 2098 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 2099 | lType.getAsString(), rType.getAsString(), |
| 2100 | lex->getSourceRange(), rex->getSourceRange()); |
| 2101 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
| 2102 | return Context.IntTy; |
| 2103 | } |
| 2104 | if (lType->isIntegerType() && rType->isBlockPointerType()) { |
| 2105 | if (!LHSIsNull) |
| 2106 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 2107 | lType.getAsString(), rType.getAsString(), |
| 2108 | lex->getSourceRange(), rex->getSourceRange()); |
| 2109 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
| 2110 | return Context.IntTy; |
| 2111 | } |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2112 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 2115 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| 2116 | /// operates on extended vector types. Instead of producing an IntTy result, |
| 2117 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 2118 | /// types. |
| 2119 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
| 2120 | SourceLocation loc, |
| 2121 | bool isRelational) { |
| 2122 | // Check to make sure we're operating on vectors of the same type and width, |
| 2123 | // Allowing one side to be a scalar of element type. |
| 2124 | QualType vType = CheckVectorOperands(loc, lex, rex); |
| 2125 | if (vType.isNull()) |
| 2126 | return vType; |
| 2127 | |
| 2128 | QualType lType = lex->getType(); |
| 2129 | QualType rType = rex->getType(); |
| 2130 | |
| 2131 | // For non-floating point types, check for self-comparisons of the form |
| 2132 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 2133 | // often indicate logic errors in the program. |
| 2134 | if (!lType->isFloatingType()) { |
| 2135 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 2136 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 2137 | if (DRL->getDecl() == DRR->getDecl()) |
| 2138 | Diag(loc, diag::warn_selfcomparison); |
| 2139 | } |
| 2140 | |
| 2141 | // Check for comparisons of floating point operands using != and ==. |
| 2142 | if (!isRelational && lType->isFloatingType()) { |
| 2143 | assert (rType->isFloatingType()); |
| 2144 | CheckFloatComparison(loc,lex,rex); |
| 2145 | } |
| 2146 | |
| 2147 | // Return the type for the comparison, which is the same as vector type for |
| 2148 | // integer vectors, or an integer type of identical size and number of |
| 2149 | // elements for floating point vectors. |
| 2150 | if (lType->isIntegerType()) |
| 2151 | return lType; |
| 2152 | |
| 2153 | const VectorType *VTy = lType->getAsVectorType(); |
| 2154 | |
| 2155 | // FIXME: need to deal with non-32b int / non-64b long long |
| 2156 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 2157 | if (TypeSize == 32) { |
| 2158 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 2159 | } |
| 2160 | assert(TypeSize == 64 && "Unhandled vector element size in vector compare"); |
| 2161 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 2162 | } |
| 2163 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2164 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2165 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2166 | { |
| 2167 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 2168 | return CheckVectorOperands(loc, lex, rex); |
| 2169 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2170 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2171 | |
| 2172 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2173 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2174 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
| 2178 | Expr *&lex, Expr *&rex, SourceLocation loc) |
| 2179 | { |
| 2180 | UsualUnaryConversions(lex); |
| 2181 | UsualUnaryConversions(rex); |
| 2182 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 2183 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2184 | return Context.IntTy; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 2185 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 2189 | Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2190 | { |
| 2191 | QualType lhsType = lex->getType(); |
| 2192 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 2193 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(Context); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2194 | |
| 2195 | switch (mlval) { // C99 6.5.16p2 |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2196 | case Expr::MLV_Valid: |
| 2197 | break; |
| 2198 | case Expr::MLV_ConstQualified: |
| 2199 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 2200 | return QualType(); |
| 2201 | case Expr::MLV_ArrayType: |
| 2202 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 2203 | lhsType.getAsString(), lex->getSourceRange()); |
| 2204 | return QualType(); |
| 2205 | case Expr::MLV_NotObjectType: |
| 2206 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 2207 | lhsType.getAsString(), lex->getSourceRange()); |
| 2208 | return QualType(); |
| 2209 | case Expr::MLV_InvalidExpression: |
| 2210 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 2211 | lex->getSourceRange()); |
| 2212 | return QualType(); |
| 2213 | case Expr::MLV_IncompleteType: |
| 2214 | case Expr::MLV_IncompleteVoidType: |
| 2215 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 2216 | lhsType.getAsString(), lex->getSourceRange()); |
| 2217 | return QualType(); |
| 2218 | case Expr::MLV_DuplicateVectorComponents: |
| 2219 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 2220 | lex->getSourceRange()); |
| 2221 | return QualType(); |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 2222 | case Expr::MLV_NotBlockQualified: |
| 2223 | Diag(loc, diag::err_block_decl_ref_not_modifiable_lvalue, |
| 2224 | lex->getSourceRange()); |
| 2225 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2226 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2227 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2228 | AssignConvertType ConvTy; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 2229 | if (compoundType.isNull()) { |
| 2230 | // Simple assignment "x = y". |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2231 | ConvTy = CheckSingleAssignmentConstraints(lhsType, rex); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 2232 | |
| 2233 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 2234 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 2235 | // instead of "x += 4". |
| 2236 | Expr *RHSCheck = rex; |
| 2237 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 2238 | RHSCheck = ICE->getSubExpr(); |
| 2239 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 2240 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 2241 | UO->getOpcode() == UnaryOperator::Minus) && |
| 2242 | loc.isFileID() && UO->getOperatorLoc().isFileID() && |
| 2243 | // Only if the two operators are exactly adjacent. |
| 2244 | loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
| 2245 | Diag(loc, diag::warn_not_compound_assign, |
| 2246 | UO->getOpcode() == UnaryOperator::Plus ? "+" : "-", |
| 2247 | SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc())); |
| 2248 | } |
| 2249 | } else { |
| 2250 | // Compound assignment "x += y" |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2251 | ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 2252 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2253 | |
| 2254 | if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType, |
| 2255 | rex, "assigning")) |
| 2256 | return QualType(); |
| 2257 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2258 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 2259 | // left operand unless the left operand has qualified type, in which case |
| 2260 | // it is the unqualified version of the type of the left operand. |
| 2261 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 2262 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2263 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 2264 | // oprdu. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2265 | return lhsType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
| 2269 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
| 2272 | DefaultFunctionArrayConversion(rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2273 | return rex->getType(); |
| 2274 | } |
| 2275 | |
| 2276 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 2277 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
| 2278 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
| 2279 | QualType resType = op->getType(); |
| 2280 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
| 2281 | |
Steve Naroff | d30e193 | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 2282 | // C99 6.5.2.4p1: We allow complex as a GCC extension. |
Steve Naroff | ce82758 | 2007-11-11 14:15:57 +0000 | [diff] [blame] | 2283 | if (const PointerType *pt = resType->getAsPointerType()) { |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2284 | if (pt->getPointeeType()->isVoidType()) { |
| 2285 | Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange()); |
| 2286 | } else if (!pt->getPointeeType()->isObjectType()) { |
| 2287 | // C99 6.5.2.4p2, 6.5.6p2 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2288 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 2289 | resType.getAsString(), op->getSourceRange()); |
| 2290 | return QualType(); |
| 2291 | } |
Steve Naroff | d30e193 | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 2292 | } else if (!resType->isRealType()) { |
| 2293 | if (resType->isComplexType()) |
| 2294 | // C99 does not support ++/-- on complex types. |
| 2295 | Diag(OpLoc, diag::ext_integer_increment_complex, |
| 2296 | resType.getAsString(), op->getSourceRange()); |
| 2297 | else { |
| 2298 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 2299 | resType.getAsString(), op->getSourceRange()); |
| 2300 | return QualType(); |
| 2301 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2302 | } |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 2303 | // At this point, we know we have a real, complex or pointer type. |
| 2304 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 2305 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(Context); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2306 | if (mlval != Expr::MLV_Valid) { |
| 2307 | // FIXME: emit a more precise diagnostic... |
| 2308 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
| 2309 | op->getSourceRange()); |
| 2310 | return QualType(); |
| 2311 | } |
| 2312 | return resType; |
| 2313 | } |
| 2314 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2315 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2316 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2317 | /// where the declaration is needed for type checking. We only need to |
| 2318 | /// handle cases when the expression references a function designator |
| 2319 | /// or is an lvalue. Here are some examples: |
| 2320 | /// - &(x) => x |
| 2321 | /// - &*****f => f for f a function designator. |
| 2322 | /// - &s.xx => s |
| 2323 | /// - &s.zz[1].yy -> s, if zz is an array |
| 2324 | /// - *(x + 1) -> x, if x is an array |
| 2325 | /// - &"123"[2] -> 0 |
| 2326 | /// - & __real__ x -> x |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2327 | static ValueDecl *getPrimaryDecl(Expr *E) { |
| 2328 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2329 | case Stmt::DeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2330 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2331 | case Stmt::MemberExprClass: |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2332 | // Fields cannot be declared with a 'register' storage class. |
| 2333 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2334 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2335 | return 0; |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2336 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2337 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2338 | // &X[4] and &4[X] refers to X if X is not a pointer. |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2340 | ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Anders Carlsson | 655694e | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 2341 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2342 | return 0; |
| 2343 | else |
| 2344 | return VD; |
| 2345 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2346 | case Stmt::UnaryOperatorClass: { |
| 2347 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| 2348 | |
| 2349 | switch(UO->getOpcode()) { |
| 2350 | case UnaryOperator::Deref: { |
| 2351 | // *(X + 1) refers to X if X is not a pointer. |
| 2352 | ValueDecl *VD = getPrimaryDecl(UO->getSubExpr()); |
| 2353 | if (!VD || VD->getType()->isPointerType()) |
| 2354 | return 0; |
| 2355 | return VD; |
| 2356 | } |
| 2357 | case UnaryOperator::Real: |
| 2358 | case UnaryOperator::Imag: |
| 2359 | case UnaryOperator::Extension: |
| 2360 | return getPrimaryDecl(UO->getSubExpr()); |
| 2361 | default: |
| 2362 | return 0; |
| 2363 | } |
| 2364 | } |
| 2365 | case Stmt::BinaryOperatorClass: { |
| 2366 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 2367 | |
| 2368 | // Handle cases involving pointer arithmetic. The result of an |
| 2369 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 2370 | |
| 2371 | // (x + n) or (n + x) => x |
| 2372 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 2373 | if (BO->getLHS()->getType()->isPointerType()) { |
| 2374 | return getPrimaryDecl(BO->getLHS()); |
| 2375 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 2376 | return getPrimaryDecl(BO->getRHS()); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | return 0; |
| 2381 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2382 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2383 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2384 | case Stmt::ImplicitCastExprClass: |
| 2385 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2386 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2387 | default: |
| 2388 | return 0; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 2393 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 2394 | /// object cannot be declared with storage class register or be a bit field. |
| 2395 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 2396 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| 2397 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 2398 | if (getLangOptions().C99) { |
| 2399 | // Implement C99-only parts of addressof rules. |
| 2400 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 2401 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 2402 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 2403 | // (assuming the deref expression is valid). |
| 2404 | return uOp->getSubExpr()->getType(); |
| 2405 | } |
| 2406 | // Technically, there should be a check for array subscript |
| 2407 | // expressions here, but the result of one is always an lvalue anyway. |
| 2408 | } |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2409 | ValueDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 2410 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2411 | |
| 2412 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2413 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 2414 | // FIXME: emit more specific diag... |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2415 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 2416 | op->getSourceRange()); |
| 2417 | return QualType(); |
| 2418 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 2419 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
| 2420 | if (MemExpr->getMemberDecl()->isBitField()) { |
| 2421 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2422 | std::string("bit-field"), op->getSourceRange()); |
| 2423 | return QualType(); |
| 2424 | } |
| 2425 | // Check for Apple extension for accessing vector components. |
| 2426 | } else if (isa<ArraySubscriptExpr>(op) && |
| 2427 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) { |
| 2428 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2429 | std::string("vector"), op->getSourceRange()); |
| 2430 | return QualType(); |
| 2431 | } else if (dcl) { // C99 6.5.3.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2432 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 2433 | // with the register storage-class specifier. |
| 2434 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 2435 | if (vd->getStorageClass() == VarDecl::Register) { |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 2436 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2437 | std::string("register variable"), op->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2438 | return QualType(); |
| 2439 | } |
| 2440 | } else |
| 2441 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2442 | } |
Chris Lattner | a55e321 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2444 | // If the operand has type "type", the result has type "pointer to type". |
| 2445 | return Context.getPointerType(op->getType()); |
| 2446 | } |
| 2447 | |
| 2448 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
| 2449 | UsualUnaryConversions(op); |
| 2450 | QualType qType = op->getType(); |
| 2451 | |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 2452 | if (const PointerType *PT = qType->getAsPointerType()) { |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 2453 | // Note that per both C89 and C99, this is always legal, even |
| 2454 | // if ptype is an incomplete type or void. |
| 2455 | // It would be possible to warn about dereferencing a |
| 2456 | // void pointer, but it's completely well-defined, |
| 2457 | // and such a warning is unlikely to catch any mistakes. |
| 2458 | return PT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2459 | } |
| 2460 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
| 2461 | qType.getAsString(), op->getSourceRange()); |
| 2462 | return QualType(); |
| 2463 | } |
| 2464 | |
| 2465 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 2466 | tok::TokenKind Kind) { |
| 2467 | BinaryOperator::Opcode Opc; |
| 2468 | switch (Kind) { |
| 2469 | default: assert(0 && "Unknown binop!"); |
| 2470 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 2471 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 2472 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 2473 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 2474 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 2475 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 2476 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 2477 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 2478 | case tok::less: Opc = BinaryOperator::LT; break; |
| 2479 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 2480 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 2481 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 2482 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 2483 | case tok::amp: Opc = BinaryOperator::And; break; |
| 2484 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 2485 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 2486 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 2487 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 2488 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 2489 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 2490 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 2491 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 2492 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 2493 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 2494 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 2495 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 2496 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 2497 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 2498 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 2499 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 2500 | } |
| 2501 | return Opc; |
| 2502 | } |
| 2503 | |
| 2504 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 2505 | tok::TokenKind Kind) { |
| 2506 | UnaryOperator::Opcode Opc; |
| 2507 | switch (Kind) { |
| 2508 | default: assert(0 && "Unknown unary op!"); |
| 2509 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 2510 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 2511 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 2512 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 2513 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 2514 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 2515 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 2516 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 2517 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 2518 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 2519 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 2520 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 2521 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 2522 | } |
| 2523 | return Opc; |
| 2524 | } |
| 2525 | |
| 2526 | // Binary Operators. 'Tok' is the token for the operator. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2527 | Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2528 | ExprTy *LHS, ExprTy *RHS) { |
| 2529 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 2530 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 2531 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2532 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 2533 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2534 | |
| 2535 | QualType ResultTy; // Result type of the binary operator. |
| 2536 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 2537 | |
| 2538 | switch (Opc) { |
| 2539 | default: |
| 2540 | assert(0 && "Unknown binary expr!"); |
| 2541 | case BinaryOperator::Assign: |
| 2542 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
| 2543 | break; |
| 2544 | case BinaryOperator::Mul: |
| 2545 | case BinaryOperator::Div: |
| 2546 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 2547 | break; |
| 2548 | case BinaryOperator::Rem: |
| 2549 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 2550 | break; |
| 2551 | case BinaryOperator::Add: |
| 2552 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 2553 | break; |
| 2554 | case BinaryOperator::Sub: |
| 2555 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 2556 | break; |
| 2557 | case BinaryOperator::Shl: |
| 2558 | case BinaryOperator::Shr: |
| 2559 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 2560 | break; |
| 2561 | case BinaryOperator::LE: |
| 2562 | case BinaryOperator::LT: |
| 2563 | case BinaryOperator::GE: |
| 2564 | case BinaryOperator::GT: |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2565 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2566 | break; |
| 2567 | case BinaryOperator::EQ: |
| 2568 | case BinaryOperator::NE: |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2569 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | case BinaryOperator::And: |
| 2572 | case BinaryOperator::Xor: |
| 2573 | case BinaryOperator::Or: |
| 2574 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 2575 | break; |
| 2576 | case BinaryOperator::LAnd: |
| 2577 | case BinaryOperator::LOr: |
| 2578 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
| 2579 | break; |
| 2580 | case BinaryOperator::MulAssign: |
| 2581 | case BinaryOperator::DivAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2582 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2583 | if (!CompTy.isNull()) |
| 2584 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2585 | break; |
| 2586 | case BinaryOperator::RemAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2587 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2588 | if (!CompTy.isNull()) |
| 2589 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2590 | break; |
| 2591 | case BinaryOperator::AddAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2592 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2593 | if (!CompTy.isNull()) |
| 2594 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2595 | break; |
| 2596 | case BinaryOperator::SubAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2597 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2598 | if (!CompTy.isNull()) |
| 2599 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2600 | break; |
| 2601 | case BinaryOperator::ShlAssign: |
| 2602 | case BinaryOperator::ShrAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2603 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2604 | if (!CompTy.isNull()) |
| 2605 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2606 | break; |
| 2607 | case BinaryOperator::AndAssign: |
| 2608 | case BinaryOperator::XorAssign: |
| 2609 | case BinaryOperator::OrAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2610 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2611 | if (!CompTy.isNull()) |
| 2612 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2613 | break; |
| 2614 | case BinaryOperator::Comma: |
| 2615 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
| 2616 | break; |
| 2617 | } |
| 2618 | if (ResultTy.isNull()) |
| 2619 | return true; |
| 2620 | if (CompTy.isNull()) |
Chris Lattner | f420df1 | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 2621 | return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2622 | else |
Chris Lattner | f420df1 | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 2623 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | // Unary Operators. 'Tok' is the token for the operator. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2627 | Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2628 | ExprTy *input) { |
| 2629 | Expr *Input = (Expr*)input; |
| 2630 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 2631 | QualType resultType; |
| 2632 | switch (Opc) { |
| 2633 | default: |
| 2634 | assert(0 && "Unimplemented unary expr!"); |
| 2635 | case UnaryOperator::PreInc: |
| 2636 | case UnaryOperator::PreDec: |
| 2637 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
| 2638 | break; |
| 2639 | case UnaryOperator::AddrOf: |
| 2640 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 2641 | break; |
| 2642 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 2643 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2644 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 2645 | break; |
| 2646 | case UnaryOperator::Plus: |
| 2647 | case UnaryOperator::Minus: |
| 2648 | UsualUnaryConversions(Input); |
| 2649 | resultType = Input->getType(); |
| 2650 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 2651 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2652 | resultType.getAsString()); |
| 2653 | break; |
| 2654 | case UnaryOperator::Not: // bitwise complement |
| 2655 | UsualUnaryConversions(Input); |
| 2656 | resultType = Input->getType(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 2657 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 2658 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 2659 | // C99 does not support '~' for complex conjugation. |
| 2660 | Diag(OpLoc, diag::ext_integer_complement_complex, |
| 2661 | resultType.getAsString(), Input->getSourceRange()); |
| 2662 | else if (!resultType->isIntegerType()) |
| 2663 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2664 | resultType.getAsString(), Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2665 | break; |
| 2666 | case UnaryOperator::LNot: // logical negation |
| 2667 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 2668 | DefaultFunctionArrayConversion(Input); |
| 2669 | resultType = Input->getType(); |
| 2670 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 2671 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2672 | resultType.getAsString()); |
| 2673 | // LNot always has type int. C99 6.5.3.3p5. |
| 2674 | resultType = Context.IntTy; |
| 2675 | break; |
| 2676 | case UnaryOperator::SizeOf: |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 2677 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, |
| 2678 | Input->getSourceRange(), true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2679 | break; |
| 2680 | case UnaryOperator::AlignOf: |
Chris Lattner | f814d88 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 2681 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, |
| 2682 | Input->getSourceRange(), false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2683 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2684 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2685 | case UnaryOperator::Imag: |
Chris Lattner | 5110ad5 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 2686 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2687 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2688 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2689 | resultType = Input->getType(); |
| 2690 | break; |
| 2691 | } |
| 2692 | if (resultType.isNull()) |
| 2693 | return true; |
| 2694 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
| 2695 | } |
| 2696 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2697 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 2698 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2699 | SourceLocation LabLoc, |
| 2700 | IdentifierInfo *LabelII) { |
| 2701 | // Look up the record for this label identifier. |
| 2702 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 2703 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2704 | // If we haven't seen this label yet, create a forward reference. It |
| 2705 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2706 | if (LabelDecl == 0) |
| 2707 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 2708 | |
| 2709 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 2710 | return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 2711 | Context.getPointerType(Context.VoidTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2714 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2715 | SourceLocation RPLoc) { // "({..})" |
| 2716 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 2717 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 2718 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 2719 | |
| 2720 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 2721 | // example, it is not possible to goto into a stmt expression apparently. |
| 2722 | // More semantic analysis is needed. |
| 2723 | |
| 2724 | // FIXME: the last statement in the compount stmt has its value used. We |
| 2725 | // should not warn about it being unused. |
| 2726 | |
| 2727 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 2728 | // as the type of the stmtexpr. |
| 2729 | QualType Ty = Context.VoidTy; |
| 2730 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2731 | if (!Compound->body_empty()) { |
| 2732 | Stmt *LastStmt = Compound->body_back(); |
| 2733 | // If LastStmt is a label, skip down through into the body. |
| 2734 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 2735 | LastStmt = Label->getSubStmt(); |
| 2736 | |
| 2737 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2738 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2739 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2740 | |
| 2741 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 2742 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2743 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2744 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc, |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2745 | SourceLocation TypeLoc, |
| 2746 | TypeTy *argty, |
| 2747 | OffsetOfComponent *CompPtr, |
| 2748 | unsigned NumComponents, |
| 2749 | SourceLocation RPLoc) { |
| 2750 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 2751 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 2752 | |
| 2753 | // We must have at least one component that refers to the type, and the first |
| 2754 | // one is known to be a field designator. Verify that the ArgTy represents |
| 2755 | // a struct/union/class. |
| 2756 | if (!ArgTy->isRecordType()) |
| 2757 | return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString()); |
| 2758 | |
| 2759 | // Otherwise, create a compound literal expression as the base, and |
| 2760 | // iteratively process the offsetof designators. |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2761 | Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2762 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 2763 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 2764 | // GCC extension, diagnose them. |
| 2765 | if (NumComponents != 1) |
| 2766 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator, |
| 2767 | SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd)); |
| 2768 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2769 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 2770 | const OffsetOfComponent &OC = CompPtr[i]; |
| 2771 | if (OC.isBrackets) { |
| 2772 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2773 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2774 | if (!AT) { |
| 2775 | delete Res; |
| 2776 | return Diag(OC.LocEnd, diag::err_offsetof_array_type, |
| 2777 | Res->getType().getAsString()); |
| 2778 | } |
| 2779 | |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2780 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 2781 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2782 | // C99 6.5.2.1p1 |
| 2783 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 2784 | if (!Idx->getType()->isIntegerType()) |
| 2785 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript, |
| 2786 | Idx->getSourceRange()); |
| 2787 | |
| 2788 | Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd); |
| 2789 | continue; |
| 2790 | } |
| 2791 | |
| 2792 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 2793 | if (!RC) { |
| 2794 | delete Res; |
| 2795 | return Diag(OC.LocEnd, diag::err_offsetof_record_type, |
| 2796 | Res->getType().getAsString()); |
| 2797 | } |
| 2798 | |
| 2799 | // Get the decl corresponding to this. |
| 2800 | RecordDecl *RD = RC->getDecl(); |
| 2801 | FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo); |
| 2802 | if (!MemberDecl) |
| 2803 | return Diag(BuiltinLoc, diag::err_typecheck_no_member, |
| 2804 | OC.U.IdentInfo->getName(), |
| 2805 | SourceRange(OC.LocStart, OC.LocEnd)); |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2806 | |
| 2807 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 2808 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2809 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 2810 | // matter here. |
| 2811 | Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(), |
| 2815 | BuiltinLoc); |
| 2816 | } |
| 2817 | |
| 2818 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2819 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2820 | TypeTy *arg1, TypeTy *arg2, |
| 2821 | SourceLocation RPLoc) { |
| 2822 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 2823 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 2824 | |
| 2825 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 2826 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2827 | return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2830 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 2831 | ExprTy *expr1, ExprTy *expr2, |
| 2832 | SourceLocation RPLoc) { |
| 2833 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 2834 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 2835 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 2836 | |
| 2837 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 2838 | |
| 2839 | // The conditional expression is required to be a constant expression. |
| 2840 | llvm::APSInt condEval(32); |
| 2841 | SourceLocation ExpLoc; |
| 2842 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 2843 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant, |
| 2844 | CondExpr->getSourceRange()); |
| 2845 | |
| 2846 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 2847 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 2848 | RHSExpr->getType(); |
| 2849 | return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc); |
| 2850 | } |
| 2851 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2852 | //===----------------------------------------------------------------------===// |
| 2853 | // Clang Extensions. |
| 2854 | //===----------------------------------------------------------------------===// |
| 2855 | |
| 2856 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
| 2857 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope, |
| 2858 | Declarator &ParamInfo) { |
| 2859 | // Analyze block parameters. |
| 2860 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
| 2861 | |
| 2862 | // Add BSI to CurBlock. |
| 2863 | BSI->PrevBlockInfo = CurBlock; |
| 2864 | CurBlock = BSI; |
| 2865 | |
| 2866 | BSI->ReturnType = 0; |
| 2867 | BSI->TheScope = BlockScope; |
| 2868 | |
| 2869 | // Analyze arguments to block. |
| 2870 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 2871 | "Not a function declarator!"); |
| 2872 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
| 2873 | |
| 2874 | BSI->hasPrototype = FTI.hasPrototype; |
| 2875 | BSI->isVariadic = true; |
| 2876 | |
| 2877 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 2878 | // no arguments, not a function that takes a single void argument. |
| 2879 | if (FTI.hasPrototype && |
| 2880 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2881 | (!((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType().getCVRQualifiers() && |
| 2882 | ((ParmVarDecl *)FTI.ArgInfo[0].Param)->getType()->isVoidType())) { |
| 2883 | // empty arg list, don't push any params. |
| 2884 | BSI->isVariadic = false; |
| 2885 | } else if (FTI.hasPrototype) { |
| 2886 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 2887 | BSI->Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param); |
| 2888 | BSI->isVariadic = FTI.isVariadic; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 2893 | /// is invoked to pop the information about the block from the action impl. |
| 2894 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 2895 | // Ensure that CurBlock is deleted. |
| 2896 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
| 2897 | |
| 2898 | // Pop off CurBlock, handle nested blocks. |
| 2899 | CurBlock = CurBlock->PrevBlockInfo; |
| 2900 | |
| 2901 | // FIXME: Delete the ParmVarDecl objects as well??? |
| 2902 | |
| 2903 | } |
| 2904 | |
| 2905 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 2906 | /// literal was successfully completed. ^(int x){...} |
| 2907 | Sema::ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *body, |
| 2908 | Scope *CurScope) { |
| 2909 | // Ensure that CurBlock is deleted. |
| 2910 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
| 2911 | llvm::OwningPtr<CompoundStmt> Body(static_cast<CompoundStmt*>(body)); |
| 2912 | |
| 2913 | // Pop off CurBlock, handle nested blocks. |
| 2914 | CurBlock = CurBlock->PrevBlockInfo; |
| 2915 | |
| 2916 | QualType RetTy = Context.VoidTy; |
| 2917 | if (BSI->ReturnType) |
| 2918 | RetTy = QualType(BSI->ReturnType, 0); |
| 2919 | |
| 2920 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 2921 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 2922 | ArgTypes.push_back(BSI->Params[i]->getType()); |
| 2923 | |
| 2924 | QualType BlockTy; |
| 2925 | if (!BSI->hasPrototype) |
| 2926 | BlockTy = Context.getFunctionTypeNoProto(RetTy); |
| 2927 | else |
| 2928 | BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(), |
| 2929 | BSI->isVariadic); |
| 2930 | |
| 2931 | BlockTy = Context.getBlockPointerType(BlockTy); |
Steve Naroff | b31e2b3 | 2008-09-17 18:37:59 +0000 | [diff] [blame] | 2932 | return new BlockExpr(CaretLoc, BlockTy, &BSI->Params[0], BSI->Params.size(), |
| 2933 | Body.take()); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2936 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2937 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2938 | /// The number of arguments has already been validated to match the number of |
| 2939 | /// arguments in FnType. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2940 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType, |
| 2941 | ASTContext &Context) { |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2942 | unsigned NumParams = FnType->getNumArgs(); |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2943 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2944 | QualType ExprTy = Context.getCanonicalType(Args[i]->getType()); |
| 2945 | QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i)); |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2946 | |
| 2947 | if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType()) |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2948 | return false; |
Nate Begeman | 778fd3b | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2949 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2950 | return true; |
| 2951 | } |
| 2952 | |
| 2953 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 2954 | SourceLocation *CommaLocs, |
| 2955 | SourceLocation BuiltinLoc, |
| 2956 | SourceLocation RParenLoc) { |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2957 | // __builtin_overload requires at least 2 arguments |
| 2958 | if (NumArgs < 2) |
| 2959 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2960 | SourceRange(BuiltinLoc, RParenLoc)); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2961 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2962 | // The first argument is required to be a constant expression. It tells us |
| 2963 | // the number of arguments to pass to each of the functions to be overloaded. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2964 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2965 | Expr *NParamsExpr = Args[0]; |
| 2966 | llvm::APSInt constEval(32); |
| 2967 | SourceLocation ExpLoc; |
| 2968 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
| 2969 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2970 | NParamsExpr->getSourceRange()); |
| 2971 | |
| 2972 | // Verify that the number of parameters is > 0 |
| 2973 | unsigned NumParams = constEval.getZExtValue(); |
| 2974 | if (NumParams == 0) |
| 2975 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2976 | NParamsExpr->getSourceRange()); |
| 2977 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 2978 | if ((NumParams + 1) > NumArgs) |
| 2979 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2980 | SourceRange(BuiltinLoc, RParenLoc)); |
| 2981 | |
| 2982 | // Figure out the return type, by matching the args to one of the functions |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2983 | // listed after the parameters. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2984 | OverloadExpr *OE = 0; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2985 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 2986 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 2987 | // pointer to function. |
| 2988 | Expr *Fn = UsualUnaryConversions(Args[i]); |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2989 | const FunctionTypeProto *FnType = 0; |
| 2990 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) |
| 2991 | FnType = PT->getPointeeType()->getAsFunctionTypeProto(); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2992 | |
| 2993 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 2994 | // parameters, and the number of parameters must match the value passed to |
| 2995 | // the builtin. |
| 2996 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2997 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype, |
| 2998 | Fn->getSourceRange()); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2999 | |
| 3000 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 3001 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 3002 | // If they match, return a new OverloadExpr. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3003 | if (ExprsMatchFnType(Args+1, FnType, Context)) { |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 3004 | if (OE) |
| 3005 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match, |
| 3006 | OE->getFn()->getSourceRange()); |
| 3007 | // Remember our match, and continue processing the remaining arguments |
| 3008 | // to catch any errors. |
| 3009 | OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(), |
| 3010 | BuiltinLoc, RParenLoc); |
| 3011 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 3012 | } |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 3013 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 3014 | // exactly one of the candidate functions. |
| 3015 | if (OE) |
| 3016 | return OE; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 3017 | |
| 3018 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 3019 | // the return an error. |
| 3020 | std::string typeNames; |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 3021 | for (unsigned i = 0; i != NumParams; ++i) { |
| 3022 | if (i != 0) typeNames += ", "; |
| 3023 | typeNames += Args[i+1]->getType().getAsString(); |
| 3024 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 3025 | |
| 3026 | return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames, |
| 3027 | SourceRange(BuiltinLoc, RParenLoc)); |
| 3028 | } |
| 3029 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 3030 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 3031 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3032 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 3033 | Expr *E = static_cast<Expr*>(expr); |
| 3034 | QualType T = QualType::getFromOpaquePtr(type); |
| 3035 | |
| 3036 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 3037 | |
| 3038 | // Get the va_list type |
| 3039 | QualType VaListType = Context.getBuiltinVaListType(); |
| 3040 | // Deal with implicit array decay; for example, on x86-64, |
| 3041 | // va_list is an array, but it's supposed to decay to |
| 3042 | // a pointer for va_arg. |
| 3043 | if (VaListType->isArrayType()) |
| 3044 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 8754e5b | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 3045 | // Make sure the input expression also decays appropriately. |
| 3046 | UsualUnaryConversions(E); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 3047 | |
| 3048 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 3049 | return Diag(E->getLocStart(), |
| 3050 | diag::err_first_argument_to_va_arg_not_of_type_va_list, |
| 3051 | E->getType().getAsString(), |
| 3052 | E->getSourceRange()); |
| 3053 | |
| 3054 | // FIXME: Warn if a non-POD type is passed in. |
| 3055 | |
| 3056 | return new VAArgExpr(BuiltinLoc, E, T, RPLoc); |
| 3057 | } |
| 3058 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3059 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 3060 | SourceLocation Loc, |
| 3061 | QualType DstType, QualType SrcType, |
| 3062 | Expr *SrcExpr, const char *Flavor) { |
| 3063 | // Decode the result (notice that AST's are still created for extensions). |
| 3064 | bool isInvalid = false; |
| 3065 | unsigned DiagKind; |
| 3066 | switch (ConvTy) { |
| 3067 | default: assert(0 && "Unknown conversion type"); |
| 3068 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3069 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3070 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 3071 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3072 | case IntToPointer: |
| 3073 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 3074 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3075 | case IncompatiblePointer: |
| 3076 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 3077 | break; |
| 3078 | case FunctionVoidPointer: |
| 3079 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 3080 | break; |
| 3081 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 3082 | // If the qualifiers lost were because we were applying the |
| 3083 | // (deprecated) C++ conversion from a string literal to a char* |
| 3084 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 3085 | // Ideally, this check would be performed in |
| 3086 | // CheckPointerTypesForAssignment. However, that would require a |
| 3087 | // bit of refactoring (so that the second argument is an |
| 3088 | // expression, rather than a type), which should be done as part |
| 3089 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 3090 | // C++ semantics. |
| 3091 | if (getLangOptions().CPlusPlus && |
| 3092 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 3093 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3094 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 3095 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3096 | case IntToBlockPointer: |
| 3097 | DiagKind = diag::err_int_to_block_pointer; |
| 3098 | break; |
| 3099 | case IncompatibleBlockPointer: |
Steve Naroff | 82324d6 | 2008-09-24 23:31:10 +0000 | [diff] [blame] | 3100 | DiagKind = diag::ext_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | case BlockVoidPointer: |
| 3103 | DiagKind = diag::ext_typecheck_convert_pointer_void_block; |
| 3104 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3105 | case Incompatible: |
| 3106 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 3107 | isInvalid = true; |
| 3108 | break; |
| 3109 | } |
| 3110 | |
| 3111 | Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor, |
| 3112 | SrcExpr->getSourceRange()); |
| 3113 | return isInvalid; |
| 3114 | } |