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