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