Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 29 | /// \brief Determine whether the use of this declaration is valid, and |
| 30 | /// emit any corresponding diagnostics. |
| 31 | /// |
| 32 | /// This routine diagnoses various problems with referencing |
| 33 | /// declarations that can occur when using a declaration. For example, |
| 34 | /// it might warn if a deprecated or unavailable declaration is being |
| 35 | /// used, or produce an error (and return true) if a C++0x deleted |
| 36 | /// function is being used. |
| 37 | /// |
| 38 | /// \returns true if there was an error (this declaration cannot be |
| 39 | /// referenced), false otherwise. |
| 40 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 41 | // See if the decl is deprecated. |
| 42 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 43 | // Implementing deprecated stuff requires referencing deprecated |
| 44 | // stuff. Don't warn if we are implementing a deprecated |
| 45 | // construct. |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 46 | bool isSilenced = false; |
| 47 | |
| 48 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 49 | // If this reference happens *in* a deprecated function or method, don't |
| 50 | // warn. |
| 51 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
| 52 | |
| 53 | // If this is an Objective-C method implementation, check to see if the |
| 54 | // method was deprecated on the declaration, not the definition. |
| 55 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 56 | // The semantic decl context of a ObjCMethodDecl is the |
| 57 | // ObjCImplementationDecl. |
| 58 | if (ObjCImplementationDecl *Impl |
| 59 | = dyn_cast<ObjCImplementationDecl>(MD->getParent())) { |
| 60 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 61 | MD = Impl->getClassInterface()->getMethod(Context, |
| 62 | MD->getSelector(), |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 63 | MD->isInstanceMethod()); |
| 64 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!isSilenced) |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 70 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 71 | } |
| 72 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 73 | // See if this is a deleted function. |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 74 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 75 | if (FD->isDeleted()) { |
| 76 | Diag(Loc, diag::err_deleted_function_use); |
| 77 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 78 | return true; |
| 79 | } |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 80 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 81 | |
| 82 | // See if the decl is unavailable |
| 83 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 84 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 86 | } |
| 87 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 91 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
| 92 | /// (and other functions in future), which have been declared with sentinel |
| 93 | /// attribute. It warns if call does not have the sentinel argument. |
| 94 | /// |
| 95 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
| 96 | Expr **Args, unsigned NumArgs) |
| 97 | { |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 98 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
| 99 | if (!attr) |
| 100 | return; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 101 | int sentinelPos = attr->getSentinel(); |
| 102 | int nullPos = attr->getNullPos(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 103 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 104 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 105 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 106 | unsigned int i = 0; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 107 | bool warnNotEnoughArgs = false; |
| 108 | int isMethod = 0; |
| 109 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 110 | // skip over named parameters. |
| 111 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 112 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 113 | if (nullPos) |
| 114 | --nullPos; |
| 115 | else |
| 116 | ++i; |
| 117 | } |
| 118 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 119 | isMethod = 1; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 120 | } |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 121 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 122 | // skip over named parameters. |
| 123 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 124 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 125 | if (nullPos) |
| 126 | --nullPos; |
| 127 | else |
| 128 | ++i; |
| 129 | } |
| 130 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 131 | } |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 132 | else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
| 133 | // block or function pointer call. |
| 134 | QualType Ty = V->getType(); |
| 135 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| 136 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 137 | ? Ty->getAsPointerType()->getPointeeType()->getAsFunctionType() |
| 138 | : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType(); |
| 139 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 140 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 141 | unsigned k; |
| 142 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 143 | if (nullPos) |
| 144 | --nullPos; |
| 145 | else |
| 146 | ++i; |
| 147 | } |
| 148 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 149 | } |
| 150 | if (Ty->isBlockPointerType()) |
| 151 | isMethod = 2; |
| 152 | } |
| 153 | else |
| 154 | return; |
| 155 | } |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 156 | else |
| 157 | return; |
| 158 | |
| 159 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 160 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 161 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | int sentinel = i; |
| 165 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 166 | --sentinelPos; |
| 167 | ++i; |
| 168 | } |
| 169 | if (sentinelPos > 0) { |
| 170 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 171 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | while (i < NumArgs-1) { |
| 175 | ++i; |
| 176 | ++sentinel; |
| 177 | } |
| 178 | Expr *sentinelExpr = Args[sentinel]; |
| 179 | if (sentinelExpr && (!sentinelExpr->getType()->isPointerType() || |
| 180 | !sentinelExpr->isNullPointerConstant(Context))) { |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 181 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 182 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 183 | } |
| 184 | return; |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 187 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 188 | Expr *Ex = (Expr *)E; |
| 189 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 190 | } |
| 191 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 192 | //===----------------------------------------------------------------------===// |
| 193 | // Standard Promotions and Conversions |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 196 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 197 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 198 | QualType Ty = E->getType(); |
| 199 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 200 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 201 | if (Ty->isFunctionType()) |
| 202 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 203 | else if (Ty->isArrayType()) { |
| 204 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 205 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 206 | // type 'array of type' is converted to an expression that has type 'pointer |
| 207 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 208 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 209 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 210 | // |
| 211 | // C++ 4.2p1: |
| 212 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 213 | // T" can be converted to an rvalue of type "pointer to T". |
| 214 | // |
| 215 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 216 | E->isLvalue(Context) == Expr::LV_Valid) |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 217 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 218 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 221 | /// \brief Whether this is a promotable bitfield reference according |
| 222 | /// to C99 6.3.1.1p2, bullet 2. |
| 223 | /// |
| 224 | /// \returns the type this bit-field will promote to, or NULL if no |
| 225 | /// promotion occurs. |
| 226 | static QualType isPromotableBitField(Expr *E, ASTContext &Context) { |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 227 | FieldDecl *Field = E->getBitField(); |
| 228 | if (!Field) |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 229 | return QualType(); |
| 230 | |
| 231 | const BuiltinType *BT = Field->getType()->getAsBuiltinType(); |
| 232 | if (!BT) |
| 233 | return QualType(); |
| 234 | |
| 235 | if (BT->getKind() != BuiltinType::Bool && |
| 236 | BT->getKind() != BuiltinType::Int && |
| 237 | BT->getKind() != BuiltinType::UInt) |
| 238 | return QualType(); |
| 239 | |
| 240 | llvm::APSInt BitWidthAP; |
| 241 | if (!Field->getBitWidth()->isIntegerConstantExpr(BitWidthAP, Context)) |
| 242 | return QualType(); |
| 243 | |
| 244 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 245 | uint64_t IntSize = Context.getTypeSize(Context.IntTy); |
| 246 | if (BitWidth < IntSize || |
| 247 | (Field->getType()->isSignedIntegerType() && BitWidth == IntSize)) |
| 248 | return Context.IntTy; |
| 249 | |
| 250 | if (BitWidth == IntSize && Field->getType()->isUnsignedIntegerType()) |
| 251 | return Context.UnsignedIntTy; |
| 252 | |
| 253 | return QualType(); |
| 254 | } |
| 255 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 256 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 257 | /// operators (C99 6.3). The conversions of array and function types are |
| 258 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 259 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 260 | /// In these instances, this routine should *not* be called. |
| 261 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 262 | QualType Ty = Expr->getType(); |
| 263 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 264 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 265 | // C99 6.3.1.1p2: |
| 266 | // |
| 267 | // The following may be used in an expression wherever an int or |
| 268 | // unsigned int may be used: |
| 269 | // - an object or expression with an integer type whose integer |
| 270 | // conversion rank is less than or equal to the rank of int |
| 271 | // and unsigned int. |
| 272 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 273 | // |
| 274 | // If an int can represent all values of the original type, the |
| 275 | // value is converted to an int; otherwise, it is converted to an |
| 276 | // unsigned int. These are called the integer promotions. All |
| 277 | // other types are unchanged by the integer promotions. |
| 278 | if (Ty->isPromotableIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | ImpCastExprToType(Expr, Context.IntTy); |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 280 | return Expr; |
| 281 | } else { |
| 282 | QualType T = isPromotableBitField(Expr, Context); |
| 283 | if (!T.isNull()) { |
| 284 | ImpCastExprToType(Expr, T); |
| 285 | return Expr; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 290 | return Expr; |
| 291 | } |
| 292 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 293 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 294 | /// do not have a prototype. Arguments that have type float are promoted to |
| 295 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 296 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 297 | QualType Ty = Expr->getType(); |
| 298 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 299 | |
| 300 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 301 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 302 | if (BT->getKind() == BuiltinType::Float) |
| 303 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 304 | |
| 305 | UsualUnaryConversions(Expr); |
| 306 | } |
| 307 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 308 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 309 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 310 | /// interfaces passed by value. This returns true if the argument type is |
| 311 | /// completely illegal. |
| 312 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 313 | DefaultArgumentPromotion(Expr); |
| 314 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 315 | if (Expr->getType()->isObjCInterfaceType()) { |
| 316 | Diag(Expr->getLocStart(), |
| 317 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 318 | << Expr->getType() << CT; |
| 319 | return true; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 321 | |
| 322 | if (!Expr->getType()->isPODType()) |
| 323 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 324 | << Expr->getType() << CT; |
| 325 | |
| 326 | return false; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 330 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 331 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 332 | /// routine returns the first non-arithmetic type found. The client is |
| 333 | /// responsible for emitting appropriate error diagnostics. |
| 334 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 335 | /// GCC. |
| 336 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 337 | bool isCompAssign) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 338 | if (!isCompAssign) |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 339 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 340 | |
| 341 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 343 | // For conversion purposes, we ignore any qualifiers. |
| 344 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 345 | QualType lhs = |
| 346 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 347 | QualType rhs = |
| 348 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 349 | |
| 350 | // If both types are identical, no conversion is needed. |
| 351 | if (lhs == rhs) |
| 352 | return lhs; |
| 353 | |
| 354 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 355 | // The caller can deal with this (e.g. pointer + int). |
| 356 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 357 | return lhs; |
| 358 | |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 359 | // Perform bitfield promotions. |
| 360 | QualType LHSBitfieldPromoteTy = isPromotableBitField(lhsExpr, Context); |
| 361 | if (!LHSBitfieldPromoteTy.isNull()) |
| 362 | lhs = LHSBitfieldPromoteTy; |
| 363 | QualType RHSBitfieldPromoteTy = isPromotableBitField(rhsExpr, Context); |
| 364 | if (!RHSBitfieldPromoteTy.isNull()) |
| 365 | rhs = RHSBitfieldPromoteTy; |
| 366 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 367 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 368 | if (!isCompAssign) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 369 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 370 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 371 | return destType; |
| 372 | } |
| 373 | |
| 374 | QualType Sema::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 375 | // Perform the usual unary conversions. We do this early so that |
| 376 | // integral promotions to "int" can allow us to exit early, in the |
| 377 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 378 | // qualifiers. For example, "const float" and "float" are |
| 379 | // equivalent. |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 380 | if (lhs->isPromotableIntegerType()) |
| 381 | lhs = Context.IntTy; |
| 382 | else |
| 383 | lhs = lhs.getUnqualifiedType(); |
| 384 | if (rhs->isPromotableIntegerType()) |
| 385 | rhs = Context.IntTy; |
| 386 | else |
| 387 | rhs = rhs.getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 389 | // If both types are identical, no conversion is needed. |
| 390 | if (lhs == rhs) |
| 391 | return lhs; |
| 392 | |
| 393 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 394 | // The caller can deal with this (e.g. pointer + int). |
| 395 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 396 | return lhs; |
| 397 | |
| 398 | // At this point, we have two different arithmetic types. |
| 399 | |
| 400 | // Handle complex types first (C99 6.3.1.8p1). |
| 401 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 402 | // if we have an integer operand, the result is the complex type. |
| 403 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
| 404 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 405 | return lhs; |
| 406 | } |
| 407 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
| 408 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 409 | return rhs; |
| 410 | } |
| 411 | // This handles complex/complex, complex/float, or float/complex. |
| 412 | // When both operands are complex, the shorter operand is converted to the |
| 413 | // type of the longer, and that is the type of the result. This corresponds |
| 414 | // to what is done when combining two real floating-point operands. |
| 415 | // The fun begins when size promotion occur across type domains. |
| 416 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 417 | // floating-point type, the less precise type is converted, within it's |
| 418 | // real or complex domain, to the precision of the other type. For example, |
| 419 | // when combining a "long double" with a "double _Complex", the |
| 420 | // "double _Complex" is promoted to "long double _Complex". |
| 421 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
| 422 | |
| 423 | if (result > 0) { // The left side is bigger, convert rhs. |
| 424 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 425 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 426 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 427 | } |
| 428 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 429 | // domains match. This is a requirement for our implementation, C99 |
| 430 | // does not require this promotion. |
| 431 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 432 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 433 | return rhs; |
| 434 | } else { // handle "_Complex double, double". |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 435 | return lhs; |
| 436 | } |
| 437 | } |
| 438 | return lhs; // The domain/size match exactly. |
| 439 | } |
| 440 | // Now handle "real" floating types (i.e. float, double, long double). |
| 441 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 442 | // if we have an integer operand, the result is the real floating type. |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 443 | if (rhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 444 | // convert rhs to the lhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 445 | return lhs; |
| 446 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 447 | if (rhs->isComplexIntegerType()) { |
| 448 | // convert rhs to the complex floating point type. |
| 449 | return Context.getComplexType(lhs); |
| 450 | } |
| 451 | if (lhs->isIntegerType()) { |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 452 | // convert lhs to the rhs floating point type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 453 | return rhs; |
| 454 | } |
Anders Carlsson | 488a079 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 455 | if (lhs->isComplexIntegerType()) { |
| 456 | // convert lhs to the complex floating point type. |
| 457 | return Context.getComplexType(rhs); |
| 458 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 459 | // We have two real floating types, float/complex combos were handled above. |
| 460 | // Convert the smaller operand to the bigger result. |
| 461 | int result = Context.getFloatingTypeOrder(lhs, rhs); |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 462 | if (result > 0) // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 463 | return lhs; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 464 | assert(result < 0 && "illegal float comparison"); |
| 465 | return rhs; // convert the lhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 466 | } |
| 467 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 468 | // Handle GCC complex int extension. |
| 469 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 470 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 471 | |
| 472 | if (lhsComplexInt && rhsComplexInt) { |
| 473 | if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 474 | rhsComplexInt->getElementType()) >= 0) |
| 475 | return lhs; // convert the rhs |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 476 | return rhs; |
| 477 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 478 | // convert the rhs to the lhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 479 | return lhs; |
| 480 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 481 | // convert the lhs to the rhs complex type. |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 482 | return rhs; |
| 483 | } |
| 484 | } |
| 485 | // Finally, we have two differing integer types. |
| 486 | // The rules for this case are in C99 6.3.1.8 |
| 487 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 488 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 489 | rhsSigned = rhs->isSignedIntegerType(); |
| 490 | QualType destType; |
| 491 | if (lhsSigned == rhsSigned) { |
| 492 | // Same signedness; use the higher-ranked type |
| 493 | destType = compare >= 0 ? lhs : rhs; |
| 494 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 495 | // The unsigned type has greater than or equal rank to the |
| 496 | // signed type, so use the unsigned type |
| 497 | destType = lhsSigned ? rhs : lhs; |
| 498 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 499 | // The two types are different widths; if we are here, that |
| 500 | // means the signed type is larger than the unsigned type, so |
| 501 | // use the signed type. |
| 502 | destType = lhsSigned ? lhs : rhs; |
| 503 | } else { |
| 504 | // The signed type is higher-ranked than the unsigned type, |
| 505 | // but isn't actually any bigger (like unsigned int and long |
| 506 | // on most 32-bit systems). Use the unsigned type corresponding |
| 507 | // to the signed type. |
| 508 | destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 509 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 510 | return destType; |
| 511 | } |
| 512 | |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | // Semantic Analysis for various Expression Types |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | |
| 517 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 518 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 519 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 520 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 521 | /// multiple tokens. However, the common case is that StringToks points to one |
| 522 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 523 | /// |
| 524 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 525 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 526 | assert(NumStringToks && "Must have at least one string!"); |
| 527 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 528 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 529 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 530 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 531 | |
| 532 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 533 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 534 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 535 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 536 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 537 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 538 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 539 | |
| 540 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 541 | if (getLangOptions().CPlusPlus) |
| 542 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 543 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 544 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 545 | // the nul terminator character as well as the string length for pascal |
| 546 | // strings. |
| 547 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 548 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 549 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 551 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 552 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 553 | Literal.GetStringLength(), |
| 554 | Literal.AnyWide, StrTy, |
| 555 | &StringTokLocs[0], |
| 556 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 559 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 560 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 561 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 562 | /// for values inside the block or for globals). |
| 563 | /// |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 564 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 565 | /// up-to-date. |
| 566 | /// |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 567 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 568 | ValueDecl *VD) { |
| 569 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 570 | // we wanted to. |
| 571 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 572 | return false; |
| 573 | |
| 574 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 575 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 576 | return false; |
| 577 | |
| 578 | // If this is a reference to an extern, static, or global variable, no need to |
| 579 | // snapshot it. |
| 580 | // FIXME: What about 'const' variables in C++? |
| 581 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 582 | if (!Var->hasLocalStorage()) |
| 583 | return false; |
| 584 | |
| 585 | // Blocks that have these can't be constant. |
| 586 | CurBlock->hasBlockDeclRefExprs = true; |
| 587 | |
| 588 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 589 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 590 | // be defined outside all of the current blocks (in which case the blocks do |
| 591 | // all get the bit). Walk the nesting chain. |
| 592 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 593 | NextBlock = NextBlock->PrevBlockInfo) { |
| 594 | // If we found the defining block for the variable, don't mark the block as |
| 595 | // having a reference outside it. |
| 596 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 597 | break; |
| 598 | |
| 599 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 600 | // a snapshot as well. |
| 601 | NextBlock->hasBlockDeclRefExprs = true; |
| 602 | } |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 603 | |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 609 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 610 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 611 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 612 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 613 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 614 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 615 | IdentifierInfo &II, |
| 616 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 617 | const CXXScopeSpec *SS, |
| 618 | bool isAddressOfOperand) { |
| 619 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 620 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 623 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 624 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 625 | /// nested-name-specifier. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 626 | DeclRefExpr * |
| 627 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 628 | bool TypeDependent, bool ValueDependent, |
| 629 | const CXXScopeSpec *SS) { |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 630 | if (SS && !SS->isEmpty()) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 631 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
| 632 | ValueDependent, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 633 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 634 | } else |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 635 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 638 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 639 | /// variable corresponding to the anonymous union or struct whose type |
| 640 | /// is Record. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 641 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 642 | RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | assert(Record->isAnonymousStructOrUnion() && |
| 644 | "Record must be an anonymous struct or union!"); |
| 645 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 646 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 647 | // operation rather than a slow walk through DeclContext's vector (which |
| 648 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 649 | DeclContext *Ctx = Record->getDeclContext(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 650 | for (DeclContext::decl_iterator D = Ctx->decls_begin(Context), |
| 651 | DEnd = Ctx->decls_end(Context); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 652 | D != DEnd; ++D) { |
| 653 | if (*D == Record) { |
| 654 | // The object for the anonymous struct/union directly |
| 655 | // follows its type in the list of declarations. |
| 656 | ++D; |
| 657 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 658 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 659 | return *D; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | assert(false && "Missing object for anonymous record"); |
| 664 | return 0; |
| 665 | } |
| 666 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 667 | /// \brief Given a field that represents a member of an anonymous |
| 668 | /// struct/union, build the path from that field's context to the |
| 669 | /// actual member. |
| 670 | /// |
| 671 | /// Construct the sequence of field member references we'll have to |
| 672 | /// perform to get to the field in the anonymous union/struct. The |
| 673 | /// list of members is built from the field outward, so traverse it |
| 674 | /// backwards to go from an object in the current context to the field |
| 675 | /// we found. |
| 676 | /// |
| 677 | /// \returns The variable from which the field access should begin, |
| 678 | /// for an anonymous struct/union that is not a member of another |
| 679 | /// class. Otherwise, returns NULL. |
| 680 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 681 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 682 | assert(Field->getDeclContext()->isRecord() && |
| 683 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 684 | && "Field must be stored inside an anonymous struct or union"); |
| 685 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 686 | Path.push_back(Field); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 687 | VarDecl *BaseObject = 0; |
| 688 | DeclContext *Ctx = Field->getDeclContext(); |
| 689 | do { |
| 690 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 691 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 692 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 693 | Path.push_back(AnonField); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 694 | else { |
| 695 | BaseObject = cast<VarDecl>(AnonObject); |
| 696 | break; |
| 697 | } |
| 698 | Ctx = Ctx->getParent(); |
| 699 | } while (Ctx->isRecord() && |
| 700 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 701 | |
| 702 | return BaseObject; |
| 703 | } |
| 704 | |
| 705 | Sema::OwningExprResult |
| 706 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 707 | FieldDecl *Field, |
| 708 | Expr *BaseObjectExpr, |
| 709 | SourceLocation OpLoc) { |
| 710 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 711 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
| 712 | AnonFields); |
| 713 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 714 | // Build the expression that refers to the base object, from |
| 715 | // which we will build a sequence of member references to each |
| 716 | // of the anonymous union objects and, eventually, the field we |
| 717 | // found via name lookup. |
| 718 | bool BaseObjectIsPointer = false; |
| 719 | unsigned ExtraQuals = 0; |
| 720 | if (BaseObject) { |
| 721 | // BaseObject is an anonymous struct/union variable (and is, |
| 722 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 723 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 724 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 725 | SourceLocation()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 726 | ExtraQuals |
| 727 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 728 | } else if (BaseObjectExpr) { |
| 729 | // The caller provided the base object expression. Determine |
| 730 | // whether its a pointer and whether it adds any qualifiers to the |
| 731 | // anonymous struct/union fields we're looking into. |
| 732 | QualType ObjectType = BaseObjectExpr->getType(); |
| 733 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 734 | BaseObjectIsPointer = true; |
| 735 | ObjectType = ObjectPtr->getPointeeType(); |
| 736 | } |
| 737 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 738 | } else { |
| 739 | // We've found a member of an anonymous struct/union that is |
| 740 | // inside a non-anonymous struct/union, so in a well-formed |
| 741 | // program our base object expression is "this". |
| 742 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 743 | if (!MD->isStatic()) { |
| 744 | QualType AnonFieldType |
| 745 | = Context.getTagDeclType( |
| 746 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 747 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 748 | if ((Context.getCanonicalType(AnonFieldType) |
| 749 | == Context.getCanonicalType(ThisType)) || |
| 750 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 751 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 752 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 753 | MD->getThisType(Context)); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 754 | BaseObjectIsPointer = true; |
| 755 | } |
| 756 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 757 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 758 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 759 | } |
| 760 | ExtraQuals = MD->getTypeQualifiers(); |
| 761 | } |
| 762 | |
| 763 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 764 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 765 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // Build the implicit member references to the field of the |
| 769 | // anonymous struct/union. |
| 770 | Expr *Result = BaseObjectExpr; |
| 771 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 772 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 773 | FI != FIEnd; ++FI) { |
| 774 | QualType MemberType = (*FI)->getType(); |
| 775 | if (!(*FI)->isMutable()) { |
| 776 | unsigned combinedQualifiers |
| 777 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 778 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 779 | } |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 780 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 781 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 782 | BaseObjectIsPointer = false; |
| 783 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 786 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 789 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 790 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 791 | /// performs lookup on that name and returns an expression that refers |
| 792 | /// to that name. This routine isn't directly called from the parser, |
| 793 | /// because the parser doesn't know about DeclarationName. Rather, |
| 794 | /// this routine is called by ActOnIdentifierExpr, |
| 795 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 796 | /// which form the DeclarationName from the corresponding syntactic |
| 797 | /// forms. |
| 798 | /// |
| 799 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 800 | /// function call context. LookupCtx is only used for a C++ |
| 801 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 802 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 803 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 804 | /// isAddressOfOperand means that this expression is the direct operand |
| 805 | /// of an address-of operator. This matters because this is the only |
| 806 | /// situation where a qualified name referencing a non-static member may |
| 807 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 808 | Sema::OwningExprResult |
| 809 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 810 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 811 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 812 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 813 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 814 | if (SS && SS->isInvalid()) |
| 815 | return ExprError(); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 816 | |
| 817 | // C++ [temp.dep.expr]p3: |
| 818 | // An id-expression is type-dependent if it contains: |
| 819 | // -- a nested-name-specifier that contains a class-name that |
| 820 | // names a dependent type. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 821 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 822 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 823 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
| 824 | Loc, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 825 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()))); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 828 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 829 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 830 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 831 | if (Lookup.isAmbiguous()) { |
| 832 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 833 | SS && SS->isSet() ? SS->getRange() |
| 834 | : SourceRange()); |
| 835 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | NamedDecl *D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 839 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 840 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 841 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 842 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 843 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 844 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 845 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 846 | // found a decl, but that decl is outside the current instance method (i.e. |
| 847 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 848 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 849 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 850 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 851 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 852 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 853 | ClassDeclared)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 854 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 855 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 856 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 857 | |
| 858 | // If we're referencing an invalid decl, just return this as a silent |
| 859 | // error node. The error diagnostic was already emitted on the decl. |
| 860 | if (IV->isInvalidDecl()) |
| 861 | return ExprError(); |
| 862 | |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 863 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 864 | // If a class method attemps to use a free standing ivar, this is |
| 865 | // an error. |
| 866 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 867 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 868 | << IV->getDeclName()); |
| 869 | // If a class method uses a global variable, even if an ivar with |
| 870 | // same name exists, use the global. |
| 871 | if (!IsClsMethod) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 872 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 873 | ClassDeclared != IFace) |
| 874 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 875 | // FIXME: This should use a new expr for a direct reference, don't |
| 876 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 877 | IdentifierInfo &II = Context.Idents.get("self"); |
| 878 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Daniel Dunbar | f5254bd | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 879 | return Owned(new (Context) |
| 880 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 881 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 882 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 885 | else if (getCurMethodDecl()->isInstanceMethod()) { |
| 886 | // We should warn if a local variable hides an ivar. |
| 887 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 888 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 889 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 890 | ClassDeclared)) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 891 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 892 | IFace == ClassDeclared) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 893 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 894 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 895 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 896 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 897 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 898 | QualType T; |
| 899 | |
| 900 | if (getCurMethodDecl()->isInstanceMethod()) |
| 901 | T = Context.getPointerType(Context.getObjCInterfaceType( |
| 902 | getCurMethodDecl()->getClassInterface())); |
| 903 | else |
| 904 | T = Context.getObjCClassType(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 905 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 906 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 907 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 909 | // Determine whether this name might be a candidate for |
| 910 | // argument-dependent lookup. |
| 911 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 912 | HasTrailingLParen; |
| 913 | |
| 914 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 915 | // We've seen something of the form |
| 916 | // |
| 917 | // identifier( |
| 918 | // |
| 919 | // and we did not find any entity by the name |
| 920 | // "identifier". However, this identifier is still subject to |
| 921 | // argument-dependent lookup, so keep track of the name. |
| 922 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 923 | Context.OverloadTy, |
| 924 | Loc)); |
| 925 | } |
| 926 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 927 | if (D == 0) { |
| 928 | // Otherwise, this could be an implicitly declared function reference (legal |
| 929 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 930 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 931 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 932 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 933 | else { |
| 934 | // If this name wasn't predeclared and if this is not a function call, |
| 935 | // diagnose the problem. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 936 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 937 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 938 | << Name << SS->getRange()); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 939 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 940 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 941 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 942 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 943 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 944 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 947 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 948 | // If this is an expression of the form &Class::member, don't build an |
| 949 | // implicit member ref, because we want a pointer to the member in general, |
| 950 | // not any specific instance's member. |
| 951 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 952 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 953 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 954 | QualType DType; |
| 955 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 956 | DType = FD->getType().getNonReferenceType(); |
| 957 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 958 | DType = Method->getType(); |
| 959 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 960 | DType = Context.OverloadTy; |
| 961 | } |
| 962 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 963 | if (!DType.isNull()) { |
| 964 | // The pointer is type- and value-dependent if it points into something |
| 965 | // dependent. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 966 | bool Dependent = DC->isDependentContext(); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 967 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 972 | // We may have found a field within an anonymous union or struct |
| 973 | // (C++ [class.union]). |
| 974 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 975 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 976 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 977 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 978 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 979 | if (!MD->isStatic()) { |
| 980 | // C++ [class.mfct.nonstatic]p2: |
| 981 | // [...] if name lookup (3.4.1) resolves the name in the |
| 982 | // id-expression to a nonstatic nontype member of class X or of |
| 983 | // a base class of X, the id-expression is transformed into a |
| 984 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 985 | // as the postfix-expression to the left of the '.' operator. |
| 986 | DeclContext *Ctx = 0; |
| 987 | QualType MemberType; |
| 988 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 989 | Ctx = FD->getDeclContext(); |
| 990 | MemberType = FD->getType(); |
| 991 | |
| 992 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 993 | MemberType = RefType->getPointeeType(); |
| 994 | else if (!FD->isMutable()) { |
| 995 | unsigned combinedQualifiers |
| 996 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 997 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 998 | } |
| 999 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 1000 | if (!Method->isStatic()) { |
| 1001 | Ctx = Method->getParent(); |
| 1002 | MemberType = Method->getType(); |
| 1003 | } |
| 1004 | } else if (OverloadedFunctionDecl *Ovl |
| 1005 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 1006 | for (OverloadedFunctionDecl::function_iterator |
| 1007 | Func = Ovl->function_begin(), |
| 1008 | FuncEnd = Ovl->function_end(); |
| 1009 | Func != FuncEnd; ++Func) { |
| 1010 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 1011 | if (!DMethod->isStatic()) { |
| 1012 | Ctx = Ovl->getDeclContext(); |
| 1013 | MemberType = Context.OverloadTy; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1018 | |
| 1019 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1020 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 1021 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 1022 | if ((Context.getCanonicalType(CtxType) |
| 1023 | == Context.getCanonicalType(ThisType)) || |
| 1024 | IsDerivedFrom(ThisType, CtxType)) { |
| 1025 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1026 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1027 | MD->getThisType(Context)); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1028 | return Owned(new (Context) MemberExpr(This, true, D, |
Eli Friedman | 1653e23 | 2009-04-29 17:56:47 +0000 | [diff] [blame] | 1029 | Loc, MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1035 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1036 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 1037 | if (MD->isStatic()) |
| 1038 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1039 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 1040 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1043 | // Any other ways we could have found the field in a well-formed |
| 1044 | // program would have been turned into implicit member expressions |
| 1045 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1046 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1047 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1048 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1050 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1051 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1052 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1053 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1054 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1055 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1056 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1057 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1058 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1059 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 1060 | false, false, SS)); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1061 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 1062 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 1063 | false, false, SS)); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1064 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1066 | // Check whether this declaration can be used. Note that we suppress |
| 1067 | // this check when we're going to perform argument-dependent lookup |
| 1068 | // on this function name, because this might not be the function |
| 1069 | // that overload resolution actually selects. |
| 1070 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 1071 | return ExprError(); |
| 1072 | |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1073 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 1074 | // Warn about constructs like: |
| 1075 | // if (void *X = foo()) { ... } else { X }. |
| 1076 | // In the else block, the pointer is always false. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 1077 | |
| 1078 | // FIXME: In a template instantiation, we don't have scope |
| 1079 | // information to check this property. |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1080 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1081 | Scope *CheckS = S; |
| 1082 | while (CheckS) { |
| 1083 | if (CheckS->isWithinElse() && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1084 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1085 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1086 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 1087 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1088 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1089 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 1090 | << Var->getDeclName()); |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | |
| 1094 | // Move up one more control parent to check again. |
| 1095 | CheckS = CheckS->getControlParent(); |
| 1096 | if (CheckS) |
| 1097 | CheckS = CheckS->getParent(); |
| 1098 | } |
| 1099 | } |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1100 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) { |
| 1101 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1102 | // C99 DR 316 says that, if a function type comes from a |
| 1103 | // function definition (without a prototype), that type is only |
| 1104 | // used for checking compatibility. Therefore, when referencing |
| 1105 | // the function, we pretend that we don't have the full function |
| 1106 | // type. |
| 1107 | QualType T = Func->getType(); |
| 1108 | QualType NoProtoType = T; |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1109 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 1110 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
Douglas Gregor | 1f88aa7 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1111 | return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS)); |
| 1112 | } |
Douglas Gregor | 48840c7 | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1113 | } |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1114 | |
| 1115 | // Only create DeclRefExpr's for valid Decl's. |
| 1116 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1117 | return ExprError(); |
| 1118 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1119 | // If the identifier reference is inside a block, and it refers to a value |
| 1120 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1121 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1122 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1123 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1124 | // We do not do this for things like enum constants, global variables, etc, |
| 1125 | // as they do not get snapshotted. |
| 1126 | // |
| 1127 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1128 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1129 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1130 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1131 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1132 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1133 | // Variable will be bound by-copy, make it const within the closure. |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1134 | ExprTy.addConst(); |
| 1135 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1136 | } |
| 1137 | // If this reference is not in a block or if the referenced variable is |
| 1138 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1140 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1141 | bool ValueDependent = false; |
| 1142 | if (getLangOptions().CPlusPlus) { |
| 1143 | // C++ [temp.dep.expr]p3: |
| 1144 | // An id-expression is type-dependent if it contains: |
| 1145 | // - an identifier that was declared with a dependent type, |
| 1146 | if (VD->getType()->isDependentType()) |
| 1147 | TypeDependent = true; |
| 1148 | // - FIXME: a template-id that is dependent, |
| 1149 | // - a conversion-function-id that specifies a dependent type, |
| 1150 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1151 | Name.getCXXNameType()->isDependentType()) |
| 1152 | TypeDependent = true; |
| 1153 | // - a nested-name-specifier that contains a class-name that |
| 1154 | // names a dependent type. |
| 1155 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1156 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1157 | DC; DC = DC->getParent()) { |
| 1158 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1159 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1160 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1161 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1162 | TypeDependent = true; |
| 1163 | break; |
| 1164 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1169 | // C++ [temp.dep.constexpr]p2: |
| 1170 | // |
| 1171 | // An identifier is value-dependent if it is: |
| 1172 | // - a name declared with a dependent type, |
| 1173 | if (TypeDependent) |
| 1174 | ValueDependent = true; |
| 1175 | // - the name of a non-type template parameter, |
| 1176 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1177 | ValueDependent = true; |
| 1178 | // - a constant with integral or enumeration type and is |
| 1179 | // initialized with an expression that is value-dependent |
| 1180 | // (FIXME!). |
| 1181 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1182 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1183 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1184 | TypeDependent, ValueDependent, SS)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1187 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1188 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1189 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1191 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1192 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1193 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1194 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1195 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1196 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1198 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1199 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1200 | unsigned Length; |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1201 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1202 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1203 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1204 | Length = MD->getSynthesizedMethodSize(); |
| 1205 | else { |
| 1206 | Diag(Loc, diag::ext_predef_outside_function); |
| 1207 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 1208 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 1209 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1210 | |
| 1211 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1212 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1213 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1214 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1215 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1218 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1219 | llvm::SmallString<16> CharBuffer; |
| 1220 | CharBuffer.resize(Tok.getLength()); |
| 1221 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1222 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1224 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1225 | Tok.getLocation(), PP); |
| 1226 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1227 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1228 | |
| 1229 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1230 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1231 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1232 | Literal.isWide(), |
| 1233 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1236 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1237 | // Fast path for a single digit (which is quite common). A single digit |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1238 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1239 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1240 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1241 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1242 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1243 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1244 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1246 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1247 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1248 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1249 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1250 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1251 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1252 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1253 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1254 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1255 | Tok.getLocation(), PP); |
| 1256 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1257 | return ExprError(); |
| 1258 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1259 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1260 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1261 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1262 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1263 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1264 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1265 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1266 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1267 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1268 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1269 | |
| 1270 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1271 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1272 | // isExact will be set by GetFloatValue(). |
| 1273 | bool isExact = false; |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1274 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1275 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1277 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1278 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1279 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1280 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1281 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1282 | // long long is a C99 feature. |
| 1283 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1284 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1285 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1286 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1287 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1288 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1290 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1291 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1292 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1293 | Ty = Context.UnsignedLongLongTy; |
| 1294 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1295 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1296 | } else { |
| 1297 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1298 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1300 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1301 | // be an unsigned int. |
| 1302 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1303 | |
| 1304 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1305 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1306 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1307 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1308 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1310 | // Does it fit in a unsigned int? |
| 1311 | if (ResultVal.isIntN(IntSize)) { |
| 1312 | // Does it fit in a signed int? |
| 1313 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1314 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1315 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1316 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1317 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1318 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1319 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1321 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1322 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1323 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1325 | // Does it fit in a unsigned long? |
| 1326 | if (ResultVal.isIntN(LongSize)) { |
| 1327 | // Does it fit in a signed long? |
| 1328 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1329 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1330 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1331 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1332 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1333 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1336 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1337 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1338 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1340 | // Does it fit in a unsigned long long? |
| 1341 | if (ResultVal.isIntN(LongLongSize)) { |
| 1342 | // Does it fit in a signed long long? |
| 1343 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1344 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1345 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1346 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1347 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1351 | // If we still couldn't decide a type, we probably have something that |
| 1352 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1353 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1354 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1355 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1356 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1357 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1359 | if (ResultVal.getBitWidth() != Width) |
| 1360 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1361 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1362 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1363 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1364 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1365 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1366 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1367 | Res = new (Context) ImaginaryLiteral(Res, |
| 1368 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1369 | |
| 1370 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1373 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1374 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1375 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1376 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1377 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1381 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1382 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1383 | SourceLocation OpLoc, |
| 1384 | const SourceRange &ExprRange, |
| 1385 | bool isSizeof) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1386 | if (exprType->isDependentType()) |
| 1387 | return false; |
| 1388 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1389 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1390 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1391 | // alignof(function) is allowed as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1392 | if (isSizeof) |
| 1393 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1394 | return false; |
| 1395 | } |
| 1396 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1397 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1398 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1399 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1400 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1401 | return false; |
| 1402 | } |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1404 | if (RequireCompleteType(OpLoc, exprType, |
| 1405 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1406 | diag::err_alignof_incomplete_type, |
| 1407 | ExprRange)) |
| 1408 | return true; |
| 1409 | |
| 1410 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | bf2b095 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1411 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1412 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1413 | << exprType << isSizeof << ExprRange; |
| 1414 | return true; |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1417 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1420 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1421 | const SourceRange &ExprRange) { |
| 1422 | E = E->IgnoreParens(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1424 | // alignof decl is always ok. |
| 1425 | if (isa<DeclRefExpr>(E)) |
| 1426 | return false; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Cannot know anything else if the expression is dependent. |
| 1429 | if (E->isTypeDependent()) |
| 1430 | return false; |
| 1431 | |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1432 | if (E->getBitField()) { |
| 1433 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1434 | return true; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1435 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1436 | |
| 1437 | // Alignment of a field access is always okay, so long as it isn't a |
| 1438 | // bit-field. |
| 1439 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 1440 | if (dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 1441 | return false; |
| 1442 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1443 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1444 | } |
| 1445 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1446 | /// \brief Build a sizeof or alignof expression given a type operand. |
| 1447 | Action::OwningExprResult |
| 1448 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
| 1449 | bool isSizeOf, SourceRange R) { |
| 1450 | if (T.isNull()) |
| 1451 | return ExprError(); |
| 1452 | |
| 1453 | if (!T->isDependentType() && |
| 1454 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1455 | return ExprError(); |
| 1456 | |
| 1457 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1458 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1459 | Context.getSizeType(), OpLoc, |
| 1460 | R.getEnd())); |
| 1461 | } |
| 1462 | |
| 1463 | /// \brief Build a sizeof or alignof expression given an expression |
| 1464 | /// operand. |
| 1465 | Action::OwningExprResult |
| 1466 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1467 | bool isSizeOf, SourceRange R) { |
| 1468 | // Verify that the operand is valid. |
| 1469 | bool isInvalid = false; |
| 1470 | if (E->isTypeDependent()) { |
| 1471 | // Delay type-checking for type-dependent expressions. |
| 1472 | } else if (!isSizeOf) { |
| 1473 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1474 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1475 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1476 | isInvalid = true; |
| 1477 | } else { |
| 1478 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1479 | } |
| 1480 | |
| 1481 | if (isInvalid) |
| 1482 | return ExprError(); |
| 1483 | |
| 1484 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1485 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1486 | Context.getSizeType(), OpLoc, |
| 1487 | R.getEnd())); |
| 1488 | } |
| 1489 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1490 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1491 | /// the same for @c alignof and @c __alignof |
| 1492 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1493 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1494 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1495 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1496 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1497 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1498 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1499 | if (isType) { |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1500 | QualType ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1501 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
| 1502 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1503 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1504 | // Get the end location. |
| 1505 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1506 | Action::OwningExprResult Result |
| 1507 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1508 | |
| 1509 | if (Result.isInvalid()) |
| 1510 | DeleteExpr(ArgEx); |
| 1511 | |
| 1512 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1515 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1516 | if (V->isTypeDependent()) |
| 1517 | return Context.DependentTy; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1519 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1520 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1521 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1522 | |
| 1523 | // Otherwise they pass through real integer and floating point types here. |
| 1524 | if (V->getType()->isArithmeticType()) |
| 1525 | return V->getType(); |
| 1526 | |
| 1527 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1528 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1529 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1530 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1534 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1535 | Action::OwningExprResult |
| 1536 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1537 | tok::TokenKind Kind, ExprArg Input) { |
| 1538 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1539 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1540 | UnaryOperator::Opcode Opc; |
| 1541 | switch (Kind) { |
| 1542 | default: assert(0 && "Unknown unary op!"); |
| 1543 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1544 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1545 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1547 | if (getLangOptions().CPlusPlus && |
| 1548 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1549 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1550 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1551 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1552 | |
| 1553 | // C++ [over.inc]p1: |
| 1554 | // |
| 1555 | // [...] If the function is a member function with one |
| 1556 | // parameter (which shall be of type int) or a non-member |
| 1557 | // function with two parameters (the second of which shall be |
| 1558 | // of type int), it defines the postfix increment operator ++ |
| 1559 | // for objects of that type. When the postfix increment is |
| 1560 | // called as a result of using the ++ operator, the int |
| 1561 | // argument will have value zero. |
| 1562 | Expr *Args[2] = { |
| 1563 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1564 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1565 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1566 | }; |
| 1567 | |
| 1568 | // Build the candidate set for overloading |
| 1569 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1570 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Perform overload resolution. |
| 1573 | OverloadCandidateSet::iterator Best; |
| 1574 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1575 | case OR_Success: { |
| 1576 | // We found a built-in operator or an overloaded operator. |
| 1577 | FunctionDecl *FnDecl = Best->Function; |
| 1578 | |
| 1579 | if (FnDecl) { |
| 1580 | // We matched an overloaded operator. Build a call to that |
| 1581 | // operator. |
| 1582 | |
| 1583 | // Convert the arguments. |
| 1584 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1585 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1586 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1587 | } else { |
| 1588 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1589 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1590 | FnDecl->getParamDecl(0)->getType(), |
| 1591 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1592 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1596 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1597 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1598 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1600 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1601 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 6d8e573 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1602 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1603 | UsualUnaryConversions(FnExpr); |
| 1604 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1605 | Input.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1606 | Args[0] = Arg; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1607 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
| 1608 | Args, 2, ResultTy, |
| 1609 | OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | // We matched a built-in operator. Convert the arguments, then |
| 1612 | // break out so that we will build the appropriate built-in |
| 1613 | // operator node. |
| 1614 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1615 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1616 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1617 | |
| 1618 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1619 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | case OR_No_Viable_Function: |
| 1623 | // No viable function; fall through to handling this as a |
| 1624 | // built-in operator, which will produce an error message for us. |
| 1625 | break; |
| 1626 | |
| 1627 | case OR_Ambiguous: |
| 1628 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1629 | << UnaryOperator::getOpcodeStr(Opc) |
| 1630 | << Arg->getSourceRange(); |
| 1631 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1632 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1633 | |
| 1634 | case OR_Deleted: |
| 1635 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1636 | << Best->Function->isDeleted() |
| 1637 | << UnaryOperator::getOpcodeStr(Opc) |
| 1638 | << Arg->getSourceRange(); |
| 1639 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1640 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | // Either we found no viable overloaded operator or we matched a |
| 1644 | // built-in operator. In either case, fall through to trying to |
| 1645 | // build a built-in operation. |
| 1646 | } |
| 1647 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1648 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1649 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1650 | if (result.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1651 | return ExprError(); |
| 1652 | Input.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1653 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1656 | Action::OwningExprResult |
| 1657 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1658 | ExprArg Idx, SourceLocation RLoc) { |
| 1659 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1660 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1661 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1662 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | de72f3e | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1663 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1664 | Base.release(); |
| 1665 | Idx.release(); |
| 1666 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1667 | Context.DependentTy, RLoc)); |
| 1668 | } |
| 1669 | |
| 1670 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1671 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1672 | LHSExp->getType()->isEnumeralType() || |
| 1673 | RHSExp->getType()->isRecordType() || |
| 1674 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1675 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1676 | // to the candidate set. |
| 1677 | OverloadCandidateSet CandidateSet; |
| 1678 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1679 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1680 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1681 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1682 | // Perform overload resolution. |
| 1683 | OverloadCandidateSet::iterator Best; |
| 1684 | switch (BestViableFunction(CandidateSet, Best)) { |
| 1685 | case OR_Success: { |
| 1686 | // We found a built-in operator or an overloaded operator. |
| 1687 | FunctionDecl *FnDecl = Best->Function; |
| 1688 | |
| 1689 | if (FnDecl) { |
| 1690 | // We matched an overloaded operator. Build a call to that |
| 1691 | // operator. |
| 1692 | |
| 1693 | // Convert the arguments. |
| 1694 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1695 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1696 | PerformCopyInitialization(RHSExp, |
| 1697 | FnDecl->getParamDecl(0)->getType(), |
| 1698 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1699 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | // Convert the arguments. |
| 1702 | if (PerformCopyInitialization(LHSExp, |
| 1703 | FnDecl->getParamDecl(0)->getType(), |
| 1704 | "passing") || |
| 1705 | PerformCopyInitialization(RHSExp, |
| 1706 | FnDecl->getParamDecl(1)->getType(), |
| 1707 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1708 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1712 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1713 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1714 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1715 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1716 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1717 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1718 | SourceLocation()); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1719 | UsualUnaryConversions(FnExpr); |
| 1720 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1721 | Base.release(); |
| 1722 | Idx.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1723 | Args[0] = LHSExp; |
| 1724 | Args[1] = RHSExp; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1725 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 1726 | FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1727 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1728 | } else { |
| 1729 | // We matched a built-in operator. Convert the arguments, then |
| 1730 | // break out so that we will build the appropriate built-in |
| 1731 | // operator node. |
| 1732 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1733 | "passing") || |
| 1734 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1735 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1736 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1737 | |
| 1738 | break; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | case OR_No_Viable_Function: |
| 1743 | // No viable function; fall through to handling this as a |
| 1744 | // built-in operator, which will produce an error message for us. |
| 1745 | break; |
| 1746 | |
| 1747 | case OR_Ambiguous: |
| 1748 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1749 | << "[]" |
| 1750 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1751 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1752 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1753 | |
| 1754 | case OR_Deleted: |
| 1755 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1756 | << Best->Function->isDeleted() |
| 1757 | << "[]" |
| 1758 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1759 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1760 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | // Either we found no viable overloaded operator or we matched a |
| 1764 | // built-in operator. In either case, fall through to trying to |
| 1765 | // build a built-in operation. |
| 1766 | } |
| 1767 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1768 | // Perform default conversions. |
| 1769 | DefaultFunctionArrayConversion(LHSExp); |
| 1770 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1771 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1772 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1773 | |
| 1774 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1775 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1776 | // in the subscript position. As a result, we need to derive the array base |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1777 | // and index from the expression types. |
| 1778 | Expr *BaseExpr, *IndexExpr; |
| 1779 | QualType ResultType; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1780 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1781 | BaseExpr = LHSExp; |
| 1782 | IndexExpr = RHSExp; |
| 1783 | ResultType = Context.DependentTy; |
| 1784 | } else if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1785 | BaseExpr = LHSExp; |
| 1786 | IndexExpr = RHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1787 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1788 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1789 | // Handle the uncommon case of "123[Ptr]". |
| 1790 | BaseExpr = RHSExp; |
| 1791 | IndexExpr = LHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1792 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1793 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1794 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1795 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1796 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1797 | // FIXME: need to deal with const... |
| 1798 | ResultType = VTy->getElementType(); |
Eli Friedman | d461407 | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1799 | } else if (LHSTy->isArrayType()) { |
| 1800 | // If we see an array that wasn't promoted by |
| 1801 | // DefaultFunctionArrayConversion, it must be an array that |
| 1802 | // wasn't promoted because of the C90 rule that doesn't |
| 1803 | // allow promoting non-lvalue arrays. Warn, then |
| 1804 | // force the promotion here. |
| 1805 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1806 | LHSExp->getSourceRange(); |
| 1807 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy)); |
| 1808 | LHSTy = LHSExp->getType(); |
| 1809 | |
| 1810 | BaseExpr = LHSExp; |
| 1811 | IndexExpr = RHSExp; |
| 1812 | ResultType = LHSTy->getAsPointerType()->getPointeeType(); |
| 1813 | } else if (RHSTy->isArrayType()) { |
| 1814 | // Same as previous, except for 123[f().a] case |
| 1815 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1816 | RHSExp->getSourceRange(); |
| 1817 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy)); |
| 1818 | RHSTy = RHSExp->getType(); |
| 1819 | |
| 1820 | BaseExpr = RHSExp; |
| 1821 | IndexExpr = LHSExp; |
| 1822 | ResultType = RHSTy->getAsPointerType()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1823 | } else { |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1824 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1825 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1826 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1827 | // C99 6.5.2.1p1 |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1828 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1829 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1830 | << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1832 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| 1833 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1834 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| 1835 | // incomplete types are not object types. |
| 1836 | if (ResultType->isFunctionType()) { |
| 1837 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1838 | << ResultType << BaseExpr->getSourceRange(); |
| 1839 | return ExprError(); |
| 1840 | } |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1842 | if (!ResultType->isDependentType() && |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1843 | RequireCompleteType(LLoc, ResultType, diag::err_subscript_incomplete_type, |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1844 | BaseExpr->getSourceRange())) |
| 1845 | return ExprError(); |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1846 | |
| 1847 | // Diagnose bad cases where we step over interface counts. |
| 1848 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1849 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1850 | << ResultType << BaseExpr->getSourceRange(); |
| 1851 | return ExprError(); |
| 1852 | } |
| 1853 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1854 | Base.release(); |
| 1855 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1856 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1857 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1860 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1861 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1862 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1863 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1864 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1865 | // The vector accessor can't exceed the number of elements. |
| 1866 | const char *compStr = CompName.getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1867 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1868 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1869 | // special names that indicate a subset of exactly half the elements are |
| 1870 | // to be selected. |
| 1871 | bool HalvingSwizzle = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1872 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1873 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1874 | // indicating that it is a string of hex values to be used as vector indices. |
| 1875 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Check that we've found one of the special components, or that the component |
| 1878 | // names must come from the same set. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1879 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1880 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1881 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1882 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1883 | do |
| 1884 | compStr++; |
| 1885 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1886 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1887 | do |
| 1888 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1889 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1890 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1891 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1892 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1893 | // We didn't get to the end of the string. This means the component names |
| 1894 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1895 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1896 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1897 | return QualType(); |
| 1898 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1899 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1900 | // Ensure no component accessor exceeds the width of the vector type it |
| 1901 | // operates on. |
| 1902 | if (!HalvingSwizzle) { |
| 1903 | compStr = CompName.getName(); |
| 1904 | |
| 1905 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1906 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1907 | |
| 1908 | while (*compStr) { |
| 1909 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1910 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1911 | << baseType << SourceRange(CompLoc); |
| 1912 | return QualType(); |
| 1913 | } |
| 1914 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1915 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1916 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1917 | // If this is a halving swizzle, verify that the base type has an even |
| 1918 | // number of elements. |
| 1919 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1920 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1921 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1922 | return QualType(); |
| 1923 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1924 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1925 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1926 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1927 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1928 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1929 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1930 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1931 | : CompName.getLength(); |
| 1932 | if (HexSwizzle) |
| 1933 | CompSize--; |
| 1934 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1935 | if (CompSize == 1) |
| 1936 | return vecType->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1937 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1938 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1939 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1940 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1941 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1942 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1943 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1944 | } |
| 1945 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1948 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 1949 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1950 | const Selector &Sel, |
| 1951 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1953 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Context, &Member)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1954 | return PD; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1955 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Context, Sel)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1956 | return OMD; |
| 1957 | |
| 1958 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1959 | E = PDecl->protocol_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1960 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
| 1961 | Context)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1962 | return D; |
| 1963 | } |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static Decl *FindGetterNameDecl(const ObjCQualifiedIdType *QIdTy, |
| 1968 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1969 | const Selector &Sel, |
| 1970 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1971 | // Check protocols on qualified interfaces. |
| 1972 | Decl *GDecl = 0; |
| 1973 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
| 1974 | E = QIdTy->qual_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1975 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, &Member)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1976 | GDecl = PD; |
| 1977 | break; |
| 1978 | } |
| 1979 | // Also must look for a getter name which uses property syntax. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1980 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Context, Sel)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1981 | GDecl = OMD; |
| 1982 | break; |
| 1983 | } |
| 1984 | } |
| 1985 | if (!GDecl) { |
| 1986 | for (ObjCQualifiedIdType::qual_iterator I = QIdTy->qual_begin(), |
| 1987 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1988 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1989 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1990 | if (GDecl) |
| 1991 | return GDecl; |
| 1992 | } |
| 1993 | } |
| 1994 | return GDecl; |
| 1995 | } |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1996 | |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1997 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 1998 | /// all base class implementations. |
| 1999 | /// |
| 2000 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 2001 | const ObjCInterfaceDecl *IFace, |
| 2002 | const Selector &Sel) { |
| 2003 | ObjCMethodDecl *Method = 0; |
Douglas Gregor | afd5eb3 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 2004 | if (ObjCImplementationDecl *ImpDecl |
| 2005 | = LookupObjCImplementation(IFace->getIdentifier())) |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2006 | Method = ImpDecl->getInstanceMethod(Context, Sel); |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2007 | |
| 2008 | if (!Method && IFace->getSuperClass()) |
| 2009 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 2010 | return Method; |
| 2011 | } |
| 2012 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2013 | Action::OwningExprResult |
| 2014 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2015 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2016 | IdentifierInfo &Member, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2017 | DeclPtrTy ObjCImpDecl) { |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2018 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2019 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2020 | |
| 2021 | // Perform default conversions. |
| 2022 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2023 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2024 | QualType BaseType = BaseExpr->getType(); |
| 2025 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2027 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2028 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2029 | if (OpKind == tok::arrow) { |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2030 | if (BaseType->isDependentType()) |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2031 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2032 | BaseExpr, true, |
| 2033 | OpLoc, |
| 2034 | DeclarationName(&Member), |
| 2035 | MemberLoc)); |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2036 | else if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2037 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 7f3fec5 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 2038 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2039 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 2040 | MemberLoc, Member)); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2041 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2042 | return ExprError(Diag(MemberLoc, |
| 2043 | diag::err_typecheck_member_reference_arrow) |
| 2044 | << BaseType << BaseExpr->getSourceRange()); |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2045 | } else { |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2046 | if (BaseType->isDependentType()) { |
| 2047 | // Require that the base type isn't a pointer type |
| 2048 | // (so we'll report an error for) |
| 2049 | // T* t; |
| 2050 | // t.f; |
| 2051 | // |
| 2052 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2053 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2054 | // allows this, while still reporting an error if T is a struct pointer. |
| 2055 | const PointerType *PT = BaseType->getAsPointerType(); |
| 2056 | |
| 2057 | if (!PT || (getLangOptions().ObjC1 && |
| 2058 | !PT->getPointeeType()->isRecordType())) |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2059 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2060 | BaseExpr, false, |
| 2061 | OpLoc, |
| 2062 | DeclarationName(&Member), |
| 2063 | MemberLoc)); |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2064 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2065 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2066 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2067 | // Handle field access to simple records. This also handles access to fields |
| 2068 | // of the ObjC 'id' struct. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2069 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2070 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2071 | if (RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2072 | diag::err_typecheck_incomplete_tag, |
| 2073 | BaseExpr->getSourceRange())) |
| 2074 | return ExprError(); |
| 2075 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2076 | // The record definition is complete, now make sure the member is valid. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2077 | // FIXME: Qualified name lookup for C++ is a bit more complicated than this. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2078 | LookupResult Result |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2079 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 2080 | LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2082 | if (!Result) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2083 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 2084 | << &Member << BaseExpr->getSourceRange()); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2085 | if (Result.isAmbiguous()) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2086 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 2087 | MemberLoc, BaseExpr->getSourceRange()); |
| 2088 | return ExprError(); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2093 | // If the decl being referenced had an error, return an error for this |
| 2094 | // sub-expr without emitting another error, in order to avoid cascading |
| 2095 | // error cases. |
| 2096 | if (MemberDecl->isInvalidDecl()) |
| 2097 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2098 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2099 | // Check the use of this field |
| 2100 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 2101 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2103 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2104 | // We may have found a field within an anonymous union or struct |
| 2105 | // (C++ [class.union]). |
| 2106 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2107 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2108 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2110 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2111 | // FIXME: Handle address space modifiers |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2112 | QualType MemberType = FD->getType(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2113 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 2114 | MemberType = Ref->getPointeeType(); |
| 2115 | else { |
| 2116 | unsigned combinedQualifiers = |
| 2117 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2118 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2119 | combinedQualifiers &= ~QualType::Const; |
| 2120 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 2121 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2122 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2123 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 2124 | MemberLoc, MemberType)); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2128 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2129 | Var, MemberLoc, |
| 2130 | Var->getType().getNonReferenceType())); |
| 2131 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2132 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2133 | MemberFn, MemberLoc, |
| 2134 | MemberFn->getType())); |
| 2135 | if (OverloadedFunctionDecl *Ovl |
| 2136 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2137 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2138 | MemberLoc, Context.OverloadTy)); |
| 2139 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2140 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 2141 | Enum, MemberLoc, Enum->getType())); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2142 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2143 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2144 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2146 | // We found a declaration kind that we didn't expect. This is a |
| 2147 | // generic error message that tells the user that she can't refer |
| 2148 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2149 | return ExprError(Diag(MemberLoc, |
| 2150 | diag::err_typecheck_member_reference_unknown) |
| 2151 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2152 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2153 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2154 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2155 | // (*Obj).ivar. |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2156 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2157 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2158 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(Context, |
| 2159 | &Member, |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2160 | ClassDeclared)) { |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2161 | // If the decl being referenced had an error, return an error for this |
| 2162 | // sub-expr without emitting another error, in order to avoid cascading |
| 2163 | // error cases. |
| 2164 | if (IV->isInvalidDecl()) |
| 2165 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Check whether we can reference this field. |
| 2168 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2169 | return ExprError(); |
Steve Naroff | 8c56ee0 | 2009-03-26 16:01:08 +0000 | [diff] [blame] | 2170 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2171 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2172 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2173 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2174 | ClassOfMethodDecl = MD->getClassInterface(); |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2175 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2176 | // Case of a c-function declared inside an objc implementation. |
| 2177 | // FIXME: For a c-style function nested inside an objc implementation |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2178 | // class, there is no implementation context available, so we pass |
| 2179 | // down the context as argument to this routine. Ideally, this context |
| 2180 | // need be passed down in the AST node and somehow calculated from the |
| 2181 | // AST for a function decl. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2182 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2183 | if (ObjCImplementationDecl *IMPD = |
| 2184 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2185 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2186 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2187 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2188 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
Steve Naroff | f960657 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2189 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2190 | |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2191 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2192 | if (ClassDeclared != IFTy->getDecl() || |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2193 | ClassOfMethodDecl != ClassDeclared) |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2194 | Diag(MemberLoc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2195 | } |
| 2196 | // @protected |
| 2197 | else if (!IFTy->getDecl()->isSuperClassOf(ClassOfMethodDecl)) |
| 2198 | Diag(MemberLoc, diag::error_protected_ivar_access) << IV->getDeclName(); |
| 2199 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2200 | |
Daniel Dunbar | f5254bd | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2201 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2202 | MemberLoc, BaseExpr, |
Daniel Dunbar | f5254bd | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2203 | OpKind == tok::arrow)); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2204 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2205 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 2206 | << IFTy->getDecl()->getDeclName() << &Member |
| 2207 | << BaseExpr->getSourceRange()); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2208 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2210 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2211 | // pointer to a (potentially qualified) interface type. |
| 2212 | const PointerType *PTy; |
| 2213 | const ObjCInterfaceType *IFTy; |
| 2214 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 2215 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 2216 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | dd85128 | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2217 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2218 | // Search for a declared property first. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2219 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Context, |
| 2220 | &Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2221 | // Check whether we can reference this property. |
| 2222 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2223 | return ExprError(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2224 | QualType ResTy = PD->getType(); |
| 2225 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2226 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel); |
Fariborz Jahanian | 80ccaa9 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2227 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2228 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2229 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2230 | MemberLoc, BaseExpr)); |
| 2231 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2232 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2233 | // Check protocols on qualified interfaces. |
Chris Lattner | d5f8179 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 2234 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 2235 | E = IFTy->qual_end(); I != E; ++I) |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2236 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, |
| 2237 | &Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2238 | // Check whether we can reference this property. |
| 2239 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2240 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2241 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2242 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2243 | MemberLoc, BaseExpr)); |
| 2244 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2245 | |
| 2246 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2247 | // selector is implemented. |
| 2248 | |
| 2249 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2250 | // shared with the code in ActOnInstanceMessage. |
| 2251 | |
| 2252 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2253 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2254 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2255 | // If this reference is in an @implementation, check for 'private' methods. |
| 2256 | if (!Getter) |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2257 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2258 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2259 | // Look through local category implementations associated with the class. |
| 2260 | if (!Getter) { |
| 2261 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 2262 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2263 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel); |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2266 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2267 | // Check if we can reference this property. |
| 2268 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2269 | return ExprError(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2270 | } |
| 2271 | // If we found a getter then this may be a valid dot-reference, we |
| 2272 | // will look for the matching setter, in case it is needed. |
| 2273 | Selector SetterSel = |
| 2274 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2275 | PP.getSelectorTable(), &Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2276 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(Context, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2277 | if (!Setter) { |
| 2278 | // If this reference is in an @implementation, also check for 'private' |
| 2279 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2280 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2281 | } |
| 2282 | // Look through local category implementations associated with the class. |
| 2283 | if (!Setter) { |
| 2284 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2285 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2286 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(Context, SetterSel); |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 2287 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2288 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2289 | |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2290 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2291 | return ExprError(); |
| 2292 | |
| 2293 | if (Getter || Setter) { |
| 2294 | QualType PType; |
| 2295 | |
| 2296 | if (Getter) |
| 2297 | PType = Getter->getResultType(); |
| 2298 | else { |
| 2299 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2300 | E = Setter->param_end(); PI != E; ++PI) |
| 2301 | PType = (*PI)->getType(); |
| 2302 | } |
| 2303 | // FIXME: we must check that the setter has property type. |
| 2304 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2305 | Setter, MemberLoc, BaseExpr)); |
| 2306 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2307 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2308 | << &Member << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2309 | } |
Steve Naroff | d1d4440 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 2310 | // Handle properties on qualified "id" protocols. |
| 2311 | const ObjCQualifiedIdType *QIdTy; |
| 2312 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 2313 | // Check protocols on qualified interfaces. |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2314 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2315 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2316 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2317 | // Check the use of this declaration |
| 2318 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2319 | return ExprError(); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2320 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2321 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2322 | MemberLoc, BaseExpr)); |
| 2323 | } |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2324 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2325 | // Check the use of this method. |
| 2326 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2327 | return ExprError(); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2328 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2329 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2330 | OMD->getResultType(), |
| 2331 | OMD, OpLoc, MemberLoc, |
| 2332 | NULL, 0)); |
Fariborz Jahanian | 94cc823 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2335 | |
| 2336 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2337 | << &Member << BaseType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2338 | } |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2339 | // Handle properties on ObjC 'Class' types. |
| 2340 | if (OpKind == tok::period && (BaseType == Context.getObjCClassType())) { |
| 2341 | // Also must look for a getter name which uses property syntax. |
| 2342 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2343 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2344 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2345 | ObjCMethodDecl *Getter; |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2346 | // FIXME: need to also look locally in the implementation. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2347 | if ((Getter = IFace->lookupClassMethod(Context, Sel))) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2348 | // Check the use of this method. |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2349 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2350 | return ExprError(); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2351 | } |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2352 | // If we found a getter then this may be a valid dot-reference, we |
| 2353 | // will look for the matching setter, in case it is needed. |
| 2354 | Selector SetterSel = |
| 2355 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2356 | PP.getSelectorTable(), &Member); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2357 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel); |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2358 | if (!Setter) { |
| 2359 | // If this reference is in an @implementation, also check for 'private' |
| 2360 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2361 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2362 | } |
| 2363 | // Look through local category implementations associated with the class. |
| 2364 | if (!Setter) { |
| 2365 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2366 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | cd19b57 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2367 | Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel); |
Steve Naroff | 6f9e59f | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2372 | return ExprError(); |
| 2373 | |
| 2374 | if (Getter || Setter) { |
| 2375 | QualType PType; |
| 2376 | |
| 2377 | if (Getter) |
| 2378 | PType = Getter->getResultType(); |
| 2379 | else { |
| 2380 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2381 | E = Setter->param_end(); PI != E; ++PI) |
| 2382 | PType = (*PI)->getType(); |
| 2383 | } |
| 2384 | // FIXME: we must check that the setter has property type. |
| 2385 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2386 | Setter, MemberLoc, BaseExpr)); |
| 2387 | } |
| 2388 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2389 | << &Member << BaseType); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2393 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2394 | if (BaseType->isExtVectorType()) { |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2395 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2396 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2397 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2398 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2399 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2400 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2401 | |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2402 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2403 | << BaseType << BaseExpr->getSourceRange(); |
| 2404 | |
| 2405 | // If the user is trying to apply -> or . to a function or function |
| 2406 | // pointer, it's probably because they forgot parentheses to call |
| 2407 | // the function. Suggest the addition of those parentheses. |
| 2408 | if (BaseType == Context.OverloadTy || |
| 2409 | BaseType->isFunctionType() || |
| 2410 | (BaseType->isPointerType() && |
| 2411 | BaseType->getAsPointerType()->isFunctionType())) { |
| 2412 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2413 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2414 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2415 | } |
| 2416 | |
| 2417 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2420 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2421 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2422 | /// function prototype Proto. Call is the call expression itself, and |
| 2423 | /// Fn is the function expression. For a C++ member function, this |
| 2424 | /// routine does not attempt to convert the object argument. Returns |
| 2425 | /// true if the call is ill-formed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2426 | bool |
| 2427 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2428 | FunctionDecl *FDecl, |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2429 | const FunctionProtoType *Proto, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2430 | Expr **Args, unsigned NumArgs, |
| 2431 | SourceLocation RParenLoc) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2432 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2433 | // assignment, to the types of the corresponding parameter, ... |
| 2434 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2435 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2436 | bool Invalid = false; |
| 2437 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2438 | // If too few arguments are available (and we don't have default |
| 2439 | // arguments for the remaining parameters), don't make the call. |
| 2440 | if (NumArgs < NumArgsInProto) { |
| 2441 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2442 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2443 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2444 | // Use default arguments for missing arguments |
| 2445 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2446 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | // If too many are passed and not variadic, error on the extras and drop |
| 2450 | // them. |
| 2451 | if (NumArgs > NumArgsInProto) { |
| 2452 | if (!Proto->isVariadic()) { |
| 2453 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2454 | diag::err_typecheck_call_too_many_args) |
| 2455 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2456 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2457 | Args[NumArgs-1]->getLocEnd()); |
| 2458 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2459 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2460 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2461 | } |
| 2462 | NumArgsToCheck = NumArgsInProto; |
| 2463 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2464 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2465 | // Continue to check argument types (even if we have too few/many args). |
| 2466 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2467 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2468 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2469 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2470 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2471 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2472 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2473 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2474 | ProtoArgType, |
| 2475 | diag::err_call_incomplete_argument, |
| 2476 | Arg->getSourceRange())) |
| 2477 | return true; |
| 2478 | |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2479 | // Pass the argument. |
| 2480 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2481 | return true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2482 | } else |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2483 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2484 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2485 | QualType ArgType = Arg->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2487 | Call->setArg(i, Arg); |
| 2488 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2490 | // If this is a variadic call, handle args passed through "...". |
| 2491 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2492 | VariadicCallType CallType = VariadicFunction; |
| 2493 | if (Fn->getType()->isBlockPointerType()) |
| 2494 | CallType = VariadicBlock; // Block |
| 2495 | else if (isa<MemberExpr>(Fn)) |
| 2496 | CallType = VariadicMethod; |
| 2497 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2498 | // Promote the arguments (C99 6.5.2.2p7). |
| 2499 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2500 | Expr *Arg = Args[i]; |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2501 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2502 | Call->setArg(i, Arg); |
| 2503 | } |
| 2504 | } |
| 2505 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2506 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2509 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2510 | /// This provides the location of the left/right parens and a list of comma |
| 2511 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2512 | Action::OwningExprResult |
| 2513 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2514 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2515 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2516 | unsigned NumArgs = args.size(); |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2517 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2518 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2519 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2520 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2521 | NamedDecl *NDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2522 | DeclarationName UnqualifiedName; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2524 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2525 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2526 | // in which case we won't do any semantic analysis now. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2527 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2528 | // Fn. |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2529 | bool Dependent = false; |
| 2530 | if (Fn->isTypeDependent()) |
| 2531 | Dependent = true; |
| 2532 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2533 | Dependent = true; |
| 2534 | |
| 2535 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2536 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2537 | Context.DependentTy, RParenLoc)); |
| 2538 | |
| 2539 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2540 | if (Fn->getType()->isRecordType()) |
| 2541 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2542 | CommaLocs, RParenLoc)); |
| 2543 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2544 | // Determine whether this is a call to a member function. |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2545 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2546 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2547 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2548 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2549 | CommaLocs, RParenLoc)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2552 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2553 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2554 | Expr *FnExpr = Fn; |
| 2555 | bool ADL = true; |
| 2556 | while (true) { |
| 2557 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2558 | FnExpr = IcExpr->getSubExpr(); |
| 2559 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2560 | // Parentheses around a function disable ADL |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2561 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2562 | ADL = false; |
| 2563 | FnExpr = PExpr->getSubExpr(); |
| 2564 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2565 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2566 | == UnaryOperator::AddrOf) { |
| 2567 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2568 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2569 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2570 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2571 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2572 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2573 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2574 | UnqualifiedName = DepName->getName(); |
| 2575 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2576 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2577 | // Any kind of name that does not refer to a declaration (or |
| 2578 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2579 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2582 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2583 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2584 | OverloadedFunctionDecl *Ovl = 0; |
| 2585 | if (DRExpr) { |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2586 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2587 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2588 | NDecl = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2589 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2591 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2592 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2593 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2594 | ADL = false; |
| 2595 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2596 | // We don't perform ADL in C. |
| 2597 | if (!getLangOptions().CPlusPlus) |
| 2598 | ADL = false; |
| 2599 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2600 | if (Ovl || ADL) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2601 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2602 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2603 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2604 | if (!FDecl) |
| 2605 | return ExprError(); |
| 2606 | |
| 2607 | // Update Fn to refer to the actual function selected. |
| 2608 | Expr *NewFn = 0; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2609 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2610 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2611 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2612 | QDRExpr->getLocation(), |
| 2613 | false, false, |
| 2614 | QDRExpr->getQualifierRange(), |
| 2615 | QDRExpr->getQualifier()); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2616 | else |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2617 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2618 | Fn->getSourceRange().getBegin()); |
| 2619 | Fn->Destroy(Context); |
| 2620 | Fn = NewFn; |
| 2621 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2622 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2623 | |
| 2624 | // Promote the function operand. |
| 2625 | UsualUnaryConversions(Fn); |
| 2626 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2627 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2628 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2629 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2630 | Args, NumArgs, |
| 2631 | Context.BoolTy, |
| 2632 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2633 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2634 | const FunctionType *FuncT; |
| 2635 | if (!Fn->getType()->isBlockPointerType()) { |
| 2636 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2637 | // have type pointer to function". |
| 2638 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2639 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2640 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2641 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2642 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2643 | } else { // This is a block call. |
| 2644 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2645 | getAsFunctionType(); |
| 2646 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2647 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2648 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2649 | << Fn->getType() << Fn->getSourceRange()); |
| 2650 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2651 | // Check for a valid return type |
| 2652 | if (!FuncT->getResultType()->isVoidType() && |
| 2653 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2654 | FuncT->getResultType(), |
| 2655 | diag::err_call_incomplete_return, |
| 2656 | TheCall->getSourceRange())) |
| 2657 | return ExprError(); |
| 2658 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2659 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2660 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2661 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2662 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2663 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2664 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2665 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2666 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2667 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2669 | if (FDecl) { |
| 2670 | // Check if we have too few/too many template arguments, based |
| 2671 | // on our knowledge of the function definition. |
| 2672 | const FunctionDecl *Def = 0; |
Douglas Gregor | e3241e9 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 2673 | if (FDecl->getBody(Context, Def) && NumArgs != Def->param_size()) |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2674 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2675 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2676 | } |
| 2677 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2678 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2679 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2680 | Expr *Arg = Args[i]; |
| 2681 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2682 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2683 | Arg->getType(), |
| 2684 | diag::err_call_incomplete_argument, |
| 2685 | Arg->getSourceRange())) |
| 2686 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2687 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2688 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2689 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2690 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2691 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2692 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2693 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2694 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2695 | |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2696 | // Check for sentinels |
| 2697 | if (NDecl) |
| 2698 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2699 | // Do special checking on direct calls to functions. |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2700 | if (FDecl) |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2701 | return CheckFunctionCall(FDecl, TheCall.take()); |
Fariborz Jahanian | f83c85f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 2702 | if (NDecl) |
| 2703 | return CheckBlockCall(NDecl, TheCall.take()); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2704 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2705 | return Owned(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2708 | Action::OwningExprResult |
| 2709 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2710 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2711 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2712 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 2713 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2714 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2715 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2716 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2717 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2718 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2719 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2720 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | ed71c54 | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2721 | } else if (!literalType->isDependentType() && |
| 2722 | RequireCompleteType(LParenLoc, literalType, |
| 2723 | diag::err_typecheck_decl_incomplete_type, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2724 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2725 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2726 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2727 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2728 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2729 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2730 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2731 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2732 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2733 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2734 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2735 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2736 | InitExpr.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2737 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2738 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2741 | Action::OwningExprResult |
| 2742 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2743 | SourceLocation RBraceLoc) { |
| 2744 | unsigned NumInit = initlist.size(); |
| 2745 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2746 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2747 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2748 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2749 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2750 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2751 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2752 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2753 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2756 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 5ad49de | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2757 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2758 | UsualUnaryConversions(castExpr); |
| 2759 | |
| 2760 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2761 | // type needs to be scalar. |
| 2762 | if (castType->isVoidType()) { |
| 2763 | // Cast to void allows any expr type. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2764 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2765 | // We can't check any more until template instantiation time. |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2766 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2767 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2768 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2769 | (castType->isStructureType() || castType->isUnionType())) { |
| 2770 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2771 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2772 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2773 | << castType << castExpr->getSourceRange(); |
| 2774 | } else if (castType->isUnionType()) { |
| 2775 | // GCC cast to union extension |
| 2776 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2777 | RecordDecl::field_iterator Field, FieldEnd; |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2778 | for (Field = RD->field_begin(Context), FieldEnd = RD->field_end(Context); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2779 | Field != FieldEnd; ++Field) { |
| 2780 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2781 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2782 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2783 | << castExpr->getSourceRange(); |
| 2784 | break; |
| 2785 | } |
| 2786 | } |
| 2787 | if (Field == FieldEnd) |
| 2788 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2789 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2790 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2791 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2792 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2793 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2794 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2795 | } else if (!castExpr->getType()->isScalarType() && |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2796 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2797 | return Diag(castExpr->getLocStart(), |
| 2798 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2799 | << castExpr->getType() << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2800 | } else if (castExpr->getType()->isVectorType()) { |
| 2801 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2802 | return true; |
| 2803 | } else if (castType->isVectorType()) { |
| 2804 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2805 | return true; |
Steve Naroff | ff6c802 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2806 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | 49fd7ad | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 2807 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Eli Friedman | 970e56c | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 2808 | } else if (!castType->isArithmeticType()) { |
| 2809 | QualType castExprType = castExpr->getType(); |
| 2810 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 2811 | return Diag(castExpr->getLocStart(), |
| 2812 | diag::err_cast_pointer_from_non_pointer_int) |
| 2813 | << castExprType << castExpr->getSourceRange(); |
| 2814 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 2815 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 2816 | return Diag(castExpr->getLocStart(), |
| 2817 | diag::err_cast_pointer_to_non_pointer_int) |
| 2818 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2819 | } |
Fariborz Jahanian | 4862e87 | 2009-05-22 21:42:52 +0000 | [diff] [blame] | 2820 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 2821 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2822 | return false; |
| 2823 | } |
| 2824 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2825 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2826 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2827 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2828 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2829 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2830 | return Diag(R.getBegin(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2831 | Ty->isVectorType() ? |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2832 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2833 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2834 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2835 | } else |
| 2836 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2837 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2838 | << VectorTy << Ty << R; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2839 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2840 | return false; |
| 2841 | } |
| 2842 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2843 | Action::OwningExprResult |
| 2844 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2845 | SourceLocation RParenLoc, ExprArg Op) { |
| 2846 | assert((Ty != 0) && (Op.get() != 0) && |
| 2847 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2848 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2849 | Expr *castExpr = Op.takeAs<Expr>(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2850 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2851 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2852 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2853 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2854 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2855 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2858 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 2859 | /// In that case, lhs = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2860 | /// C99 6.5.15 |
| 2861 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2862 | SourceLocation QuestionLoc) { |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2863 | // C++ is sufficiently different to merit its own checker. |
| 2864 | if (getLangOptions().CPlusPlus) |
| 2865 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 2866 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2867 | UsualUnaryConversions(Cond); |
| 2868 | UsualUnaryConversions(LHS); |
| 2869 | UsualUnaryConversions(RHS); |
| 2870 | QualType CondTy = Cond->getType(); |
| 2871 | QualType LHSTy = LHS->getType(); |
| 2872 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2873 | |
| 2874 | // first, check the condition. |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2875 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2876 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2877 | << CondTy; |
| 2878 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2879 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2880 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2881 | // Now check the two expressions. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2882 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2883 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2884 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2885 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2886 | UsualArithmeticConversions(LHS, RHS); |
| 2887 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2888 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2889 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2890 | // If both operands are the same structure or union type, the result is that |
| 2891 | // type. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2892 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2893 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2894 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2895 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2896 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2897 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2898 | // FIXME: Type of conditional expression must be complete in C mode. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2899 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2900 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2901 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2902 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2903 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2904 | if (!LHSTy->isVoidType()) |
| 2905 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2906 | << RHS->getSourceRange(); |
| 2907 | if (!RHSTy->isVoidType()) |
| 2908 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2909 | << LHS->getSourceRange(); |
| 2910 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2911 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2912 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2913 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2914 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2915 | // the type of the other operand." |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2916 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2917 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2918 | RHS->isNullPointerConstant(Context)) { |
| 2919 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2920 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2921 | } |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2922 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2923 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2924 | LHS->isNullPointerConstant(Context)) { |
| 2925 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2926 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2927 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2928 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2929 | const PointerType *LHSPT = LHSTy->getAsPointerType(); |
| 2930 | const PointerType *RHSPT = RHSTy->getAsPointerType(); |
| 2931 | const BlockPointerType *LHSBPT = LHSTy->getAsBlockPointerType(); |
| 2932 | const BlockPointerType *RHSBPT = RHSTy->getAsBlockPointerType(); |
| 2933 | |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2934 | // Handle the case where both operands are pointers before we handle null |
| 2935 | // pointer constants in case both operands are null pointer constants. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2936 | if ((LHSPT || LHSBPT) && (RHSPT || RHSBPT)) { // C99 6.5.15p3,6 |
| 2937 | // get the "pointed to" types |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 2938 | QualType lhptee = (LHSPT ? LHSPT->getPointeeType() |
| 2939 | : LHSBPT->getPointeeType()); |
| 2940 | QualType rhptee = (RHSPT ? RHSPT->getPointeeType() |
| 2941 | : RHSBPT->getPointeeType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2942 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2943 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2944 | if (lhptee->isVoidType() |
| 2945 | && (RHSBPT || rhptee->isIncompleteOrObjectType())) { |
| 2946 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2947 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 2948 | QualType destType = Context.getPointerType(destPointee); |
| 2949 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2950 | ImpCastExprToType(RHS, destType); // promote to void* |
| 2951 | return destType; |
| 2952 | } |
| 2953 | if (rhptee->isVoidType() |
| 2954 | && (LHSBPT || lhptee->isIncompleteOrObjectType())) { |
| 2955 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 2956 | QualType destType = Context.getPointerType(destPointee); |
| 2957 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2958 | ImpCastExprToType(RHS, destType); // promote to void* |
| 2959 | return destType; |
| 2960 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2961 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2962 | bool sameKind = (LHSPT && RHSPT) || (LHSBPT && RHSBPT); |
| 2963 | if (sameKind |
| 2964 | && Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 2965 | // Two identical pointer types are always compatible. |
| 2966 | return LHSTy; |
| 2967 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2968 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2969 | QualType compositeType = LHSTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2970 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2971 | // If either type is an Objective-C object type then check |
| 2972 | // compatibility according to Objective-C. |
| 2973 | if (Context.isObjCObjectPointerType(LHSTy) || |
| 2974 | Context.isObjCObjectPointerType(RHSTy)) { |
| 2975 | // If both operands are interfaces and either operand can be |
| 2976 | // assigned to the other, use that type as the composite |
| 2977 | // type. This allows |
| 2978 | // xxx ? (A*) a : (B*) b |
| 2979 | // where B is a subclass of A. |
| 2980 | // |
| 2981 | // Additionally, as for assignment, if either type is 'id' |
| 2982 | // allow silent coercion. Finally, if the types are |
| 2983 | // incompatible then make sure to use 'id' as the composite |
| 2984 | // type so the result is acceptable for sending messages to. |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 2985 | |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2986 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 2987 | // It could return the composite type. |
| 2988 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2989 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2990 | if (LHSIface && RHSIface && |
| 2991 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 2992 | compositeType = LHSTy; |
| 2993 | } else if (LHSIface && RHSIface && |
| 2994 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
| 2995 | compositeType = RHSTy; |
| 2996 | } else if (Context.isObjCIdStructType(lhptee) || |
| 2997 | Context.isObjCIdStructType(rhptee)) { |
| 2998 | compositeType = Context.getObjCIdType(); |
| 2999 | } else if (LHSBPT || RHSBPT) { |
| 3000 | if (!sameKind |
| 3001 | || !Context.typesAreBlockCompatible(lhptee.getUnqualifiedType(), |
| 3002 | rhptee.getUnqualifiedType())) |
| 3003 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3004 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3005 | return QualType(); |
| 3006 | } else { |
| 3007 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3008 | << LHSTy << RHSTy |
| 3009 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3010 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3011 | ImpCastExprToType(LHS, incompatTy); |
| 3012 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | cd23bb2 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 3013 | return incompatTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 3014 | } |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3015 | } else if (!sameKind |
| 3016 | || !Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3017 | rhptee.getUnqualifiedType())) { |
| 3018 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3019 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3020 | // In this situation, we assume void* type. No especially good |
| 3021 | // reason, but this is what gcc does, and we do have to pick |
| 3022 | // to get a consistent AST. |
| 3023 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3024 | ImpCastExprToType(LHS, incompatTy); |
| 3025 | ImpCastExprToType(RHS, incompatTy); |
| 3026 | return incompatTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3027 | } |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3028 | // The pointer types are compatible. |
| 3029 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3030 | // differently qualified versions of compatible types, the result type is |
| 3031 | // a pointer to an appropriately qualified version of the *composite* |
| 3032 | // type. |
| 3033 | // FIXME: Need to calculate the composite type. |
| 3034 | // FIXME: Need to add qualifiers |
| 3035 | ImpCastExprToType(LHS, compositeType); |
| 3036 | ImpCastExprToType(RHS, compositeType); |
| 3037 | return compositeType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3038 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3039 | |
Steve Naroff | 6ba2268 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3040 | // GCC compatibility: soften pointer/integer mismatch. |
| 3041 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3042 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3043 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3044 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 3045 | return RHSTy; |
| 3046 | } |
| 3047 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3048 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3049 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3050 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 3051 | return LHSTy; |
| 3052 | } |
| 3053 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3054 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 3055 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3056 | // id with statically typed objects). |
| 3057 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3058 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3059 | // id. Currently localizing to here until clear this should be |
| 3060 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3061 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3062 | (LHSTy->isObjCQualifiedIdType() && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3063 | Context.isObjCObjectPointerType(RHSTy)) || |
| 3064 | (RHSTy->isObjCQualifiedIdType() && |
| 3065 | Context.isObjCObjectPointerType(LHSTy))) { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3066 | // FIXME: This is not the correct composite type. This only happens to |
| 3067 | // work because id can more or less be used anywhere, however this may |
| 3068 | // change the type of method sends. |
| 3069 | |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3070 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 3071 | // (confusing) incompatible comparison warnings in some |
| 3072 | // cases. Investigate. |
| 3073 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3074 | ImpCastExprToType(LHS, compositeType); |
| 3075 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3076 | return compositeType; |
| 3077 | } |
| 3078 | } |
| 3079 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3080 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3081 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3082 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3083 | return QualType(); |
| 3084 | } |
| 3085 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3086 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3087 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3088 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3089 | SourceLocation ColonLoc, |
| 3090 | ExprArg Cond, ExprArg LHS, |
| 3091 | ExprArg RHS) { |
| 3092 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3093 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3094 | |
| 3095 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3096 | // was the condition. |
| 3097 | bool isLHSNull = LHSExpr == 0; |
| 3098 | if (isLHSNull) |
| 3099 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3100 | |
| 3101 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3102 | RHSExpr, QuestionLoc); |
| 3103 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3104 | return ExprError(); |
| 3105 | |
| 3106 | Cond.release(); |
| 3107 | LHS.release(); |
| 3108 | RHS.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3109 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3110 | isLHSNull ? 0 : LHSExpr, |
| 3111 | RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3114 | |
| 3115 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3116 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3117 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3118 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3119 | // FIXME: add a couple examples in this comment. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3120 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3121 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 3122 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3123 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3124 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 3125 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 3126 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3128 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3129 | lhptee = Context.getCanonicalType(lhptee); |
| 3130 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3132 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3133 | |
| 3134 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3135 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3136 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3137 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3138 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3139 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3140 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3141 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3142 | // incomplete type and the other is a pointer to a qualified or unqualified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3143 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3144 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3145 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3146 | return ConvTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3148 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3149 | assert(rhptee->isFunctionType()); |
| 3150 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3151 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3152 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3153 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3154 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3155 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3156 | |
| 3157 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3158 | assert(lhptee->isFunctionType()); |
| 3159 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3160 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3161 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3162 | // unqualified versions of compatible types, ... |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3163 | lhptee = lhptee.getUnqualifiedType(); |
| 3164 | rhptee = rhptee.getUnqualifiedType(); |
| 3165 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3166 | // Check if the pointee types are compatible ignoring the sign. |
| 3167 | // We explicitly check for char so that we catch "char" vs |
| 3168 | // "unsigned char" on systems where "char" is unsigned. |
| 3169 | if (lhptee->isCharType()) { |
| 3170 | lhptee = Context.UnsignedCharTy; |
| 3171 | } else if (lhptee->isSignedIntegerType()) { |
| 3172 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 3173 | } |
| 3174 | if (rhptee->isCharType()) { |
| 3175 | rhptee = Context.UnsignedCharTy; |
| 3176 | } else if (rhptee->isSignedIntegerType()) { |
| 3177 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 3178 | } |
| 3179 | if (lhptee == rhptee) { |
| 3180 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3181 | // takes priority over sign incompatibility because the sign |
| 3182 | // warning can be disabled. |
| 3183 | if (ConvTy != Compatible) |
| 3184 | return ConvTy; |
| 3185 | return IncompatiblePointerSign; |
| 3186 | } |
| 3187 | // General pointer incompatibility takes priority over qualifiers. |
| 3188 | return IncompatiblePointer; |
| 3189 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3190 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3193 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3194 | /// block pointer types are compatible or whether a block and normal pointer |
| 3195 | /// are compatible. It is more restrict than comparing two function pointer |
| 3196 | // types. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3197 | Sema::AssignConvertType |
| 3198 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3199 | QualType rhsType) { |
| 3200 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3201 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3202 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 3203 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3204 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 3205 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3206 | // make sure we operate on the canonical type |
| 3207 | lhptee = Context.getCanonicalType(lhptee); |
| 3208 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3209 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3210 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3211 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3212 | // For blocks we enforce that qualifiers are identical. |
| 3213 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 3214 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3215 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3216 | if (!Context.typesAreBlockCompatible(lhptee, rhptee)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3217 | return IncompatibleBlockPointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3218 | return ConvTy; |
| 3219 | } |
| 3220 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3221 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3222 | /// has code to accommodate several GCC extensions when type checking |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3223 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3224 | /// |
| 3225 | /// int a, *pint; |
| 3226 | /// short *pshort; |
| 3227 | /// struct foo *pfoo; |
| 3228 | /// |
| 3229 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3230 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3231 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3232 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3233 | /// |
| 3234 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3235 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3236 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3237 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3238 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3239 | // Get canonical types. We're not formatting these types, just comparing |
| 3240 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3241 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3242 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3245 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3247 | // If the left-hand side is a reference type, then we are in a |
| 3248 | // (rare!) case where we've allowed the use of references in C, |
| 3249 | // e.g., as a parameter type in a built-in function. In this case, |
| 3250 | // just make sure that the type referenced is compatible with the |
| 3251 | // right-hand side type. The caller is responsible for adjusting |
| 3252 | // lhsType so that the resulting expression does not have reference |
| 3253 | // type. |
| 3254 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 3255 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3256 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3257 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3258 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3259 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3260 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 3261 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3262 | return Compatible; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3263 | // Relax integer conversions like we do for pointers below. |
| 3264 | if (rhsType->isIntegerType()) |
| 3265 | return IntToPointer; |
| 3266 | if (lhsType->isIntegerType()) |
| 3267 | return PointerToInt; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3268 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3269 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3270 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3271 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3272 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3273 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 3274 | if (LV->getElementType() == rhsType) |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3275 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3276 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3277 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3278 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3279 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3280 | if (getLangOptions().LaxVectorConversions && |
| 3281 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3282 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3283 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3284 | } |
| 3285 | return Incompatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3286 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3287 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3288 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3289 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3291 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3292 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3293 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3294 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3295 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3296 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3297 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3298 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3299 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3300 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3301 | |
| 3302 | // Treat block pointers as objects. |
| 3303 | if (getLangOptions().ObjC1 && |
| 3304 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 3305 | return Compatible; |
| 3306 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3307 | return Incompatible; |
| 3308 | } |
| 3309 | |
| 3310 | if (isa<BlockPointerType>(lhsType)) { |
| 3311 | if (rhsType->isIntegerType()) |
Eli Friedman | c589830 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3312 | return IntToBlockPointer; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3313 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3314 | // Treat block pointers as objects. |
| 3315 | if (getLangOptions().ObjC1 && |
| 3316 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 3317 | return Compatible; |
| 3318 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3319 | if (rhsType->isBlockPointerType()) |
| 3320 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3321 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3322 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 3323 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3324 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3325 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3326 | return Incompatible; |
| 3327 | } |
| 3328 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3329 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3330 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3331 | if (lhsType == Context.BoolTy) |
| 3332 | return Compatible; |
| 3333 | |
| 3334 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3335 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3336 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3337 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3338 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3339 | |
| 3340 | if (isa<BlockPointerType>(lhsType) && |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3341 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3342 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3343 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3344 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3345 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3346 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3347 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3348 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3349 | } |
| 3350 | return Incompatible; |
| 3351 | } |
| 3352 | |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3353 | /// \brief Constructs a transparent union from an expression that is |
| 3354 | /// used to initialize the transparent union. |
| 3355 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
| 3356 | QualType UnionType, FieldDecl *Field) { |
| 3357 | // Build an initializer list that designates the appropriate member |
| 3358 | // of the transparent union. |
| 3359 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3360 | &E, 1, |
| 3361 | SourceLocation()); |
| 3362 | Initializer->setType(UnionType); |
| 3363 | Initializer->setInitializedFieldInUnion(Field); |
| 3364 | |
| 3365 | // Build a compound literal constructing a value of the transparent |
| 3366 | // union type from this initializer list. |
| 3367 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3368 | false); |
| 3369 | } |
| 3370 | |
| 3371 | Sema::AssignConvertType |
| 3372 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3373 | QualType FromType = rExpr->getType(); |
| 3374 | |
| 3375 | // If the ArgType is a Union type, we want to handle a potential |
| 3376 | // transparent_union GCC extension. |
| 3377 | const RecordType *UT = ArgType->getAsUnionType(); |
| 3378 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 3379 | return Incompatible; |
| 3380 | |
| 3381 | // The field to initialize within the transparent union. |
| 3382 | RecordDecl *UD = UT->getDecl(); |
| 3383 | FieldDecl *InitField = 0; |
| 3384 | // It's compatible if the expression matches any of the fields. |
| 3385 | for (RecordDecl::field_iterator it = UD->field_begin(Context), |
| 3386 | itend = UD->field_end(Context); |
| 3387 | it != itend; ++it) { |
| 3388 | if (it->getType()->isPointerType()) { |
| 3389 | // If the transparent union contains a pointer type, we allow: |
| 3390 | // 1) void pointer |
| 3391 | // 2) null pointer constant |
| 3392 | if (FromType->isPointerType()) |
| 3393 | if (FromType->getAsPointerType()->getPointeeType()->isVoidType()) { |
| 3394 | ImpCastExprToType(rExpr, it->getType()); |
| 3395 | InitField = *it; |
| 3396 | break; |
| 3397 | } |
| 3398 | |
| 3399 | if (rExpr->isNullPointerConstant(Context)) { |
| 3400 | ImpCastExprToType(rExpr, it->getType()); |
| 3401 | InitField = *it; |
| 3402 | break; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 3407 | == Compatible) { |
| 3408 | InitField = *it; |
| 3409 | break; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | if (!InitField) |
| 3414 | return Incompatible; |
| 3415 | |
| 3416 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 3417 | return Compatible; |
| 3418 | } |
| 3419 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3420 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3421 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3422 | if (getLangOptions().CPlusPlus) { |
| 3423 | if (!lhsType->isRecordType()) { |
| 3424 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3425 | // expression is implicitly converted (C++ 4) to the |
| 3426 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3427 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3428 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3429 | return Incompatible; |
Chris Lattner | 79e9a42 | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 3430 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3434 | // structures. |
| 3435 | } |
| 3436 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3437 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3438 | // a null pointer constant. |
Steve Naroff | d305a86 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 3439 | if ((lhsType->isPointerType() || |
| 3440 | lhsType->isObjCQualifiedIdType() || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3441 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3442 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3443 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3444 | return Compatible; |
| 3445 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3446 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3447 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3448 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3449 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3450 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3451 | // |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3452 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3453 | if (!lhsType->isReferenceType()) |
| 3454 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3455 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3456 | Sema::AssignConvertType result = |
| 3457 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3458 | |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3459 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3460 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3461 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3462 | // so that we can use references in built-in functions even in C. |
| 3463 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3464 | // does not have reference type. |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3465 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3466 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3467 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3470 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3471 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3472 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3473 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3474 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3477 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3478 | Expr *&rex) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3479 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3480 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3481 | QualType lhsType = |
| 3482 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3483 | QualType rhsType = |
| 3484 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3485 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3486 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3487 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3488 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3489 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3490 | // Handle the case of a vector & extvector type of the same size and element |
| 3491 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3492 | if (getLangOptions().LaxVectorConversions) { |
| 3493 | // FIXME: Should we warn here? |
| 3494 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3495 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3496 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3497 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3498 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3499 | } |
| 3500 | } |
| 3501 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3502 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3503 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 3504 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3505 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3506 | QualType eltType = V->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3507 | |
| 3508 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3509 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 3510 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3511 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3512 | return lhsType; |
| 3513 | } |
| 3514 | } |
| 3515 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3516 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3517 | // promote the lhs to the vector type. |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3518 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3519 | QualType eltType = V->getElementType(); |
| 3520 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3521 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3522 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 3523 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3524 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3525 | return rhsType; |
| 3526 | } |
| 3527 | } |
| 3528 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3529 | // You cannot convert between vector values of different size. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3530 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3531 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3532 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3533 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3536 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3537 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3538 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 3539 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3540 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3541 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3542 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3543 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3544 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3545 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3546 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3550 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3551 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 3552 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3553 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 3554 | return CheckVectorOperands(Loc, lex, rex); |
| 3555 | return InvalidOperands(Loc, lex, rex); |
| 3556 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3557 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3558 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3559 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3560 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3561 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3562 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3566 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3567 | { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3568 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3569 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3570 | if (CompLHSTy) *CompLHSTy = compType; |
| 3571 | return compType; |
| 3572 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3573 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3574 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3575 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3576 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3577 | if (lex->getType()->isArithmeticType() && |
| 3578 | rex->getType()->isArithmeticType()) { |
| 3579 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3580 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3581 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3582 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3583 | // Put any potential pointer into PExp |
| 3584 | Expr* PExp = lex, *IExp = rex; |
| 3585 | if (IExp->getType()->isPointerType()) |
| 3586 | std::swap(PExp, IExp); |
| 3587 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3588 | if (const PointerType *PTy = PExp->getType()->getAsPointerType()) { |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3589 | if (IExp->getType()->isIntegerType()) { |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3590 | QualType PointeeTy = PTy->getPointeeType(); |
| 3591 | // Check for arithmetic on pointers to incomplete types. |
| 3592 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3593 | if (getLangOptions().CPlusPlus) { |
| 3594 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3595 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3596 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3597 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3598 | |
| 3599 | // GNU extension: arithmetic on pointer to void |
| 3600 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3601 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3602 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3603 | if (getLangOptions().CPlusPlus) { |
| 3604 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3605 | << lex->getType() << lex->getSourceRange(); |
| 3606 | return QualType(); |
| 3607 | } |
| 3608 | |
| 3609 | // GNU extension: arithmetic on pointer to function |
| 3610 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3611 | << lex->getType() << lex->getSourceRange(); |
| 3612 | } else if (!PTy->isDependentType() && |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3613 | RequireCompleteType(Loc, PointeeTy, |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3614 | diag::err_typecheck_arithmetic_incomplete_type, |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3615 | PExp->getSourceRange(), SourceRange(), |
| 3616 | PExp->getType())) |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3617 | return QualType(); |
| 3618 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3619 | // Diagnose bad cases where we step over interface counts. |
| 3620 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 3621 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 3622 | << PointeeTy << PExp->getSourceRange(); |
| 3623 | return QualType(); |
| 3624 | } |
| 3625 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3626 | if (CompLHSTy) { |
| 3627 | QualType LHSTy = lex->getType(); |
| 3628 | if (LHSTy->isPromotableIntegerType()) |
| 3629 | LHSTy = Context.IntTy; |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 3630 | else { |
| 3631 | QualType T = isPromotableBitField(lex, Context); |
| 3632 | if (!T.isNull()) |
| 3633 | LHSTy = T; |
| 3634 | } |
| 3635 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3636 | *CompLHSTy = LHSTy; |
| 3637 | } |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3638 | return PExp->getType(); |
| 3639 | } |
| 3640 | } |
| 3641 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3642 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3645 | // C99 6.5.6 |
| 3646 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3647 | SourceLocation Loc, QualType* CompLHSTy) { |
| 3648 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3649 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3650 | if (CompLHSTy) *CompLHSTy = compType; |
| 3651 | return compType; |
| 3652 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3653 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3654 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3655 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3656 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3657 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3658 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 3659 | if (lex->getType()->isArithmeticType() |
| 3660 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3661 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3662 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3663 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3664 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3665 | // Either ptr - int or ptr - ptr. |
| 3666 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3667 | QualType lpointee = LHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3669 | // The LHS must be an completely-defined object type. |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3671 | bool ComplainAboutVoid = false; |
| 3672 | Expr *ComplainAboutFunc = 0; |
| 3673 | if (lpointee->isVoidType()) { |
| 3674 | if (getLangOptions().CPlusPlus) { |
| 3675 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3676 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3677 | return QualType(); |
| 3678 | } |
| 3679 | |
| 3680 | // GNU C extension: arithmetic on pointer to void |
| 3681 | ComplainAboutVoid = true; |
| 3682 | } else if (lpointee->isFunctionType()) { |
| 3683 | if (getLangOptions().CPlusPlus) { |
| 3684 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3685 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3686 | return QualType(); |
| 3687 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3688 | |
| 3689 | // GNU C extension: arithmetic on pointer to function |
| 3690 | ComplainAboutFunc = lex; |
| 3691 | } else if (!lpointee->isDependentType() && |
| 3692 | RequireCompleteType(Loc, lpointee, |
| 3693 | diag::err_typecheck_sub_ptr_object, |
| 3694 | lex->getSourceRange(), |
| 3695 | SourceRange(), |
| 3696 | lex->getType())) |
| 3697 | return QualType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3698 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3699 | // Diagnose bad cases where we step over interface counts. |
| 3700 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 3701 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 3702 | << lpointee << lex->getSourceRange(); |
| 3703 | return QualType(); |
| 3704 | } |
| 3705 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3706 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3707 | if (rex->getType()->isIntegerType()) { |
| 3708 | if (ComplainAboutVoid) |
| 3709 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3710 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3711 | if (ComplainAboutFunc) |
| 3712 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3713 | << ComplainAboutFunc->getType() |
| 3714 | << ComplainAboutFunc->getSourceRange(); |
| 3715 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3716 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3717 | return lex->getType(); |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3718 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3719 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3720 | // Handle pointer-pointer subtractions. |
| 3721 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3722 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3724 | // RHS must be a completely-type object type. |
| 3725 | // Handle the GNU void* extension. |
| 3726 | if (rpointee->isVoidType()) { |
| 3727 | if (getLangOptions().CPlusPlus) { |
| 3728 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3729 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3730 | return QualType(); |
| 3731 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3732 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3733 | ComplainAboutVoid = true; |
| 3734 | } else if (rpointee->isFunctionType()) { |
| 3735 | if (getLangOptions().CPlusPlus) { |
| 3736 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3737 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3738 | return QualType(); |
| 3739 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3740 | |
| 3741 | // GNU extension: arithmetic on pointer to function |
| 3742 | if (!ComplainAboutFunc) |
| 3743 | ComplainAboutFunc = rex; |
| 3744 | } else if (!rpointee->isDependentType() && |
| 3745 | RequireCompleteType(Loc, rpointee, |
| 3746 | diag::err_typecheck_sub_ptr_object, |
| 3747 | rex->getSourceRange(), |
| 3748 | SourceRange(), |
| 3749 | rex->getType())) |
| 3750 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3751 | |
Eli Friedman | 143ddc9 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 3752 | if (getLangOptions().CPlusPlus) { |
| 3753 | // Pointee types must be the same: C++ [expr.add] |
| 3754 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 3755 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 3756 | << lex->getType() << rex->getType() |
| 3757 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3758 | return QualType(); |
| 3759 | } |
| 3760 | } else { |
| 3761 | // Pointee types must be compatible C99 6.5.6p3 |
| 3762 | if (!Context.typesAreCompatible( |
| 3763 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 3764 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 3765 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 3766 | << lex->getType() << rex->getType() |
| 3767 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3768 | return QualType(); |
| 3769 | } |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3770 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3772 | if (ComplainAboutVoid) |
| 3773 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3774 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3775 | if (ComplainAboutFunc) |
| 3776 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3777 | << ComplainAboutFunc->getType() |
| 3778 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3779 | |
| 3780 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3781 | return Context.getPointerDiffType(); |
| 3782 | } |
| 3783 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3784 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3785 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3788 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3789 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3790 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3791 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3792 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3793 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3794 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3795 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3796 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3797 | QualType LHSTy; |
| 3798 | if (lex->getType()->isPromotableIntegerType()) |
| 3799 | LHSTy = Context.IntTy; |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 3800 | else { |
| 3801 | LHSTy = isPromotableBitField(lex, Context); |
| 3802 | if (LHSTy.isNull()) |
| 3803 | LHSTy = lex->getType(); |
| 3804 | } |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3805 | if (!isCompAssign) |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3806 | ImpCastExprToType(lex, LHSTy); |
| 3807 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3808 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3809 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3810 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3811 | return LHSTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3814 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3815 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3816 | unsigned OpaqueOpc, bool isRelational) { |
| 3817 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 3818 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3819 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3820 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3821 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3822 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3823 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3824 | UsualArithmeticConversions(lex, rex); |
| 3825 | else { |
| 3826 | UsualUnaryConversions(lex); |
| 3827 | UsualUnaryConversions(rex); |
| 3828 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3829 | QualType lType = lex->getType(); |
| 3830 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3831 | |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 3832 | if (!lType->isFloatingType() |
| 3833 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3834 | // For non-floating point types, check for self-comparisons of the form |
| 3835 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3836 | // often indicate logic errors in the program. |
Ted Kremenek | 264b5cb | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 3837 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
| 3838 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3839 | Expr *LHSStripped = lex->IgnoreParens(); |
| 3840 | Expr *RHSStripped = rex->IgnoreParens(); |
| 3841 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 3842 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | f042dc6 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 3843 | if (DRL->getDecl() == DRR->getDecl() && |
| 3844 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3845 | Diag(Loc, diag::warn_selfcomparison); |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3846 | |
| 3847 | if (isa<CastExpr>(LHSStripped)) |
| 3848 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 3849 | if (isa<CastExpr>(RHSStripped)) |
| 3850 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| 3851 | |
| 3852 | // Warn about comparisons against a string constant (unless the other |
| 3853 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3854 | Expr *literalString = 0; |
| 3855 | Expr *literalStringStripped = 0; |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3856 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3857 | !RHSStripped->isNullPointerConstant(Context)) { |
| 3858 | literalString = lex; |
| 3859 | literalStringStripped = LHSStripped; |
| 3860 | } |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3861 | else if ((isa<StringLiteral>(RHSStripped) || |
| 3862 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3863 | !LHSStripped->isNullPointerConstant(Context)) { |
| 3864 | literalString = rex; |
| 3865 | literalStringStripped = RHSStripped; |
| 3866 | } |
| 3867 | |
| 3868 | if (literalString) { |
| 3869 | std::string resultComparison; |
| 3870 | switch (Opc) { |
| 3871 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 3872 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 3873 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 3874 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 3875 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 3876 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 3877 | default: assert(false && "Invalid comparison operator"); |
| 3878 | } |
| 3879 | Diag(Loc, diag::warn_stringcompare) |
| 3880 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 3881 | << literalString->getSourceRange() |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3882 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 3883 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 3884 | "strcmp(") |
| 3885 | << CodeModificationHint::CreateInsertion( |
| 3886 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3887 | resultComparison); |
| 3888 | } |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3889 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3891 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3892 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3893 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3894 | if (isRelational) { |
| 3895 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3896 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3897 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3898 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3899 | if (lType->isFloatingType()) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3900 | assert(rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3901 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3902 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3903 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3904 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3905 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3906 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3907 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3908 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3909 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3910 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3911 | // All of the following pointer related warnings are GCC extensions, except |
| 3912 | // when handling null pointer constants. One day, we can consider making them |
| 3913 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3914 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3915 | QualType LCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3916 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3917 | QualType RCanPointeeTy = |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3918 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3920 | // Simple check: if the pointee types are identical, we're done. |
| 3921 | if (LCanPointeeTy == RCanPointeeTy) |
| 3922 | return ResultTy; |
| 3923 | |
| 3924 | if (getLangOptions().CPlusPlus) { |
| 3925 | // C++ [expr.rel]p2: |
| 3926 | // [...] Pointer conversions (4.10) and qualification |
| 3927 | // conversions (4.4) are performed on pointer operands (or on |
| 3928 | // a pointer operand and a null pointer constant) to bring |
| 3929 | // them to their composite pointer type. [...] |
| 3930 | // |
| 3931 | // C++ [expr.eq]p2 uses the same notion for (in)equality |
| 3932 | // comparisons of pointers. |
Douglas Gregor | cf651d2 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 3933 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3934 | if (T.isNull()) { |
| 3935 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 3936 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 3937 | return QualType(); |
| 3938 | } |
| 3939 | |
| 3940 | ImpCastExprToType(lex, T); |
| 3941 | ImpCastExprToType(rex, T); |
| 3942 | return ResultTy; |
| 3943 | } |
| 3944 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3945 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3946 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3947 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 0d9549b | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3948 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 17c0382 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3949 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3950 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3951 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3952 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3953 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3954 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3955 | } |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3956 | // C++ allows comparison of pointers with null pointer constants. |
| 3957 | if (getLangOptions().CPlusPlus) { |
| 3958 | if (lType->isPointerType() && RHSIsNull) { |
| 3959 | ImpCastExprToType(rex, lType); |
| 3960 | return ResultTy; |
| 3961 | } |
| 3962 | if (rType->isPointerType() && LHSIsNull) { |
| 3963 | ImpCastExprToType(lex, rType); |
| 3964 | return ResultTy; |
| 3965 | } |
| 3966 | // And comparison of nullptr_t with itself. |
| 3967 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 3968 | return ResultTy; |
| 3969 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3970 | // Handle block pointer types. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3971 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3972 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 3973 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3974 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3975 | if (!LHSIsNull && !RHSIsNull && |
| 3976 | !Context.typesAreBlockCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3977 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3978 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3979 | } |
| 3980 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3981 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3982 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3983 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3984 | if (!isRelational |
| 3985 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 3986 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3987 | if (!LHSIsNull && !RHSIsNull) { |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3988 | if (!((rType->isPointerType() && rType->getAsPointerType() |
| 3989 | ->getPointeeType()->isVoidType()) |
| 3990 | || (lType->isPointerType() && lType->getAsPointerType() |
| 3991 | ->getPointeeType()->isVoidType()))) |
| 3992 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 3993 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3994 | } |
| 3995 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3996 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 3997 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3998 | |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3999 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4000 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4001 | const PointerType *LPT = lType->getAsPointerType(); |
| 4002 | const PointerType *RPT = rType->getAsPointerType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4003 | bool LPtrToVoid = LPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4004 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4005 | bool RPtrToVoid = RPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4006 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4007 | |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4008 | if (!LPtrToVoid && !RPtrToVoid && |
| 4009 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4010 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4011 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4012 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4013 | return ResultTy; |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4014 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4015 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4016 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4017 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4018 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 4019 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4020 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4021 | } else { |
| 4022 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4023 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4024 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4025 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4026 | return ResultTy; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4027 | } |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4028 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4029 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4030 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4031 | rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4032 | if (!RHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4033 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4034 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4035 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4036 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4037 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4038 | if (lType->isIntegerType() && |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4039 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4040 | if (!LHSIsNull) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4041 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4042 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4043 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4044 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4045 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4046 | // Handle block pointers. |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4047 | if (!isRelational && RHSIsNull |
| 4048 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4049 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4050 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4051 | } |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4052 | if (!isRelational && LHSIsNull |
| 4053 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4054 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4055 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4056 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4057 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4060 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4061 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4062 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4063 | /// types. |
| 4064 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4065 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4066 | bool isRelational) { |
| 4067 | // Check to make sure we're operating on vectors of the same type and width, |
| 4068 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4069 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4070 | if (vType.isNull()) |
| 4071 | return vType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4072 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4073 | QualType lType = lex->getType(); |
| 4074 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4075 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4076 | // For non-floating point types, check for self-comparisons of the form |
| 4077 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4078 | // often indicate logic errors in the program. |
| 4079 | if (!lType->isFloatingType()) { |
| 4080 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4081 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4082 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4083 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4084 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4085 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4086 | // Check for comparisons of floating point operands using != and ==. |
| 4087 | if (!isRelational && lType->isFloatingType()) { |
| 4088 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4089 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4090 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4091 | |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4092 | // FIXME: Vector compare support in the LLVM backend is not fully reliable, |
| 4093 | // just reject all vector comparisons for now. |
| 4094 | if (1) { |
| 4095 | Diag(Loc, diag::err_typecheck_vector_comparison) |
| 4096 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4097 | return QualType(); |
| 4098 | } |
| 4099 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4100 | // Return the type for the comparison, which is the same as vector type for |
| 4101 | // integer vectors, or an integer type of identical size and number of |
| 4102 | // elements for floating point vectors. |
| 4103 | if (lType->isIntegerType()) |
| 4104 | return lType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4105 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4106 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4107 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4108 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4109 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4110 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4111 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4112 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4113 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4114 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4115 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4116 | } |
| 4117 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4118 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4119 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4120 | { |
| 4121 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4122 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4123 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4124 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4125 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4126 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4127 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4128 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4132 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4133 | { |
| 4134 | UsualUnaryConversions(lex); |
| 4135 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4136 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 4137 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4138 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4139 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4142 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4143 | /// is a read-only property; return true if so. A readonly property expression |
| 4144 | /// depends on various declarations and thus must be treated specially. |
| 4145 | /// |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4146 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4147 | { |
| 4148 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4149 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4150 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4151 | QualType BaseType = PropExpr->getBase()->getType(); |
| 4152 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4153 | if (const ObjCInterfaceType *IFTy = |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4154 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 4155 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 4156 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4157 | return true; |
| 4158 | } |
| 4159 | } |
| 4160 | return false; |
| 4161 | } |
| 4162 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4163 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4164 | /// emit an error and return true. If so, return false. |
| 4165 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4166 | SourceLocation OrigLoc = Loc; |
| 4167 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
| 4168 | &Loc); |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4169 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4170 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4171 | if (IsLV == Expr::MLV_Valid) |
| 4172 | return false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4173 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4174 | unsigned Diag = 0; |
| 4175 | bool NeedType = false; |
| 4176 | switch (IsLV) { // C99 6.5.16p2 |
| 4177 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4178 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4179 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4180 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4181 | NeedType = true; |
| 4182 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4183 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4184 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4185 | NeedType = true; |
| 4186 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4187 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4188 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4189 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4190 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4191 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4192 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4193 | case Expr::MLV_IncompleteType: |
| 4194 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4195 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4196 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 4197 | E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4198 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4199 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4200 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4201 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4202 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4203 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4204 | case Expr::MLV_ReadonlyProperty: |
| 4205 | Diag = diag::error_readonly_property_assignment; |
| 4206 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4207 | case Expr::MLV_NoSetterProperty: |
| 4208 | Diag = diag::error_nosetter_property_assignment; |
| 4209 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4210 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4211 | |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4212 | SourceRange Assign; |
| 4213 | if (Loc != OrigLoc) |
| 4214 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4215 | if (NeedType) |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4216 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4217 | else |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4218 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4219 | return true; |
| 4220 | } |
| 4221 | |
| 4222 | |
| 4223 | |
| 4224 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4225 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4226 | SourceLocation Loc, |
| 4227 | QualType CompoundType) { |
| 4228 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4229 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4230 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4231 | |
| 4232 | QualType LHSType = LHS->getType(); |
| 4233 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4234 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4235 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4236 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4237 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4238 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4239 | // Special case of NSObject attributes on c-style pointer types. |
| 4240 | if (ConvTy == IncompatiblePointer && |
| 4241 | ((Context.isObjCNSObjectType(LHSType) && |
| 4242 | Context.isObjCObjectPointerType(RHSType)) || |
| 4243 | (Context.isObjCNSObjectType(RHSType) && |
| 4244 | Context.isObjCObjectPointerType(LHSType)))) |
| 4245 | ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4246 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4247 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 4248 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 4249 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4250 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4251 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 4252 | RHSCheck = ICE->getSubExpr(); |
| 4253 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 4254 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 4255 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4256 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4257 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4258 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 4259 | // And there is a space or other character before the subexpr of the |
| 4260 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | f1e5d4a | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 4261 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 4262 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4263 | Diag(Loc, diag::warn_not_compound_assign) |
| 4264 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 4265 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4266 | } |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4267 | } |
| 4268 | } else { |
| 4269 | // Compound assignment "x += y" |
Eli Friedman | b653af4 | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 4270 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4271 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4272 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4273 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 4274 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4275 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4276 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4277 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 4278 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4279 | // it is the unqualified version of the type of the left operand. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4280 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 4281 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4282 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4283 | // operand. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4284 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4287 | // C99 6.5.17 |
| 4288 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 4289 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4290 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4291 | |
| 4292 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 4293 | // incomplete in C++). |
| 4294 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4295 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 4299 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4300 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 4301 | bool isInc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4302 | if (Op->isTypeDependent()) |
| 4303 | return Context.DependentTy; |
| 4304 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4305 | QualType ResType = Op->getType(); |
| 4306 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4307 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4308 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 4309 | // Decrement of bool is not allowed. |
| 4310 | if (!isInc) { |
| 4311 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 4312 | return QualType(); |
| 4313 | } |
| 4314 | // Increment of bool sets it to true, but is deprecated. |
| 4315 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 4316 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4317 | // OK! |
| 4318 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 4319 | // C99 6.5.2.4p2, 6.5.6p2 |
Douglas Gregor | cde3a2d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 4320 | if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4321 | if (getLangOptions().CPlusPlus) { |
| 4322 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 4323 | << Op->getSourceRange(); |
| 4324 | return QualType(); |
| 4325 | } |
| 4326 | |
| 4327 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4328 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4329 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4330 | if (getLangOptions().CPlusPlus) { |
| 4331 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 4332 | << Op->getType() << Op->getSourceRange(); |
| 4333 | return QualType(); |
| 4334 | } |
| 4335 | |
| 4336 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4337 | << ResType << Op->getSourceRange(); |
Douglas Gregor | cde3a2d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 4338 | } else if (RequireCompleteType(OpLoc, PT->getPointeeType(), |
| 4339 | diag::err_typecheck_arithmetic_incomplete_type, |
| 4340 | Op->getSourceRange(), SourceRange(), |
| 4341 | ResType)) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4342 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4343 | } else if (ResType->isComplexType()) { |
| 4344 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 4345 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4346 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4347 | } else { |
| 4348 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4349 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4350 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4351 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4352 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 4353 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4354 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4355 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4356 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4359 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4360 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4361 | /// where the declaration is needed for type checking. We only need to |
| 4362 | /// handle cases when the expression references a function designator |
| 4363 | /// or is an lvalue. Here are some examples: |
| 4364 | /// - &(x) => x |
| 4365 | /// - &*****f => f for f a function designator. |
| 4366 | /// - &s.xx => s |
| 4367 | /// - &s.zz[1].yy -> s, if zz is an array |
| 4368 | /// - *(x + 1) -> x, if x is an array |
| 4369 | /// - &"123"[2] -> 0 |
| 4370 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4371 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4372 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4373 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 4374 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4375 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4376 | case Stmt::MemberExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4377 | // If this is an arrow operator, the address is an offset from |
| 4378 | // the base's value, so the object the base refers to is |
| 4379 | // irrelevant. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4380 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4381 | return 0; |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4382 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4383 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4384 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4385 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 4386 | // promotion of register arrays earlier. |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4387 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 4388 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 4389 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 4390 | return getPrimaryDecl(ICE->getSubExpr()); |
| 4391 | } |
| 4392 | return 0; |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4393 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4394 | case Stmt::UnaryOperatorClass: { |
| 4395 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4396 | |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4397 | switch(UO->getOpcode()) { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4398 | case UnaryOperator::Real: |
| 4399 | case UnaryOperator::Imag: |
| 4400 | case UnaryOperator::Extension: |
| 4401 | return getPrimaryDecl(UO->getSubExpr()); |
| 4402 | default: |
| 4403 | return 0; |
| 4404 | } |
| 4405 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4406 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4407 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4408 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4409 | // If the result of an implicit cast is an l-value, we care about |
| 4410 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4411 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4412 | default: |
| 4413 | return 0; |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4418 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4419 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4420 | /// Note: The usual conversions are *not* applied to the operand of the & |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4421 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4422 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4423 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4424 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4425 | // Make sure to ignore parentheses in subsequent checks |
| 4426 | op = op->IgnoreParens(); |
| 4427 | |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 4428 | if (op->isTypeDependent()) |
| 4429 | return Context.DependentTy; |
| 4430 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4431 | if (getLangOptions().C99) { |
| 4432 | // Implement C99-only parts of addressof rules. |
| 4433 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 4434 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 4435 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 4436 | // (assuming the deref expression is valid). |
| 4437 | return uOp->getSubExpr()->getType(); |
| 4438 | } |
| 4439 | // Technically, there should be a check for array subscript |
| 4440 | // expressions here, but the result of one is always an lvalue anyway. |
| 4441 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4442 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 4443 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 4444 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4445 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 4446 | // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4447 | // The operand must be either an l-value or a function designator |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4448 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4449 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4450 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 4451 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4452 | return QualType(); |
| 4453 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 4454 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4455 | // The operand cannot be a bit-field |
| 4456 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4457 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 4458 | return QualType(); |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4459 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 4460 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4461 | // The operand cannot be an element of a vector |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4462 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4463 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4464 | return QualType(); |
| 4465 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4466 | // We have an lvalue with a decl. Make sure the decl is not declared |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4467 | // with the register storage-class specifier. |
| 4468 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 4469 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4470 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4471 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4472 | return QualType(); |
| 4473 | } |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4474 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4475 | return Context.OverloadTy; |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4476 | } else if (isa<FieldDecl>(dcl)) { |
| 4477 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4478 | // Could be a pointer to member, though, if there is an explicit |
| 4479 | // scope qualifier for the class. |
| 4480 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 4481 | DeclContext *Ctx = dcl->getDeclContext(); |
| 4482 | if (Ctx && Ctx->isRecord()) |
| 4483 | return Context.getMemberPointerType(op->getType(), |
| 4484 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 4485 | } |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4486 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 4487 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4488 | // As above. |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4489 | if (isa<QualifiedDeclRefExpr>(op) && MD->isInstance()) |
| 4490 | return Context.getMemberPointerType(op->getType(), |
| 4491 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 4492 | } else if (!isa<FunctionDecl>(dcl)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4493 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4494 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4495 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4496 | if (lval == Expr::LV_IncompleteVoidType) { |
| 4497 | // Taking the address of a void variable is technically illegal, but we |
| 4498 | // allow it in cases which are otherwise valid. |
| 4499 | // Example: "extern void x; void* y = &x;". |
| 4500 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 4501 | } |
| 4502 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4503 | // If the operand has type "type", the result has type "pointer to type". |
| 4504 | return Context.getPointerType(op->getType()); |
| 4505 | } |
| 4506 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4507 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4508 | if (Op->isTypeDependent()) |
| 4509 | return Context.DependentTy; |
| 4510 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4511 | UsualUnaryConversions(Op); |
| 4512 | QualType Ty = Op->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4513 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4514 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 4515 | // incomplete type or void. It would be possible to warn about dereferencing |
| 4516 | // a void pointer, but it's completely well-defined, and such a warning is |
| 4517 | // unlikely to catch any mistakes. |
| 4518 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4519 | return PT->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4520 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4521 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4522 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4523 | return QualType(); |
| 4524 | } |
| 4525 | |
| 4526 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 4527 | tok::TokenKind Kind) { |
| 4528 | BinaryOperator::Opcode Opc; |
| 4529 | switch (Kind) { |
| 4530 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4531 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 4532 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4533 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 4534 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 4535 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 4536 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 4537 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 4538 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 4539 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 4540 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 4541 | case tok::less: Opc = BinaryOperator::LT; break; |
| 4542 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 4543 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 4544 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 4545 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 4546 | case tok::amp: Opc = BinaryOperator::And; break; |
| 4547 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 4548 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 4549 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 4550 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 4551 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 4552 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 4553 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 4554 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 4555 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 4556 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 4557 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 4558 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 4559 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 4560 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 4561 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 4562 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 4563 | } |
| 4564 | return Opc; |
| 4565 | } |
| 4566 | |
| 4567 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 4568 | tok::TokenKind Kind) { |
| 4569 | UnaryOperator::Opcode Opc; |
| 4570 | switch (Kind) { |
| 4571 | default: assert(0 && "Unknown unary op!"); |
| 4572 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 4573 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 4574 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 4575 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 4576 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 4577 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 4578 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 4579 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4580 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 4581 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 4582 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 4583 | } |
| 4584 | return Opc; |
| 4585 | } |
| 4586 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4587 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 4588 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 4589 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4590 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 4591 | unsigned Op, |
| 4592 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4593 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4594 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4595 | // The following two variables are used for compound assignment operators |
| 4596 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 4597 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4598 | |
| 4599 | switch (Opc) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4600 | case BinaryOperator::Assign: |
| 4601 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 4602 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4603 | case BinaryOperator::PtrMemD: |
| 4604 | case BinaryOperator::PtrMemI: |
| 4605 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 4606 | Opc == BinaryOperator::PtrMemI); |
| 4607 | break; |
| 4608 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4609 | case BinaryOperator::Div: |
| 4610 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 4611 | break; |
| 4612 | case BinaryOperator::Rem: |
| 4613 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 4614 | break; |
| 4615 | case BinaryOperator::Add: |
| 4616 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 4617 | break; |
| 4618 | case BinaryOperator::Sub: |
| 4619 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 4620 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4621 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4622 | case BinaryOperator::Shr: |
| 4623 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 4624 | break; |
| 4625 | case BinaryOperator::LE: |
| 4626 | case BinaryOperator::LT: |
| 4627 | case BinaryOperator::GE: |
| 4628 | case BinaryOperator::GT: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4629 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4630 | break; |
| 4631 | case BinaryOperator::EQ: |
| 4632 | case BinaryOperator::NE: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4633 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | case BinaryOperator::And: |
| 4636 | case BinaryOperator::Xor: |
| 4637 | case BinaryOperator::Or: |
| 4638 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 4639 | break; |
| 4640 | case BinaryOperator::LAnd: |
| 4641 | case BinaryOperator::LOr: |
| 4642 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 4643 | break; |
| 4644 | case BinaryOperator::MulAssign: |
| 4645 | case BinaryOperator::DivAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4646 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 4647 | CompLHSTy = CompResultTy; |
| 4648 | if (!CompResultTy.isNull()) |
| 4649 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4650 | break; |
| 4651 | case BinaryOperator::RemAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4652 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 4653 | CompLHSTy = CompResultTy; |
| 4654 | if (!CompResultTy.isNull()) |
| 4655 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4656 | break; |
| 4657 | case BinaryOperator::AddAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4658 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4659 | if (!CompResultTy.isNull()) |
| 4660 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4661 | break; |
| 4662 | case BinaryOperator::SubAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4663 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4664 | if (!CompResultTy.isNull()) |
| 4665 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4666 | break; |
| 4667 | case BinaryOperator::ShlAssign: |
| 4668 | case BinaryOperator::ShrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4669 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 4670 | CompLHSTy = CompResultTy; |
| 4671 | if (!CompResultTy.isNull()) |
| 4672 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4673 | break; |
| 4674 | case BinaryOperator::AndAssign: |
| 4675 | case BinaryOperator::XorAssign: |
| 4676 | case BinaryOperator::OrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4677 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 4678 | CompLHSTy = CompResultTy; |
| 4679 | if (!CompResultTy.isNull()) |
| 4680 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4681 | break; |
| 4682 | case BinaryOperator::Comma: |
| 4683 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 4684 | break; |
| 4685 | } |
| 4686 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4687 | return ExprError(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4688 | if (CompResultTy.isNull()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4689 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 4690 | else |
| 4691 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4692 | CompLHSTy, CompResultTy, |
| 4693 | OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4696 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4697 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 4698 | tok::TokenKind Kind, |
| 4699 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4700 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 4701 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4702 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4703 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 4704 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4705 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4706 | if (getLangOptions().CPlusPlus && |
| 4707 | (lhs->getType()->isOverloadableType() || |
| 4708 | rhs->getType()->isOverloadableType())) { |
| 4709 | // Find all of the overloaded operators visible from this |
| 4710 | // point. We perform both an operator-name lookup from the local |
| 4711 | // scope and an argument-dependent lookup based on the types of |
| 4712 | // the arguments. |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4713 | FunctionSet Functions; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4714 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 4715 | if (OverOp != OO_None) { |
| 4716 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 4717 | Functions); |
| 4718 | Expr *Args[2] = { lhs, rhs }; |
| 4719 | DeclarationName OpName |
| 4720 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4721 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4722 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4724 | // Build the (potentially-overloaded, potentially-dependent) |
| 4725 | // binary operation. |
| 4726 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4729 | // Build a built-in binary operation. |
| 4730 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4733 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| 4734 | unsigned OpcIn, |
| 4735 | ExprArg InputArg) { |
| 4736 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4737 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4738 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4739 | Expr *Input = (Expr *)InputArg.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4740 | QualType resultType; |
| 4741 | switch (Opc) { |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4742 | case UnaryOperator::PostInc: |
| 4743 | case UnaryOperator::PostDec: |
| 4744 | case UnaryOperator::OffsetOf: |
| 4745 | assert(false && "Invalid unary operator"); |
| 4746 | break; |
| 4747 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4748 | case UnaryOperator::PreInc: |
| 4749 | case UnaryOperator::PreDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4750 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4751 | Opc == UnaryOperator::PreInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4752 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4753 | case UnaryOperator::AddrOf: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4754 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4755 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4756 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4757 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4758 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4759 | break; |
| 4760 | case UnaryOperator::Plus: |
| 4761 | case UnaryOperator::Minus: |
| 4762 | UsualUnaryConversions(Input); |
| 4763 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4764 | if (resultType->isDependentType()) |
| 4765 | break; |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4766 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4767 | break; |
| 4768 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4769 | resultType->isEnumeralType()) |
| 4770 | break; |
| 4771 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4772 | Opc == UnaryOperator::Plus && |
| 4773 | resultType->isPointerType()) |
| 4774 | break; |
| 4775 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4776 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4777 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4778 | case UnaryOperator::Not: // bitwise complement |
| 4779 | UsualUnaryConversions(Input); |
| 4780 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4781 | if (resultType->isDependentType()) |
| 4782 | break; |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4783 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4784 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4785 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4786 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4787 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4788 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4789 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4790 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4791 | break; |
| 4792 | case UnaryOperator::LNot: // logical negation |
| 4793 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 4794 | DefaultFunctionArrayConversion(Input); |
| 4795 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4796 | if (resultType->isDependentType()) |
| 4797 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4798 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4799 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4800 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4801 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4802 | // In C++, it's bool. C++ 5.3.1p8 |
| 4803 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4804 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4805 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4806 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4807 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4808 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4809 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4810 | resultType = Input->getType(); |
| 4811 | break; |
| 4812 | } |
| 4813 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4814 | return ExprError(); |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4815 | |
| 4816 | InputArg.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4817 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4820 | // Unary Operators. 'Tok' is the token for the operator. |
| 4821 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 4822 | tok::TokenKind Op, ExprArg input) { |
| 4823 | Expr *Input = (Expr*)input.get(); |
| 4824 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 4825 | |
| 4826 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 4827 | // Find all of the overloaded operators visible from this |
| 4828 | // point. We perform both an operator-name lookup from the local |
| 4829 | // scope and an argument-dependent lookup based on the types of |
| 4830 | // the arguments. |
| 4831 | FunctionSet Functions; |
| 4832 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 4833 | if (OverOp != OO_None) { |
| 4834 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 4835 | Functions); |
| 4836 | DeclarationName OpName |
| 4837 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4838 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 4839 | } |
| 4840 | |
| 4841 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 4842 | } |
| 4843 | |
| 4844 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 4845 | } |
| 4846 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4847 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4848 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 4849 | SourceLocation LabLoc, |
| 4850 | IdentifierInfo *LabelII) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4851 | // Look up the record for this label identifier. |
Chris Lattner | 2616d8c | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 4852 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4853 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4854 | // If we haven't seen this label yet, create a forward reference. It |
| 4855 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | b88d81c | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 4856 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4857 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4858 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4859 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4860 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4861 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4864 | Sema::OwningExprResult |
| 4865 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 4866 | SourceLocation RPLoc) { // "({..})" |
| 4867 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4868 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4869 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4870 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4871 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | aa25759 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 4872 | if (isFileScope) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4873 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4874 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4875 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4876 | // example, it is not possible to goto into a stmt expression apparently. |
| 4877 | // More semantic analysis is needed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4878 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4879 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4880 | // as the type of the stmtexpr. |
| 4881 | QualType Ty = Context.VoidTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4882 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4883 | if (!Compound->body_empty()) { |
| 4884 | Stmt *LastStmt = Compound->body_back(); |
| 4885 | // If LastStmt is a label, skip down through into the body. |
| 4886 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4887 | LastStmt = Label->getSubStmt(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4888 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4889 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4890 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4891 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4892 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4893 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 4894 | // expressions are not lvalues. |
| 4895 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4896 | substmt.release(); |
| 4897 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4898 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4899 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4900 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4901 | SourceLocation BuiltinLoc, |
| 4902 | SourceLocation TypeLoc, |
| 4903 | TypeTy *argty, |
| 4904 | OffsetOfComponent *CompPtr, |
| 4905 | unsigned NumComponents, |
| 4906 | SourceLocation RPLoc) { |
| 4907 | // FIXME: This function leaks all expressions in the offset components on |
| 4908 | // error. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4909 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4910 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4911 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4912 | bool Dependent = ArgTy->isDependentType(); |
| 4913 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4914 | // We must have at least one component that refers to the type, and the first |
| 4915 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4916 | // a struct/union/class. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4917 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4918 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4919 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4920 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 4921 | // with an incomplete type would be illegal. |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 4922 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4923 | // Otherwise, create a null pointer as the base, and iteratively process |
| 4924 | // the offsetof designators. |
| 4925 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 4926 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4927 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4928 | ArgTy, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4929 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4930 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4931 | // GCC extension, diagnose them. |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4932 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 4933 | // a system header! |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4934 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4935 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4936 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4937 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4938 | if (!Dependent) { |
Eli Friedman | c24ae00 | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 4939 | bool DidWarnAboutNonPOD = false; |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 4940 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4941 | // FIXME: Dependent case loses a lot of information here. And probably |
| 4942 | // leaks like a sieve. |
| 4943 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4944 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4945 | if (OC.isBrackets) { |
| 4946 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 4947 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 4948 | if (!AT) { |
| 4949 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4950 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 4951 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4955 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4956 | // Promote the array so it looks more like a normal array subscript |
| 4957 | // expression. |
| 4958 | DefaultFunctionArrayConversion(Res); |
| 4959 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4960 | // C99 6.5.2.1p1 |
| 4961 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4962 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4963 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4964 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 4965 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4966 | << Idx->getSourceRange()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4967 | |
| 4968 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 4969 | OC.LocEnd); |
| 4970 | continue; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4971 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4972 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4973 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 4974 | if (!RC) { |
| 4975 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4976 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 4977 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4978 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 4979 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4980 | // Get the decl corresponding to this. |
| 4981 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 4982 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 4983 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | bbceaea | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 4984 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 4985 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 4986 | << Res->getType()); |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 4987 | DidWarnAboutNonPOD = true; |
| 4988 | } |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4991 | FieldDecl *MemberDecl |
| 4992 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 4993 | LookupMemberName) |
| 4994 | .getAsDecl()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4995 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4996 | if (!MemberDecl) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4997 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 4998 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4999 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5000 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5001 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5002 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5003 | Res = BuildAnonymousStructUnionMemberReference( |
| 5004 | SourceLocation(), MemberDecl, Res, SourceLocation()).takeAs<Expr>(); |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5005 | } else { |
| 5006 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5007 | // doesn't matter here. |
| 5008 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5009 | MemberDecl->getType().getNonReferenceType()); |
| 5010 | } |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5011 | } |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5012 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5013 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5014 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5015 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5019 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5020 | TypeTy *arg1,TypeTy *arg2, |
| 5021 | SourceLocation RPLoc) { |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5022 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 5023 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5024 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5025 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5026 | |
Douglas Gregor | e621150 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5027 | if (getLangOptions().CPlusPlus) { |
| 5028 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5029 | << SourceRange(BuiltinLoc, RPLoc); |
| 5030 | return ExprError(); |
| 5031 | } |
| 5032 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5033 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5034 | argT1, argT2, RPLoc)); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5037 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5038 | ExprArg cond, |
| 5039 | ExprArg expr1, ExprArg expr2, |
| 5040 | SourceLocation RPLoc) { |
| 5041 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5042 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5043 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5044 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5045 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5046 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5047 | QualType resType; |
Douglas Gregor | dd4ae3f | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5048 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5049 | resType = Context.DependentTy; |
| 5050 | } else { |
| 5051 | // The conditional expression is required to be a constant expression. |
| 5052 | llvm::APSInt condEval(32); |
| 5053 | SourceLocation ExpLoc; |
| 5054 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5055 | return ExprError(Diag(ExpLoc, |
| 5056 | diag::err_typecheck_choose_expr_requires_constant) |
| 5057 | << CondExpr->getSourceRange()); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5058 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5059 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5060 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 5061 | } |
| 5062 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5063 | cond.release(); expr1.release(); expr2.release(); |
| 5064 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 5065 | resType, RPLoc)); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5068 | //===----------------------------------------------------------------------===// |
| 5069 | // Clang Extensions. |
| 5070 | //===----------------------------------------------------------------------===// |
| 5071 | |
| 5072 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5073 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5074 | // Analyze block parameters. |
| 5075 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5076 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5077 | // Add BSI to CurBlock. |
| 5078 | BSI->PrevBlockInfo = CurBlock; |
| 5079 | CurBlock = BSI; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5080 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5081 | BSI->ReturnType = 0; |
| 5082 | BSI->TheScope = BlockScope; |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5083 | BSI->hasBlockDeclRefExprs = false; |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5084 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5085 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5086 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5087 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5088 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5091 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5092 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5093 | |
| 5094 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5095 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Fariborz Jahanian | 262c0d7 | 2009-05-18 23:17:46 +0000 | [diff] [blame] | 5096 | ProcessDeclAttributes(CurBlock->TheDecl, ParamInfo); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5097 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5098 | |
Mike Stump | 458287d | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5099 | if (T->isArrayType()) { |
| 5100 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5101 | diag::err_block_returns_array); |
| 5102 | return; |
| 5103 | } |
| 5104 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5105 | // The parameter list is optional, if there was none, assume (). |
| 5106 | if (!T->isFunctionType()) |
| 5107 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 5108 | |
| 5109 | CurBlock->hasPrototype = true; |
| 5110 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5111 | // Check for a valid sentinel attribute on this block. |
| 5112 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
| 5113 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5114 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5115 | // FIXME: remove the attribute. |
| 5116 | } |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5117 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
| 5118 | |
| 5119 | // Do not allow returning a objc interface by-value. |
| 5120 | if (RetTy->isObjCInterfaceType()) { |
| 5121 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5122 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5123 | return; |
| 5124 | } |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5125 | return; |
| 5126 | } |
| 5127 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5128 | // Analyze arguments to block. |
| 5129 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 5130 | "Not a function declarator!"); |
| 5131 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5132 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5133 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 5134 | CurBlock->isVariadic = true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5135 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5136 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 5137 | // no arguments, not a function that takes a single void argument. |
| 5138 | if (FTI.hasPrototype && |
| 5139 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5140 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 5141 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5142 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5143 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5144 | } else if (FTI.hasPrototype) { |
| 5145 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5146 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5147 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5148 | } |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5149 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5150 | CurBlock->Params.size()); |
Fariborz Jahanian | 536f73d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 5151 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Fariborz Jahanian | 262c0d7 | 2009-05-18 23:17:46 +0000 | [diff] [blame] | 5152 | ProcessDeclAttributes(CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5153 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 5154 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 5155 | // If this has an identifier, add it to the scope stack. |
| 5156 | if ((*AI)->getIdentifier()) |
| 5157 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5158 | |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5159 | // Check for a valid sentinel attribute on this block. |
| 5160 | if (!CurBlock->isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
| 5161 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5162 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5163 | // FIXME: remove the attribute. |
| 5164 | } |
| 5165 | |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5166 | // Analyze the return type. |
| 5167 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5168 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
| 5169 | |
| 5170 | // Do not allow returning a objc interface by-value. |
| 5171 | if (RetTy->isObjCInterfaceType()) { |
| 5172 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5173 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5174 | } else if (!RetTy->isDependentType()) |
| 5175 | CurBlock->ReturnType = RetTy.getTypePtr(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 5179 | /// is invoked to pop the information about the block from the action impl. |
| 5180 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 5181 | // Ensure that CurBlock is deleted. |
| 5182 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5183 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5184 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 5185 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5186 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | eb4d4a5 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 5187 | PopDeclContext(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5188 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5189 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 5193 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5194 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 5195 | StmtArg body, Scope *CurScope) { |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5196 | // If blocks are disabled, emit an error. |
| 5197 | if (!LangOpts.Blocks) |
| 5198 | Diag(CaretLoc, diag::err_blocks_disable); |
| 5199 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5200 | // Ensure that CurBlock is deleted. |
| 5201 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5202 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5203 | PopDeclContext(); |
| 5204 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5205 | // Pop off CurBlock, handle nested blocks. |
| 5206 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5207 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5208 | QualType RetTy = Context.VoidTy; |
| 5209 | if (BSI->ReturnType) |
| 5210 | RetTy = QualType(BSI->ReturnType, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5211 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5212 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 5213 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 5214 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5215 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5216 | QualType BlockTy; |
| 5217 | if (!BSI->hasPrototype) |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5218 | BlockTy = Context.getFunctionNoProtoType(RetTy); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5219 | else |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5220 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Argiris Kirtzidis | 65b9964 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5221 | BSI->isVariadic, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5222 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5223 | // FIXME: Check that return/parameter types are complete/non-abstract |
| 5224 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5225 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5226 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5227 | // If needed, diagnose invalid gotos and switches in the block. |
| 5228 | if (CurFunctionNeedsScopeChecking) |
| 5229 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 5230 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
| 5231 | |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5232 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5233 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 5234 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5237 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 5238 | ExprArg expr, TypeTy *type, |
| 5239 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5240 | QualType T = QualType::getFromOpaquePtr(type); |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5241 | Expr *E = static_cast<Expr*>(expr.get()); |
| 5242 | Expr *OrigExpr = E; |
| 5243 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5244 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5245 | |
| 5246 | // Get the va_list type |
| 5247 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5248 | if (VaListType->isArrayType()) { |
| 5249 | // Deal with implicit array decay; for example, on x86-64, |
| 5250 | // va_list is an array, but it's supposed to decay to |
| 5251 | // a pointer for va_arg. |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5252 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5253 | // Make sure the input expression also decays appropriately. |
| 5254 | UsualUnaryConversions(E); |
| 5255 | } else { |
| 5256 | // Otherwise, the va_list argument must be an l-value because |
| 5257 | // it is modified by va_arg. |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5258 | if (!E->isTypeDependent() && |
| 5259 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5260 | return ExprError(); |
| 5261 | } |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5262 | |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5263 | if (!E->isTypeDependent() && |
| 5264 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5265 | return ExprError(Diag(E->getLocStart(), |
| 5266 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5267 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 89a72c5 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 5268 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5269 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5270 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5271 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5272 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5273 | expr.release(); |
| 5274 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 5275 | RPLoc)); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5278 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5279 | // The type of __null will be int or long, depending on the size of |
| 5280 | // pointers on the target. |
| 5281 | QualType Ty; |
| 5282 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 5283 | Ty = Context.IntTy; |
| 5284 | else |
| 5285 | Ty = Context.LongTy; |
| 5286 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5287 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5290 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 5291 | SourceLocation Loc, |
| 5292 | QualType DstType, QualType SrcType, |
| 5293 | Expr *SrcExpr, const char *Flavor) { |
| 5294 | // Decode the result (notice that AST's are still created for extensions). |
| 5295 | bool isInvalid = false; |
| 5296 | unsigned DiagKind; |
| 5297 | switch (ConvTy) { |
| 5298 | default: assert(0 && "Unknown conversion type"); |
| 5299 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5300 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5301 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 5302 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5303 | case IntToPointer: |
| 5304 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 5305 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5306 | case IncompatiblePointer: |
| 5307 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 5308 | break; |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5309 | case IncompatiblePointerSign: |
| 5310 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 5311 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5312 | case FunctionVoidPointer: |
| 5313 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 5314 | break; |
| 5315 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 5316 | // If the qualifiers lost were because we were applying the |
| 5317 | // (deprecated) C++ conversion from a string literal to a char* |
| 5318 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 5319 | // Ideally, this check would be performed in |
| 5320 | // CheckPointerTypesForAssignment. However, that would require a |
| 5321 | // bit of refactoring (so that the second argument is an |
| 5322 | // expression, rather than a type), which should be done as part |
| 5323 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 5324 | // C++ semantics. |
| 5325 | if (getLangOptions().CPlusPlus && |
| 5326 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 5327 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5328 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 5329 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5330 | case IntToBlockPointer: |
| 5331 | DiagKind = diag::err_int_to_block_pointer; |
| 5332 | break; |
| 5333 | case IncompatibleBlockPointer: |
Mike Stump | d331e75 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 5334 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5335 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5336 | case IncompatibleObjCQualifiedId: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5337 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5338 | // it can give a more specific diagnostic. |
| 5339 | DiagKind = diag::warn_incompatible_qualified_id; |
| 5340 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5341 | case IncompatibleVectors: |
| 5342 | DiagKind = diag::warn_incompatible_vectors; |
| 5343 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5344 | case Incompatible: |
| 5345 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 5346 | isInvalid = true; |
| 5347 | break; |
| 5348 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5349 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5350 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 5351 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5352 | return isInvalid; |
| 5353 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5354 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 5355 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5356 | llvm::APSInt ICEResult; |
| 5357 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 5358 | if (Result) |
| 5359 | *Result = ICEResult; |
| 5360 | return false; |
| 5361 | } |
| 5362 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5363 | Expr::EvalResult EvalResult; |
| 5364 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5365 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5366 | EvalResult.HasSideEffects) { |
| 5367 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 5368 | |
| 5369 | if (EvalResult.Diag) { |
| 5370 | // We only show the note if it's not the usual "invalid subexpression" |
| 5371 | // or if it's actually in a subexpression. |
| 5372 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 5373 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 5374 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 5375 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5376 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5377 | return true; |
| 5378 | } |
| 5379 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5380 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 5381 | E->getSourceRange(); |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5382 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5383 | if (EvalResult.Diag && |
| 5384 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 5385 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5386 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5387 | if (Result) |
| 5388 | *Result = EvalResult.Val.getInt(); |
| 5389 | return false; |
| 5390 | } |