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. |
Daniel Dunbar | 662e8b5 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 331 | if (SD == 0 && &II == SuperID) { |
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. |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 872 | |
| 873 | // FIXME: This logic is not correct, we should search for |
| 874 | // properties first. Additionally, the AST node doesn't currently |
| 875 | // have enough information to store the setter argument. |
| 876 | #if 0 |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 877 | // Before we look for explicit property declarations, we check for |
| 878 | // nullary methods (which allow '.' notation). |
| 879 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 880 | if (ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel)) |
| 881 | return new ObjCPropertyRefExpr(MD, MD->getResultType(), |
| 882 | MemberLoc, BaseExpr); |
| 883 | |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 884 | // If this reference is in an @implementation, check for 'private' methods. |
| 885 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 886 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 887 | if (ObjCImplementationDecl *ImpDecl = |
| 888 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 889 | if (ObjCMethodDecl *MD = ImpDecl->getInstanceMethod(Sel)) |
| 890 | return new ObjCPropertyRefExpr(MD, MD->getResultType(), |
| 891 | MemberLoc, BaseExpr); |
| 892 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 893 | #endif |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 894 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 895 | // FIXME: Need to deal with setter methods that take 1 argument. E.g.: |
| 896 | // @interface NSBundle : NSObject {} |
| 897 | // - (NSString *)bundlePath; |
| 898 | // - (void)setBundlePath:(NSString *)x; |
| 899 | // @end |
| 900 | // void someMethod() { frameworkBundle.bundlePath = 0; } |
| 901 | // |
| 902 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) |
| 903 | return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr); |
| 904 | |
| 905 | // Lastly, check protocols on qualified interfaces. |
Chris Lattner | 9baefc2 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 906 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 907 | E = IFTy->qual_end(); I != E; ++I) |
| 908 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) |
| 909 | return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 910 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 911 | |
| 912 | // Handle 'field access' to vectors, such as 'V.xx'. |
| 913 | if (BaseType->isExtVectorType() && OpKind == tok::period) { |
| 914 | // Component access limited to variables (reject vec4.rg.g). |
| 915 | if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) && |
| 916 | !isa<ExtVectorElementExpr>(BaseExpr)) |
Chris Lattner | 2a01b72 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 917 | return Diag(MemberLoc, diag::err_ext_vector_component_access, |
| 918 | BaseExpr->getSourceRange()); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 919 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 920 | if (ret.isNull()) |
| 921 | return true; |
| 922 | return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc); |
| 923 | } |
| 924 | |
Chris Lattner | 2a01b72 | 2008-07-21 05:35:34 +0000 | [diff] [blame] | 925 | return Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union, |
| 926 | BaseType.getAsString(), BaseExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 929 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 930 | /// This provides the location of the left/right parens and a list of comma |
| 931 | /// locations. |
| 932 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 933 | ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 934 | ExprTy **args, unsigned NumArgs, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 935 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 936 | Expr *Fn = static_cast<Expr *>(fn); |
| 937 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 938 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 939 | FunctionDecl *FDecl = NULL; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 940 | |
| 941 | // Promote the function operand. |
| 942 | UsualUnaryConversions(Fn); |
| 943 | |
| 944 | // If we're directly calling a function, get the declaration for |
| 945 | // that function. |
| 946 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 947 | if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr())) |
| 948 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
| 949 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 950 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 951 | // of arguments and function on error. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 952 | llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs, |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 953 | Context.BoolTy, RParenLoc)); |
| 954 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 955 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 956 | // type pointer to function". |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 957 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | if (PT == 0) |
Chris Lattner | ad2018f | 2008-08-14 04:33:24 +0000 | [diff] [blame] | 959 | return Diag(LParenLoc, diag::err_typecheck_call_not_function, |
| 960 | Fn->getSourceRange()); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 961 | const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 962 | if (FuncT == 0) |
Chris Lattner | ad2018f | 2008-08-14 04:33:24 +0000 | [diff] [blame] | 963 | return Diag(LParenLoc, diag::err_typecheck_call_not_function, |
| 964 | Fn->getSourceRange()); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 965 | |
| 966 | // We know the result type of the call, set it. |
| 967 | TheCall->setType(FuncT->getResultType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 968 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 969 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 970 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 971 | // assignment, to the types of the corresponding parameter, ... |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 972 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 973 | unsigned NumArgsToCheck = NumArgs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 975 | // If too few arguments are available (and we don't have default |
| 976 | // arguments for the remaining parameters), don't make the call. |
| 977 | if (NumArgs < NumArgsInProto) { |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 978 | if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 979 | // Use default arguments for missing arguments |
| 980 | NumArgsToCheck = NumArgsInProto; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 981 | TheCall->setNumArgs(NumArgsInProto); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 982 | } else |
| 983 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 984 | Fn->getSourceRange()); |
| 985 | } |
| 986 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 987 | // If too many are passed and not variadic, error on the extras and drop |
| 988 | // them. |
| 989 | if (NumArgs > NumArgsInProto) { |
| 990 | if (!Proto->isVariadic()) { |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 991 | Diag(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 992 | diag::err_typecheck_call_too_many_args, Fn->getSourceRange(), |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 993 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 994 | Args[NumArgs-1]->getLocEnd())); |
| 995 | // This deletes the extra arguments. |
| 996 | TheCall->setNumArgs(NumArgsInProto); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | } |
| 998 | NumArgsToCheck = NumArgsInProto; |
| 999 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1000 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1001 | // 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] | 1002 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1003 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1004 | |
| 1005 | Expr *Arg; |
| 1006 | if (i < NumArgs) |
| 1007 | Arg = Args[i]; |
| 1008 | else |
| 1009 | Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1010 | QualType ArgType = Arg->getType(); |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1012 | // Compute implicit casts from the operand to the formal argument type. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1013 | AssignConvertType ConvTy = |
| 1014 | CheckSingleAssignmentConstraints(ProtoArgType, Arg); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1015 | TheCall->setArg(i, Arg); |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame^] | 1016 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1017 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType, |
| 1018 | ArgType, Arg, "passing")) |
| 1019 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1021 | |
| 1022 | // If this is a variadic call, handle args passed through "...". |
| 1023 | if (Proto->isVariadic()) { |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1024 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1025 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 1026 | Expr *Arg = Args[i]; |
| 1027 | DefaultArgumentPromotion(Arg); |
| 1028 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1029 | } |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1030 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1031 | } else { |
| 1032 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
| 1033 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1034 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1035 | for (unsigned i = 0; i != NumArgs; i++) { |
| 1036 | Expr *Arg = Args[i]; |
| 1037 | DefaultArgumentPromotion(Arg); |
| 1038 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 1039 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1042 | // Do special checking on direct calls to functions. |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1043 | if (FDecl) |
| 1044 | return CheckFunctionCall(FDecl, TheCall.take()); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1046 | return TheCall.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1050 | ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 1051 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1052 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1053 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 1054 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1055 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 1056 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 1057 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1058 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1059 | if (literalType->isVariableArrayType()) |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 1060 | return Diag(LParenLoc, |
| 1061 | diag::err_variable_object_no_init, |
| 1062 | SourceRange(LParenLoc, |
| 1063 | literalExpr->getSourceRange().getEnd())); |
| 1064 | } else if (literalType->isIncompleteType()) { |
| 1065 | return Diag(LParenLoc, |
| 1066 | diag::err_typecheck_decl_incomplete_type, |
| 1067 | literalType.getAsString(), |
| 1068 | SourceRange(LParenLoc, |
| 1069 | literalExpr->getSourceRange().getEnd())); |
| 1070 | } |
| 1071 | |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1072 | if (CheckInitializerTypes(literalExpr, literalType)) |
Steve Naroff | 58d1821 | 2008-01-09 20:58:06 +0000 | [diff] [blame] | 1073 | return true; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1074 | |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1075 | bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1076 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 1077 | if (CheckForConstantInitializer(literalExpr, literalType)) |
| 1078 | return true; |
| 1079 | } |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 1080 | return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1084 | ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1085 | SourceLocation RBraceLoc) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 1086 | Expr **InitList = reinterpret_cast<Expr**>(initlist); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1087 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1088 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 1089 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1091 | InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc); |
| 1092 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
| 1093 | return E; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1096 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 58d5ebb | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 1097 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1098 | UsualUnaryConversions(castExpr); |
| 1099 | |
| 1100 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 1101 | // type needs to be scalar. |
| 1102 | if (castType->isVoidType()) { |
| 1103 | // Cast to void allows any expr type. |
| 1104 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
| 1105 | // GCC struct/union extension: allow cast to self. |
| 1106 | if (Context.getCanonicalType(castType) != |
| 1107 | Context.getCanonicalType(castExpr->getType()) || |
| 1108 | (!castType->isStructureType() && !castType->isUnionType())) { |
| 1109 | // Reject any other conversions to non-scalar types. |
| 1110 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar, |
| 1111 | castType.getAsString(), castExpr->getSourceRange()); |
| 1112 | } |
| 1113 | |
| 1114 | // accept this, but emit an ext-warn. |
| 1115 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar, |
| 1116 | castType.getAsString(), castExpr->getSourceRange()); |
| 1117 | } else if (!castExpr->getType()->isScalarType() && |
| 1118 | !castExpr->getType()->isVectorType()) { |
| 1119 | return Diag(castExpr->getLocStart(), |
| 1120 | diag::err_typecheck_expect_scalar_operand, |
| 1121 | castExpr->getType().getAsString(),castExpr->getSourceRange()); |
| 1122 | } else if (castExpr->getType()->isVectorType()) { |
| 1123 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 1124 | return true; |
| 1125 | } else if (castType->isVectorType()) { |
| 1126 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 1127 | return true; |
| 1128 | } |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 1132 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 1133 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 1134 | |
| 1135 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1136 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 1137 | return Diag(R.getBegin(), |
| 1138 | Ty->isVectorType() ? |
| 1139 | diag::err_invalid_conversion_between_vectors : |
| 1140 | diag::err_invalid_conversion_between_vector_and_integer, |
| 1141 | VectorTy.getAsString().c_str(), |
| 1142 | Ty.getAsString().c_str(), R); |
| 1143 | } else |
| 1144 | return Diag(R.getBegin(), |
| 1145 | diag::err_invalid_conversion_between_vector_and_scalar, |
| 1146 | VectorTy.getAsString().c_str(), |
| 1147 | Ty.getAsString().c_str(), R); |
| 1148 | |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 1152 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1153 | ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1155 | assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 1156 | |
| 1157 | Expr *castExpr = static_cast<Expr*>(Op); |
| 1158 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 1159 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 1160 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
| 1161 | return true; |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1162 | return new ExplicitCastExpr(castType, castExpr, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1165 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 1166 | /// In that case, lex = cond. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1167 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1168 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1169 | UsualUnaryConversions(cond); |
| 1170 | UsualUnaryConversions(lex); |
| 1171 | UsualUnaryConversions(rex); |
| 1172 | QualType condT = cond->getType(); |
| 1173 | QualType lexT = lex->getType(); |
| 1174 | QualType rexT = rex->getType(); |
| 1175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1176 | // first, check the condition. |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1177 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 1178 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 1179 | condT.getAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1180 | return QualType(); |
| 1181 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Now check the two expressions. |
| 1184 | |
| 1185 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 1186 | // to find a common type: C99 6.5.15p3,5. |
| 1187 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1188 | UsualArithmeticConversions(lex, rex); |
| 1189 | return lex->getType(); |
| 1190 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1191 | |
| 1192 | // If both operands are the same structure or union type, the result is that |
| 1193 | // type. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1194 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1195 | if (const RecordType *RHSRT = rexT->getAsRecordType()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1196 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1197 | // "If both the operands have structure or union type, the result has |
| 1198 | // that type." This implies that CV qualifiers are dropped. |
| 1199 | return lexT.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1201 | |
| 1202 | // 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] | 1203 | // The following || allows only one side to be void (a GCC-ism). |
| 1204 | if (lexT->isVoidType() || rexT->isVoidType()) { |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 1205 | if (!lexT->isVoidType()) |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 1206 | Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void, |
| 1207 | rex->getSourceRange()); |
| 1208 | if (!rexT->isVoidType()) |
| 1209 | Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void, |
Nuno Lopes | d8de725 | 2008-06-04 19:14:12 +0000 | [diff] [blame] | 1210 | lex->getSourceRange()); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 1211 | ImpCastExprToType(lex, Context.VoidTy); |
| 1212 | ImpCastExprToType(rex, Context.VoidTy); |
| 1213 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 1214 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1215 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 1216 | // the type of the other operand." |
| 1217 | if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1218 | ImpCastExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1219 | return lexT; |
| 1220 | } |
| 1221 | if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1222 | ImpCastExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 1223 | return rexT; |
| 1224 | } |
Chris Lattner | bd57d36 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 1225 | // Handle the case where both operands are pointers before we handle null |
| 1226 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1227 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 1228 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 1229 | // get the "pointed to" types |
| 1230 | QualType lhptee = LHSPT->getPointeeType(); |
| 1231 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1233 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 1234 | if (lhptee->isVoidType() && |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1235 | rhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1236 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 1237 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1238 | QualType destType = Context.getPointerType(destPointee); |
| 1239 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 1240 | ImpCastExprToType(rex, destType); // promote to void* |
| 1241 | return destType; |
| 1242 | } |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1243 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1244 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1245 | QualType destType = Context.getPointerType(destPointee); |
| 1246 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 1247 | ImpCastExprToType(rex, destType); // promote to void* |
| 1248 | return destType; |
| 1249 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1251 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1252 | rhptee.getUnqualifiedType())) { |
Steve Naroff | c0ff1ca | 2008-02-01 22:44:48 +0000 | [diff] [blame] | 1253 | Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers, |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1254 | lexT.getAsString(), rexT.getAsString(), |
| 1255 | lex->getSourceRange(), rex->getSourceRange()); |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 1256 | // In this situation, assume a conservative type; in general |
| 1257 | // we assume void* type. No especially good reason, but this |
| 1258 | // is what gcc does, and we do have to pick to get a |
| 1259 | // consistent AST. However, if either type is an Objective-C |
| 1260 | // object type then use id. |
| 1261 | QualType incompatTy; |
| 1262 | if (Context.isObjCObjectPointerType(lexT) || |
| 1263 | Context.isObjCObjectPointerType(rexT)) { |
| 1264 | incompatTy = Context.getObjCIdType(); |
| 1265 | } else { |
| 1266 | incompatTy = Context.getPointerType(Context.VoidTy); |
| 1267 | } |
| 1268 | ImpCastExprToType(lex, incompatTy); |
| 1269 | ImpCastExprToType(rex, incompatTy); |
| 1270 | return incompatTy; |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1271 | } |
| 1272 | // The pointer types are compatible. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1273 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 1274 | // differently qualified versions of compatible types, the result type is |
| 1275 | // a pointer to an appropriately qualified version of the *composite* |
| 1276 | // type. |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 1277 | // FIXME: Need to calculate the composite type. |
Eli Friedman | a541d53 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 1278 | // FIXME: Need to add qualifiers |
Eli Friedman | 5835ea2 | 2008-05-16 20:37:07 +0000 | [diff] [blame] | 1279 | QualType compositeType = lexT; |
| 1280 | ImpCastExprToType(lex, compositeType); |
| 1281 | ImpCastExprToType(rex, compositeType); |
| 1282 | return compositeType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | } |
| 1284 | } |
Steve Naroff | aa73eec | 2008-05-31 22:33:45 +0000 | [diff] [blame] | 1285 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 1286 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 1287 | // id with statically typed objects). |
Steve Naroff | aa73eec | 2008-05-31 22:33:45 +0000 | [diff] [blame] | 1288 | if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) { |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 1289 | if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true)) { |
| 1290 | // FIXME: This is not the correct composite type. |
| 1291 | QualType compositeType = Context.getObjCIdType(); |
| 1292 | ImpCastExprToType(lex, compositeType); |
| 1293 | ImpCastExprToType(rex, compositeType); |
| 1294 | return compositeType; |
| 1295 | } |
Steve Naroff | aa73eec | 2008-05-31 22:33:45 +0000 | [diff] [blame] | 1296 | } |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 1297 | // Otherwise, the operands are not compatible. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1299 | lexT.getAsString(), rexT.getAsString(), |
| 1300 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | return QualType(); |
| 1302 | } |
| 1303 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1304 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | /// in the case of a the GNU conditional expr extension. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1306 | Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1307 | SourceLocation ColonLoc, |
| 1308 | ExprTy *Cond, ExprTy *LHS, |
| 1309 | ExprTy *RHS) { |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 1310 | Expr *CondExpr = (Expr *) Cond; |
| 1311 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1312 | |
| 1313 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 1314 | // was the condition. |
| 1315 | bool isLHSNull = LHSExpr == 0; |
| 1316 | if (isLHSNull) |
| 1317 | LHSExpr = CondExpr; |
| 1318 | |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 1319 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 1320 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | if (result.isNull()) |
| 1322 | return true; |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1323 | return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr, |
| 1324 | RHSExpr, result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | |
| 1328 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 1329 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 1330 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 1331 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 1332 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1333 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1334 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 1335 | QualType lhptee, rhptee; |
| 1336 | |
| 1337 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1338 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 1339 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | |
| 1341 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1342 | lhptee = Context.getCanonicalType(lhptee); |
| 1343 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1345 | AssignConvertType ConvTy = Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | |
| 1347 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 1348 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 1349 | // qualifiers of the type *pointed to* by the right; |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1350 | // FIXME: Handle ASQualType |
| 1351 | if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) != |
| 1352 | rhptee.getCVRQualifiers()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1353 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | |
| 1355 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 1356 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 1357 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1358 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1359 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1360 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1361 | |
| 1362 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1363 | assert(rhptee->isFunctionType()); |
| 1364 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1368 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1369 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1370 | |
| 1371 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 1372 | assert(lhptee->isFunctionType()); |
| 1373 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1374 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Check for ObjC interfaces |
| 1377 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 1378 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 1379 | if (LHSIface && RHSIface && |
| 1380 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 1381 | return ConvTy; |
| 1382 | |
| 1383 | // ID acts sort of like void* for ObjC interfaces |
| 1384 | if (LHSIface && Context.isObjCIdType(rhptee)) |
| 1385 | return ConvTy; |
| 1386 | if (RHSIface && Context.isObjCIdType(lhptee)) |
| 1387 | return ConvTy; |
| 1388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1389 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 1390 | // unqualified versions of compatible types, ... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1391 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1392 | rhptee.getUnqualifiedType())) |
| 1393 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1394 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 1398 | /// has code to accommodate several GCC extensions when type checking |
| 1399 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 1400 | /// |
| 1401 | /// int a, *pint; |
| 1402 | /// short *pshort; |
| 1403 | /// struct foo *pfoo; |
| 1404 | /// |
| 1405 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 1406 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 1407 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 1408 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 1409 | /// |
| 1410 | /// As a result, the code for dealing with pointers is more complex than the |
| 1411 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1413 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1415 | // Get canonical types. We're not formatting these types, just comparing |
| 1416 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1417 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 1418 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1419 | |
| 1420 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 1421 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 1422 | |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1423 | if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
Chris Lattner | 8f8fc7b | 2008-04-07 06:52:53 +0000 | [diff] [blame] | 1424 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1425 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1426 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1427 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1429 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 1430 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1431 | return Compatible; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 1432 | // Relax integer conversions like we do for pointers below. |
| 1433 | if (rhsType->isIntegerType()) |
| 1434 | return IntToPointer; |
| 1435 | if (lhsType->isIntegerType()) |
| 1436 | return PointerToInt; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1437 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1438 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1439 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1440 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1441 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1442 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 1443 | if (LV->getElementType() == rhsType) |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1444 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1445 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1446 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 1447 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 1448 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1449 | if (getLangOptions().LaxVectorConversions && |
| 1450 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1451 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
| 1452 | return Compatible; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1453 | } |
| 1454 | return Incompatible; |
| 1455 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1457 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1458 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1460 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1461 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1462 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1463 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1464 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1466 | return Incompatible; |
| 1467 | } |
| 1468 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1469 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1470 | // 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] | 1471 | if (lhsType == Context.BoolTy) |
| 1472 | return Compatible; |
| 1473 | |
| 1474 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1475 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1477 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1478 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1479 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1480 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1482 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 1483 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1485 | } |
| 1486 | return Incompatible; |
| 1487 | } |
| 1488 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1489 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1490 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1491 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 1492 | // a null pointer constant. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1493 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 1494 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1495 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1496 | return Compatible; |
| 1497 | } |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1498 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1499 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1500 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1501 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1502 | // |
| 1503 | // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references |
| 1504 | // are better understood. |
| 1505 | if (!lhsType->isReferenceType()) |
| 1506 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1508 | Sema::AssignConvertType result = |
| 1509 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1510 | |
| 1511 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 1512 | // type of the assignment expression. |
| 1513 | if (rExpr->getType() != lhsType) |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1514 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1515 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1518 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1519 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 1520 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 1521 | } |
| 1522 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1523 | QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1524 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 1525 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1526 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1527 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1530 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 1531 | Expr *&rex) { |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 1532 | // For conversion purposes, we ignore any qualifiers. |
| 1533 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1534 | QualType lhsType = |
| 1535 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 1536 | QualType rhsType = |
| 1537 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1538 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1539 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 1540 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1541 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1542 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1543 | // Handle the case of a vector & extvector type of the same size and element |
| 1544 | // type. It would be nice if we only had one vector type someday. |
| 1545 | if (getLangOptions().LaxVectorConversions) |
| 1546 | if (const VectorType *LV = lhsType->getAsVectorType()) |
| 1547 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 1548 | if (LV->getElementType() == RV->getElementType() && |
| 1549 | LV->getNumElements() == RV->getNumElements()) |
| 1550 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
| 1551 | |
| 1552 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 1553 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1554 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1555 | QualType eltType = V->getElementType(); |
| 1556 | |
| 1557 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
| 1558 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 1559 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1560 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1561 | return lhsType; |
| 1562 | } |
| 1563 | } |
| 1564 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1565 | // 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] | 1566 | // promote the lhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1567 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1568 | QualType eltType = V->getElementType(); |
| 1569 | |
| 1570 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
| 1571 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 1572 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1573 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1574 | return rhsType; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1578 | // You cannot convert between vector values of different size. |
| 1579 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 1580 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1581 | lex->getSourceRange(), rex->getSourceRange()); |
| 1582 | return QualType(); |
| 1583 | } |
| 1584 | |
| 1585 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1586 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1588 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1589 | |
| 1590 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1592 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1593 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1595 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1596 | return compType; |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1597 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1601 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1602 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1603 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1604 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1605 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1607 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1608 | return compType; |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1609 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1613 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1614 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1615 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1616 | return CheckVectorOperands(loc, lex, rex); |
| 1617 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1618 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 1619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1621 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1622 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1623 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 1624 | // Put any potential pointer into PExp |
| 1625 | Expr* PExp = lex, *IExp = rex; |
| 1626 | if (IExp->getType()->isPointerType()) |
| 1627 | std::swap(PExp, IExp); |
| 1628 | |
| 1629 | if (const PointerType* PTy = PExp->getType()->getAsPointerType()) { |
| 1630 | if (IExp->getType()->isIntegerType()) { |
| 1631 | // Check for arithmetic on pointers to incomplete types |
| 1632 | if (!PTy->getPointeeType()->isObjectType()) { |
| 1633 | if (PTy->getPointeeType()->isVoidType()) { |
| 1634 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1635 | lex->getSourceRange(), rex->getSourceRange()); |
| 1636 | } else { |
| 1637 | Diag(loc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1638 | lex->getType().getAsString(), lex->getSourceRange()); |
| 1639 | return QualType(); |
| 1640 | } |
| 1641 | } |
| 1642 | return PExp->getType(); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1646 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1649 | // C99 6.5.6 |
| 1650 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
| 1651 | SourceLocation loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1652 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1654 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1655 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1657 | // Enforce type constraints: C99 6.5.6p3. |
| 1658 | |
| 1659 | // Handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1660 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1661 | return compType; |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1662 | |
| 1663 | // Either ptr - int or ptr - ptr. |
| 1664 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1665 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1667 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1668 | if (!lpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1669 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1670 | if (lpointee->isVoidType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1671 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1672 | lex->getSourceRange(), rex->getSourceRange()); |
| 1673 | } else { |
| 1674 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1675 | lex->getType().getAsString(), lex->getSourceRange()); |
| 1676 | return QualType(); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | // The result type of a pointer-int computation is the pointer type. |
| 1681 | if (rex->getType()->isIntegerType()) |
| 1682 | return lex->getType(); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1684 | // Handle pointer-pointer subtractions. |
| 1685 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1686 | QualType rpointee = RHSPTy->getPointeeType(); |
| 1687 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1688 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1689 | if (!rpointee->isObjectType()) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1690 | // Handle the GNU void* extension. |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1691 | if (rpointee->isVoidType()) { |
| 1692 | if (!lpointee->isVoidType()) |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1693 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1694 | lex->getSourceRange(), rex->getSourceRange()); |
| 1695 | } else { |
| 1696 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1697 | rex->getType().getAsString(), rex->getSourceRange()); |
| 1698 | return QualType(); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | // Pointee types must be compatible. |
Eli Friedman | f1c7b48 | 2008-09-02 05:09:35 +0000 | [diff] [blame^] | 1703 | if (!Context.typesAreCompatible( |
| 1704 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 1705 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1706 | Diag(loc, diag::err_typecheck_sub_ptr_compatible, |
| 1707 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1708 | lex->getSourceRange(), rex->getSourceRange()); |
| 1709 | return QualType(); |
| 1710 | } |
| 1711 | |
| 1712 | return Context.getPointerDiffType(); |
| 1713 | } |
| 1714 | } |
| 1715 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1716 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1719 | // C99 6.5.7 |
| 1720 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc, |
| 1721 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1722 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 1723 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 1724 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1726 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 1727 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 1728 | if (!isCompAssign) |
| 1729 | UsualUnaryConversions(lex); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1730 | UsualUnaryConversions(rex); |
| 1731 | |
| 1732 | // "The type of the result is that of the promoted left operand." |
| 1733 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1736 | static bool areComparableObjCInterfaces(QualType LHS, QualType RHS, |
| 1737 | ASTContext& Context) { |
| 1738 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 1739 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 1740 | // ID acts sort of like void* for ObjC interfaces |
| 1741 | if (LHSIface && Context.isObjCIdType(RHS)) |
| 1742 | return true; |
| 1743 | if (RHSIface && Context.isObjCIdType(LHS)) |
| 1744 | return true; |
| 1745 | if (!LHSIface || !RHSIface) |
| 1746 | return false; |
| 1747 | return Context.canAssignObjCInterfaces(LHSIface, RHSIface) || |
| 1748 | Context.canAssignObjCInterfaces(RHSIface, LHSIface); |
| 1749 | } |
| 1750 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1751 | // C99 6.5.8 |
| 1752 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc, |
| 1753 | bool isRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1754 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1755 | return CheckVectorCompareOperands(lex, rex, loc, isRelational); |
| 1756 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1757 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1758 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1759 | UsualArithmeticConversions(lex, rex); |
| 1760 | else { |
| 1761 | UsualUnaryConversions(lex); |
| 1762 | UsualUnaryConversions(rex); |
| 1763 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1764 | QualType lType = lex->getType(); |
| 1765 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1767 | // For non-floating point types, check for self-comparisons of the form |
| 1768 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 1769 | // often indicate logic errors in the program. |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 1770 | if (!lType->isFloatingType()) { |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1771 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 1772 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 1773 | if (DRL->getDecl() == DRR->getDecl()) |
| 1774 | Diag(loc, diag::warn_selfcomparison); |
| 1775 | } |
| 1776 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1777 | if (isRelational) { |
| 1778 | if (lType->isRealType() && rType->isRealType()) |
| 1779 | return Context.IntTy; |
| 1780 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1781 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1782 | if (lType->isFloatingType()) { |
| 1783 | assert (rType->isFloatingType()); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1784 | CheckFloatComparison(loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1787 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 1788 | return Context.IntTy; |
| 1789 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1791 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 1792 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 1793 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1794 | // All of the following pointer related warnings are GCC extensions, except |
| 1795 | // when handling null pointer constants. One day, we can consider making them |
| 1796 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 1797 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 1798 | QualType LCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1799 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 1800 | QualType RCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 1801 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1802 | |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 1803 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 1804 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 1805 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 1806 | RCanPointeeTy.getUnqualifiedType()) && |
| 1807 | !areComparableObjCInterfaces(LCanPointeeTy, RCanPointeeTy, Context)) { |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1808 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 1809 | lType.getAsString(), rType.getAsString(), |
| 1810 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1811 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1812 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1813 | return Context.IntTy; |
| 1814 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 1815 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
| 1816 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 1817 | ImpCastExprToType(rex, lType); |
| 1818 | return Context.IntTy; |
| 1819 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 1820 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 1821 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
| 1822 | rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1823 | if (!RHSIsNull) |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1824 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1825 | lType.getAsString(), rType.getAsString(), |
| 1826 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1827 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1828 | return Context.IntTy; |
| 1829 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 1830 | if (lType->isIntegerType() && |
| 1831 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1832 | if (!LHSIsNull) |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1833 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1834 | lType.getAsString(), rType.getAsString(), |
| 1835 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1836 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1837 | return Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1838 | } |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1839 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 1842 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| 1843 | /// operates on extended vector types. Instead of producing an IntTy result, |
| 1844 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 1845 | /// types. |
| 1846 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
| 1847 | SourceLocation loc, |
| 1848 | bool isRelational) { |
| 1849 | // Check to make sure we're operating on vectors of the same type and width, |
| 1850 | // Allowing one side to be a scalar of element type. |
| 1851 | QualType vType = CheckVectorOperands(loc, lex, rex); |
| 1852 | if (vType.isNull()) |
| 1853 | return vType; |
| 1854 | |
| 1855 | QualType lType = lex->getType(); |
| 1856 | QualType rType = rex->getType(); |
| 1857 | |
| 1858 | // For non-floating point types, check for self-comparisons of the form |
| 1859 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 1860 | // often indicate logic errors in the program. |
| 1861 | if (!lType->isFloatingType()) { |
| 1862 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 1863 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 1864 | if (DRL->getDecl() == DRR->getDecl()) |
| 1865 | Diag(loc, diag::warn_selfcomparison); |
| 1866 | } |
| 1867 | |
| 1868 | // Check for comparisons of floating point operands using != and ==. |
| 1869 | if (!isRelational && lType->isFloatingType()) { |
| 1870 | assert (rType->isFloatingType()); |
| 1871 | CheckFloatComparison(loc,lex,rex); |
| 1872 | } |
| 1873 | |
| 1874 | // Return the type for the comparison, which is the same as vector type for |
| 1875 | // integer vectors, or an integer type of identical size and number of |
| 1876 | // elements for floating point vectors. |
| 1877 | if (lType->isIntegerType()) |
| 1878 | return lType; |
| 1879 | |
| 1880 | const VectorType *VTy = lType->getAsVectorType(); |
| 1881 | |
| 1882 | // FIXME: need to deal with non-32b int / non-64b long long |
| 1883 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 1884 | if (TypeSize == 32) { |
| 1885 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 1886 | } |
| 1887 | assert(TypeSize == 64 && "Unhandled vector element size in vector compare"); |
| 1888 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 1889 | } |
| 1890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1891 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1892 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1894 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1895 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1896 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1897 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1898 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1899 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1900 | return compType; |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1901 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1905 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1906 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1907 | UsualUnaryConversions(lex); |
| 1908 | UsualUnaryConversions(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1909 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 1910 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1911 | return Context.IntTy; |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1912 | return InvalidOperands(loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1916 | Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1917 | { |
| 1918 | QualType lhsType = lex->getType(); |
| 1919 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 1920 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1921 | |
| 1922 | switch (mlval) { // C99 6.5.16p2 |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1923 | case Expr::MLV_Valid: |
| 1924 | break; |
| 1925 | case Expr::MLV_ConstQualified: |
| 1926 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 1927 | return QualType(); |
| 1928 | case Expr::MLV_ArrayType: |
| 1929 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 1930 | lhsType.getAsString(), lex->getSourceRange()); |
| 1931 | return QualType(); |
| 1932 | case Expr::MLV_NotObjectType: |
| 1933 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 1934 | lhsType.getAsString(), lex->getSourceRange()); |
| 1935 | return QualType(); |
| 1936 | case Expr::MLV_InvalidExpression: |
| 1937 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 1938 | lex->getSourceRange()); |
| 1939 | return QualType(); |
| 1940 | case Expr::MLV_IncompleteType: |
| 1941 | case Expr::MLV_IncompleteVoidType: |
| 1942 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 1943 | lhsType.getAsString(), lex->getSourceRange()); |
| 1944 | return QualType(); |
| 1945 | case Expr::MLV_DuplicateVectorComponents: |
| 1946 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 1947 | lex->getSourceRange()); |
| 1948 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1949 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1950 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1951 | AssignConvertType ConvTy; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 1952 | if (compoundType.isNull()) { |
| 1953 | // Simple assignment "x = y". |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1954 | ConvTy = CheckSingleAssignmentConstraints(lhsType, rex); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 1955 | |
| 1956 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 1957 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 1958 | // instead of "x += 4". |
| 1959 | Expr *RHSCheck = rex; |
| 1960 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 1961 | RHSCheck = ICE->getSubExpr(); |
| 1962 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 1963 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 1964 | UO->getOpcode() == UnaryOperator::Minus) && |
| 1965 | loc.isFileID() && UO->getOperatorLoc().isFileID() && |
| 1966 | // Only if the two operators are exactly adjacent. |
| 1967 | loc.getFileLocWithOffset(1) == UO->getOperatorLoc()) |
| 1968 | Diag(loc, diag::warn_not_compound_assign, |
| 1969 | UO->getOpcode() == UnaryOperator::Plus ? "+" : "-", |
| 1970 | SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc())); |
| 1971 | } |
| 1972 | } else { |
| 1973 | // Compound assignment "x += y" |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1974 | ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 1975 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1976 | |
| 1977 | if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType, |
| 1978 | rex, "assigning")) |
| 1979 | return QualType(); |
| 1980 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1981 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 1982 | // left operand unless the left operand has qualified type, in which case |
| 1983 | // it is the unqualified version of the type of the left operand. |
| 1984 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 1985 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1986 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 1987 | // oprdu. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1988 | return lhsType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1992 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
| 1995 | DefaultFunctionArrayConversion(rex); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1996 | return rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1999 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 2000 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2001 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 2002 | QualType resType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
| 2004 | |
Steve Naroff | 084f9ed | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 2005 | // C99 6.5.2.4p1: We allow complex as a GCC extension. |
Steve Naroff | d848a38 | 2007-11-11 14:15:57 +0000 | [diff] [blame] | 2006 | if (const PointerType *pt = resType->getAsPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 2007 | if (pt->getPointeeType()->isVoidType()) { |
| 2008 | Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange()); |
| 2009 | } else if (!pt->getPointeeType()->isObjectType()) { |
| 2010 | // C99 6.5.2.4p2, 6.5.6p2 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2011 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 2012 | resType.getAsString(), op->getSourceRange()); |
| 2013 | return QualType(); |
| 2014 | } |
Steve Naroff | 084f9ed | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 2015 | } else if (!resType->isRealType()) { |
| 2016 | if (resType->isComplexType()) |
| 2017 | // C99 does not support ++/-- on complex types. |
| 2018 | Diag(OpLoc, diag::ext_integer_increment_complex, |
| 2019 | resType.getAsString(), op->getSourceRange()); |
| 2020 | else { |
| 2021 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 2022 | resType.getAsString(), op->getSourceRange()); |
| 2023 | return QualType(); |
| 2024 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2025 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 2026 | // At this point, we know we have a real, complex or pointer type. |
| 2027 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 2028 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2029 | if (mlval != Expr::MLV_Valid) { |
| 2030 | // FIXME: emit a more precise diagnostic... |
| 2031 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
| 2032 | op->getSourceRange()); |
| 2033 | return QualType(); |
| 2034 | } |
| 2035 | return resType; |
| 2036 | } |
| 2037 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2038 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2039 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2040 | /// where the declaration is needed for type checking. We only need to |
| 2041 | /// handle cases when the expression references a function designator |
| 2042 | /// or is an lvalue. Here are some examples: |
| 2043 | /// - &(x) => x |
| 2044 | /// - &*****f => f for f a function designator. |
| 2045 | /// - &s.xx => s |
| 2046 | /// - &s.zz[1].yy -> s, if zz is an array |
| 2047 | /// - *(x + 1) -> x, if x is an array |
| 2048 | /// - &"123"[2] -> 0 |
| 2049 | /// - & __real__ x -> x |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2050 | static ValueDecl *getPrimaryDecl(Expr *E) { |
| 2051 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2052 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2053 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2054 | case Stmt::MemberExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2055 | // Fields cannot be declared with a 'register' storage class. |
| 2056 | // &X->f is always ok, even if X is declared register. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2057 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2058 | return 0; |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2059 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2060 | case Stmt::ArraySubscriptExprClass: { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2061 | // &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] | 2062 | |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2063 | ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase()); |
Anders Carlsson | f2a4b84 | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 2064 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2065 | return 0; |
| 2066 | else |
| 2067 | return VD; |
| 2068 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 2069 | case Stmt::UnaryOperatorClass: { |
| 2070 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| 2071 | |
| 2072 | switch(UO->getOpcode()) { |
| 2073 | case UnaryOperator::Deref: { |
| 2074 | // *(X + 1) refers to X if X is not a pointer. |
| 2075 | ValueDecl *VD = getPrimaryDecl(UO->getSubExpr()); |
| 2076 | if (!VD || VD->getType()->isPointerType()) |
| 2077 | return 0; |
| 2078 | return VD; |
| 2079 | } |
| 2080 | case UnaryOperator::Real: |
| 2081 | case UnaryOperator::Imag: |
| 2082 | case UnaryOperator::Extension: |
| 2083 | return getPrimaryDecl(UO->getSubExpr()); |
| 2084 | default: |
| 2085 | return 0; |
| 2086 | } |
| 2087 | } |
| 2088 | case Stmt::BinaryOperatorClass: { |
| 2089 | BinaryOperator *BO = cast<BinaryOperator>(E); |
| 2090 | |
| 2091 | // Handle cases involving pointer arithmetic. The result of an |
| 2092 | // Assign or AddAssign is not an lvalue so they can be ignored. |
| 2093 | |
| 2094 | // (x + n) or (n + x) => x |
| 2095 | if (BO->getOpcode() == BinaryOperator::Add) { |
| 2096 | if (BO->getLHS()->getType()->isPointerType()) { |
| 2097 | return getPrimaryDecl(BO->getLHS()); |
| 2098 | } else if (BO->getRHS()->getType()->isPointerType()) { |
| 2099 | return getPrimaryDecl(BO->getRHS()); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | return 0; |
| 2104 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2106 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2107 | case Stmt::ImplicitCastExprClass: |
| 2108 | // &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] | 2109 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2110 | default: |
| 2111 | return 0; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 2116 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 2117 | /// object cannot be declared with storage class register or be a bit field. |
| 2118 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 2119 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| 2120 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 2121 | if (getLangOptions().C99) { |
| 2122 | // Implement C99-only parts of addressof rules. |
| 2123 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 2124 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 2125 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 2126 | // (assuming the deref expression is valid). |
| 2127 | return uOp->getSubExpr()->getType(); |
| 2128 | } |
| 2129 | // Technically, there should be a check for array subscript |
| 2130 | // expressions here, but the result of one is always an lvalue anyway. |
| 2131 | } |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 2132 | ValueDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 2133 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2134 | |
| 2135 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 2136 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 2137 | // FIXME: emit more specific diag... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2138 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 2139 | op->getSourceRange()); |
| 2140 | return QualType(); |
| 2141 | } |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 2142 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
| 2143 | if (MemExpr->getMemberDecl()->isBitField()) { |
| 2144 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2145 | std::string("bit-field"), op->getSourceRange()); |
| 2146 | return QualType(); |
| 2147 | } |
| 2148 | // Check for Apple extension for accessing vector components. |
| 2149 | } else if (isa<ArraySubscriptExpr>(op) && |
| 2150 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) { |
| 2151 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2152 | std::string("vector"), op->getSourceRange()); |
| 2153 | return QualType(); |
| 2154 | } else if (dcl) { // C99 6.5.3.2p1 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2155 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 2156 | // with the register storage-class specifier. |
| 2157 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 2158 | if (vd->getStorageClass() == VarDecl::Register) { |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 2159 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 2160 | std::string("register variable"), op->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2161 | return QualType(); |
| 2162 | } |
| 2163 | } else |
| 2164 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | } |
Chris Lattner | c36d405 | 2008-07-27 00:48:22 +0000 | [diff] [blame] | 2166 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2167 | // If the operand has type "type", the result has type "pointer to type". |
| 2168 | return Context.getPointerType(op->getType()); |
| 2169 | } |
| 2170 | |
| 2171 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2172 | UsualUnaryConversions(op); |
| 2173 | QualType qType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2174 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 2175 | if (const PointerType *PT = qType->getAsPointerType()) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 2176 | // Note that per both C89 and C99, this is always legal, even |
| 2177 | // if ptype is an incomplete type or void. |
| 2178 | // It would be possible to warn about dereferencing a |
| 2179 | // void pointer, but it's completely well-defined, |
| 2180 | // and such a warning is unlikely to catch any mistakes. |
| 2181 | return PT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2182 | } |
| 2183 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
| 2184 | qType.getAsString(), op->getSourceRange()); |
| 2185 | return QualType(); |
| 2186 | } |
| 2187 | |
| 2188 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 2189 | tok::TokenKind Kind) { |
| 2190 | BinaryOperator::Opcode Opc; |
| 2191 | switch (Kind) { |
| 2192 | default: assert(0 && "Unknown binop!"); |
| 2193 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 2194 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 2195 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 2196 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 2197 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 2198 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 2199 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 2200 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 2201 | case tok::less: Opc = BinaryOperator::LT; break; |
| 2202 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 2203 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 2204 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 2205 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 2206 | case tok::amp: Opc = BinaryOperator::And; break; |
| 2207 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 2208 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 2209 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 2210 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 2211 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 2212 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 2213 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 2214 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 2215 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 2216 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 2217 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 2218 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 2219 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 2220 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 2221 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 2222 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 2223 | } |
| 2224 | return Opc; |
| 2225 | } |
| 2226 | |
| 2227 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 2228 | tok::TokenKind Kind) { |
| 2229 | UnaryOperator::Opcode Opc; |
| 2230 | switch (Kind) { |
| 2231 | default: assert(0 && "Unknown unary op!"); |
| 2232 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 2233 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 2234 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 2235 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 2236 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 2237 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 2238 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 2239 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 2240 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 2241 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 2242 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 2243 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 2244 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 2245 | } |
| 2246 | return Opc; |
| 2247 | } |
| 2248 | |
| 2249 | // Binary Operators. 'Tok' is the token for the operator. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2250 | Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2251 | ExprTy *LHS, ExprTy *RHS) { |
| 2252 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 2253 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 2254 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2255 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 2256 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | |
| 2258 | QualType ResultTy; // Result type of the binary operator. |
| 2259 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 2260 | |
| 2261 | switch (Opc) { |
| 2262 | default: |
| 2263 | assert(0 && "Unknown binary expr!"); |
| 2264 | case BinaryOperator::Assign: |
| 2265 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
| 2266 | break; |
| 2267 | case BinaryOperator::Mul: |
| 2268 | case BinaryOperator::Div: |
| 2269 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 2270 | break; |
| 2271 | case BinaryOperator::Rem: |
| 2272 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 2273 | break; |
| 2274 | case BinaryOperator::Add: |
| 2275 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 2276 | break; |
| 2277 | case BinaryOperator::Sub: |
| 2278 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 2279 | break; |
| 2280 | case BinaryOperator::Shl: |
| 2281 | case BinaryOperator::Shr: |
| 2282 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 2283 | break; |
| 2284 | case BinaryOperator::LE: |
| 2285 | case BinaryOperator::LT: |
| 2286 | case BinaryOperator::GE: |
| 2287 | case BinaryOperator::GT: |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2288 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2289 | break; |
| 2290 | case BinaryOperator::EQ: |
| 2291 | case BinaryOperator::NE: |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 2292 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | break; |
| 2294 | case BinaryOperator::And: |
| 2295 | case BinaryOperator::Xor: |
| 2296 | case BinaryOperator::Or: |
| 2297 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 2298 | break; |
| 2299 | case BinaryOperator::LAnd: |
| 2300 | case BinaryOperator::LOr: |
| 2301 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
| 2302 | break; |
| 2303 | case BinaryOperator::MulAssign: |
| 2304 | case BinaryOperator::DivAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2305 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2306 | if (!CompTy.isNull()) |
| 2307 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2308 | break; |
| 2309 | case BinaryOperator::RemAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2310 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2311 | if (!CompTy.isNull()) |
| 2312 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2313 | break; |
| 2314 | case BinaryOperator::AddAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2315 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2316 | if (!CompTy.isNull()) |
| 2317 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2318 | break; |
| 2319 | case BinaryOperator::SubAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2320 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2321 | if (!CompTy.isNull()) |
| 2322 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2323 | break; |
| 2324 | case BinaryOperator::ShlAssign: |
| 2325 | case BinaryOperator::ShrAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2326 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2327 | if (!CompTy.isNull()) |
| 2328 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2329 | break; |
| 2330 | case BinaryOperator::AndAssign: |
| 2331 | case BinaryOperator::XorAssign: |
| 2332 | case BinaryOperator::OrAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 2333 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2334 | if (!CompTy.isNull()) |
| 2335 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 2336 | break; |
| 2337 | case BinaryOperator::Comma: |
| 2338 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
| 2339 | break; |
| 2340 | } |
| 2341 | if (ResultTy.isNull()) |
| 2342 | return true; |
| 2343 | if (CompTy.isNull()) |
Chris Lattner | 17d1b2a | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 2344 | return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2345 | else |
Chris Lattner | 17d1b2a | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 2346 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | // Unary Operators. 'Tok' is the token for the operator. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2350 | Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2351 | ExprTy *input) { |
| 2352 | Expr *Input = (Expr*)input; |
| 2353 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 2354 | QualType resultType; |
| 2355 | switch (Opc) { |
| 2356 | default: |
| 2357 | assert(0 && "Unimplemented unary expr!"); |
| 2358 | case UnaryOperator::PreInc: |
| 2359 | case UnaryOperator::PreDec: |
| 2360 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
| 2361 | break; |
| 2362 | case UnaryOperator::AddrOf: |
| 2363 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 2364 | break; |
| 2365 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 2366 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2367 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 2368 | break; |
| 2369 | case UnaryOperator::Plus: |
| 2370 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2371 | UsualUnaryConversions(Input); |
| 2372 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2373 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 2374 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2375 | resultType.getAsString()); |
| 2376 | break; |
| 2377 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2378 | UsualUnaryConversions(Input); |
| 2379 | resultType = Input->getType(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 2380 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 2381 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 2382 | // C99 does not support '~' for complex conjugation. |
| 2383 | Diag(OpLoc, diag::ext_integer_complement_complex, |
| 2384 | resultType.getAsString(), Input->getSourceRange()); |
| 2385 | else if (!resultType->isIntegerType()) |
| 2386 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2387 | resultType.getAsString(), Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | case UnaryOperator::LNot: // logical negation |
| 2390 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2391 | DefaultFunctionArrayConversion(Input); |
| 2392 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2393 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 2394 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 2395 | resultType.getAsString()); |
| 2396 | // LNot always has type int. C99 6.5.3.3p5. |
| 2397 | resultType = Context.IntTy; |
| 2398 | break; |
| 2399 | case UnaryOperator::SizeOf: |
Chris Lattner | bb280a4 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 2400 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, |
| 2401 | Input->getSourceRange(), true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | case UnaryOperator::AlignOf: |
Chris Lattner | bb280a4 | 2008-07-25 21:45:37 +0000 | [diff] [blame] | 2404 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, |
| 2405 | Input->getSourceRange(), false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2406 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2407 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2408 | case UnaryOperator::Imag: |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 2409 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2410 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2411 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2412 | resultType = Input->getType(); |
| 2413 | break; |
| 2414 | } |
| 2415 | if (resultType.isNull()) |
| 2416 | return true; |
| 2417 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
| 2418 | } |
| 2419 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2420 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 2421 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2422 | SourceLocation LabLoc, |
| 2423 | IdentifierInfo *LabelII) { |
| 2424 | // Look up the record for this label identifier. |
| 2425 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 2426 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 2427 | // If we haven't seen this label yet, create a forward reference. It |
| 2428 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2429 | if (LabelDecl == 0) |
| 2430 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 2431 | |
| 2432 | // 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] | 2433 | return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 2434 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2437 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 2438 | SourceLocation RPLoc) { // "({..})" |
| 2439 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 2440 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 2441 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 2442 | |
| 2443 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 2444 | // example, it is not possible to goto into a stmt expression apparently. |
| 2445 | // More semantic analysis is needed. |
| 2446 | |
| 2447 | // FIXME: the last statement in the compount stmt has its value used. We |
| 2448 | // should not warn about it being unused. |
| 2449 | |
| 2450 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 2451 | // as the type of the stmtexpr. |
| 2452 | QualType Ty = Context.VoidTy; |
| 2453 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2454 | if (!Compound->body_empty()) { |
| 2455 | Stmt *LastStmt = Compound->body_back(); |
| 2456 | // If LastStmt is a label, skip down through into the body. |
| 2457 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 2458 | LastStmt = Label->getSubStmt(); |
| 2459 | |
| 2460 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 2461 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2462 | } |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 2463 | |
| 2464 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 2465 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2466 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2467 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc, |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2468 | SourceLocation TypeLoc, |
| 2469 | TypeTy *argty, |
| 2470 | OffsetOfComponent *CompPtr, |
| 2471 | unsigned NumComponents, |
| 2472 | SourceLocation RPLoc) { |
| 2473 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 2474 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 2475 | |
| 2476 | // We must have at least one component that refers to the type, and the first |
| 2477 | // one is known to be a field designator. Verify that the ArgTy represents |
| 2478 | // a struct/union/class. |
| 2479 | if (!ArgTy->isRecordType()) |
| 2480 | return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString()); |
| 2481 | |
| 2482 | // Otherwise, create a compound literal expression as the base, and |
| 2483 | // iteratively process the offsetof designators. |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2484 | Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2485 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 2486 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 2487 | // GCC extension, diagnose them. |
| 2488 | if (NumComponents != 1) |
| 2489 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator, |
| 2490 | SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd)); |
| 2491 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2492 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 2493 | const OffsetOfComponent &OC = CompPtr[i]; |
| 2494 | if (OC.isBrackets) { |
| 2495 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2496 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2497 | if (!AT) { |
| 2498 | delete Res; |
| 2499 | return Diag(OC.LocEnd, diag::err_offsetof_array_type, |
| 2500 | Res->getType().getAsString()); |
| 2501 | } |
| 2502 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2503 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 2504 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2505 | // C99 6.5.2.1p1 |
| 2506 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 2507 | if (!Idx->getType()->isIntegerType()) |
| 2508 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript, |
| 2509 | Idx->getSourceRange()); |
| 2510 | |
| 2511 | Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd); |
| 2512 | continue; |
| 2513 | } |
| 2514 | |
| 2515 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 2516 | if (!RC) { |
| 2517 | delete Res; |
| 2518 | return Diag(OC.LocEnd, diag::err_offsetof_record_type, |
| 2519 | Res->getType().getAsString()); |
| 2520 | } |
| 2521 | |
| 2522 | // Get the decl corresponding to this. |
| 2523 | RecordDecl *RD = RC->getDecl(); |
| 2524 | FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo); |
| 2525 | if (!MemberDecl) |
| 2526 | return Diag(BuiltinLoc, diag::err_typecheck_no_member, |
| 2527 | OC.U.IdentInfo->getName(), |
| 2528 | SourceRange(OC.LocStart, OC.LocEnd)); |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2529 | |
| 2530 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 2531 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2532 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 2533 | // matter here. |
| 2534 | Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType()); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(), |
| 2538 | BuiltinLoc); |
| 2539 | } |
| 2540 | |
| 2541 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2542 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2543 | TypeTy *arg1, TypeTy *arg2, |
| 2544 | SourceLocation RPLoc) { |
| 2545 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 2546 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 2547 | |
| 2548 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 2549 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2550 | return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2553 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 2554 | ExprTy *expr1, ExprTy *expr2, |
| 2555 | SourceLocation RPLoc) { |
| 2556 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 2557 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 2558 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 2559 | |
| 2560 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 2561 | |
| 2562 | // The conditional expression is required to be a constant expression. |
| 2563 | llvm::APSInt condEval(32); |
| 2564 | SourceLocation ExpLoc; |
| 2565 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 2566 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant, |
| 2567 | CondExpr->getSourceRange()); |
| 2568 | |
| 2569 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 2570 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 2571 | RHSExpr->getType(); |
| 2572 | return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc); |
| 2573 | } |
| 2574 | |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2575 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2576 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2577 | /// The number of arguments has already been validated to match the number of |
| 2578 | /// arguments in FnType. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2579 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType, |
| 2580 | ASTContext &Context) { |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2581 | unsigned NumParams = FnType->getNumArgs(); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2582 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2583 | QualType ExprTy = Context.getCanonicalType(Args[i]->getType()); |
| 2584 | QualType ParmTy = Context.getCanonicalType(FnType->getArgType(i)); |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType()) |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2587 | return false; |
Nate Begeman | d6595fa | 2008-04-18 23:35:14 +0000 | [diff] [blame] | 2588 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2589 | return true; |
| 2590 | } |
| 2591 | |
| 2592 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 2593 | SourceLocation *CommaLocs, |
| 2594 | SourceLocation BuiltinLoc, |
| 2595 | SourceLocation RParenLoc) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2596 | // __builtin_overload requires at least 2 arguments |
| 2597 | if (NumArgs < 2) |
| 2598 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2599 | SourceRange(BuiltinLoc, RParenLoc)); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2600 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2601 | // The first argument is required to be a constant expression. It tells us |
| 2602 | // 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] | 2603 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2604 | Expr *NParamsExpr = Args[0]; |
| 2605 | llvm::APSInt constEval(32); |
| 2606 | SourceLocation ExpLoc; |
| 2607 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
| 2608 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2609 | NParamsExpr->getSourceRange()); |
| 2610 | |
| 2611 | // Verify that the number of parameters is > 0 |
| 2612 | unsigned NumParams = constEval.getZExtValue(); |
| 2613 | if (NumParams == 0) |
| 2614 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2615 | NParamsExpr->getSourceRange()); |
| 2616 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 2617 | if ((NumParams + 1) > NumArgs) |
| 2618 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2619 | SourceRange(BuiltinLoc, RParenLoc)); |
| 2620 | |
| 2621 | // 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] | 2622 | // listed after the parameters. |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2623 | OverloadExpr *OE = 0; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2624 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 2625 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 2626 | // pointer to function. |
| 2627 | Expr *Fn = UsualUnaryConversions(Args[i]); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2628 | const FunctionTypeProto *FnType = 0; |
| 2629 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) |
| 2630 | FnType = PT->getPointeeType()->getAsFunctionTypeProto(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2631 | |
| 2632 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 2633 | // parameters, and the number of parameters must match the value passed to |
| 2634 | // the builtin. |
| 2635 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2636 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype, |
| 2637 | Fn->getSourceRange()); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2638 | |
| 2639 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2640 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2641 | // If they match, return a new OverloadExpr. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 2642 | if (ExprsMatchFnType(Args+1, FnType, Context)) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2643 | if (OE) |
| 2644 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match, |
| 2645 | OE->getFn()->getSourceRange()); |
| 2646 | // Remember our match, and continue processing the remaining arguments |
| 2647 | // to catch any errors. |
| 2648 | OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(), |
| 2649 | BuiltinLoc, RParenLoc); |
| 2650 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2651 | } |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2652 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 2653 | // exactly one of the candidate functions. |
| 2654 | if (OE) |
| 2655 | return OE; |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2656 | |
| 2657 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 2658 | // the return an error. |
| 2659 | std::string typeNames; |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2660 | for (unsigned i = 0; i != NumParams; ++i) { |
| 2661 | if (i != 0) typeNames += ", "; |
| 2662 | typeNames += Args[i+1]->getType().getAsString(); |
| 2663 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2664 | |
| 2665 | return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames, |
| 2666 | SourceRange(BuiltinLoc, RParenLoc)); |
| 2667 | } |
| 2668 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2669 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 2670 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2671 | SourceLocation RPLoc) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2672 | Expr *E = static_cast<Expr*>(expr); |
| 2673 | QualType T = QualType::getFromOpaquePtr(type); |
| 2674 | |
| 2675 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 2676 | |
| 2677 | // Get the va_list type |
| 2678 | QualType VaListType = Context.getBuiltinVaListType(); |
| 2679 | // Deal with implicit array decay; for example, on x86-64, |
| 2680 | // va_list is an array, but it's supposed to decay to |
| 2681 | // a pointer for va_arg. |
| 2682 | if (VaListType->isArrayType()) |
| 2683 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | efbe85c | 2008-08-20 22:17:17 +0000 | [diff] [blame] | 2684 | // Make sure the input expression also decays appropriately. |
| 2685 | UsualUnaryConversions(E); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 2686 | |
| 2687 | if (CheckAssignmentConstraints(VaListType, E->getType()) != Compatible) |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2688 | return Diag(E->getLocStart(), |
| 2689 | diag::err_first_argument_to_va_arg_not_of_type_va_list, |
| 2690 | E->getType().getAsString(), |
| 2691 | E->getSourceRange()); |
| 2692 | |
| 2693 | // FIXME: Warn if a non-POD type is passed in. |
| 2694 | |
| 2695 | return new VAArgExpr(BuiltinLoc, E, T, RPLoc); |
| 2696 | } |
| 2697 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2698 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 2699 | SourceLocation Loc, |
| 2700 | QualType DstType, QualType SrcType, |
| 2701 | Expr *SrcExpr, const char *Flavor) { |
| 2702 | // Decode the result (notice that AST's are still created for extensions). |
| 2703 | bool isInvalid = false; |
| 2704 | unsigned DiagKind; |
| 2705 | switch (ConvTy) { |
| 2706 | default: assert(0 && "Unknown conversion type"); |
| 2707 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2708 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2709 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 2710 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2711 | case IntToPointer: |
| 2712 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 2713 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2714 | case IncompatiblePointer: |
| 2715 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 2716 | break; |
| 2717 | case FunctionVoidPointer: |
| 2718 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 2719 | break; |
| 2720 | case CompatiblePointerDiscardsQualifiers: |
| 2721 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 2722 | break; |
| 2723 | case Incompatible: |
| 2724 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 2725 | isInvalid = true; |
| 2726 | break; |
| 2727 | } |
| 2728 | |
| 2729 | Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor, |
| 2730 | SrcExpr->getSourceRange()); |
| 2731 | return isInvalid; |
| 2732 | } |