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