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