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