Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Douglas Gregor | 48f3bb9 | 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 | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 41 | // See if the decl is deprecated. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 42 | if (D->getAttr<DeprecatedAttr>(Context)) { |
Douglas Gregor | 48f3bb9 | 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 | f15970c | 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. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 51 | isSilenced = ND->getAttr<DeprecatedAttr>(Context); |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 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 | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 61 | MD = Impl->getClassInterface()->getMethod(Context, |
| 62 | MD->getSelector(), |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 63 | MD->isInstanceMethod()); |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 64 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(Context); |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!isSilenced) |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 70 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 71 | } |
| 72 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 73 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 74 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 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 | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 80 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 81 | |
| 82 | // See if the decl is unavailable |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 83 | if (D->getAttr<UnavailableAttr>(Context)) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 84 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 86 | } |
| 87 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Fariborz Jahanian | 5b53005 | 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 | { |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 98 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(Context); |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 99 | if (!attr) |
| 100 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 101 | int sentinelPos = attr->getSentinel(); |
| 102 | int nullPos = attr->getNullPos(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 103 | |
Mike Stump | 390b4cc | 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 | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 106 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 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 | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 120 | } |
Fariborz Jahanian | 236673e | 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 | daf0415 | 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 | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 156 | else |
| 157 | return; |
| 158 | |
| 159 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 160 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 161 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 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 | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 171 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 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 | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 181 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 182 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 183 | } |
| 184 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Douglas Gregor | 4b2d3f7 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 192 | //===----------------------------------------------------------------------===// |
| 193 | // Standard Promotions and Conversions |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | |
Chris Lattner | e7a2e91 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 201 | if (Ty->isFunctionType()) |
| 202 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 67d33d8 | 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). |
Argyrios Kyrtzidis | c39a3d7 | 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 | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 217 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty)); |
| 218 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | fc24e44 | 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 | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 227 | FieldDecl *Field = E->getBitField(); |
| 228 | if (!Field) |
Douglas Gregor | fc24e44 | 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 | e7a2e91 | 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 | fc24e44 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | ImpCastExprToType(Expr, Context.IntTy); |
Douglas Gregor | fc24e44 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 290 | return Expr; |
| 291 | } |
| 292 | |
Chris Lattner | 05faf17 | 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 | 312531a | 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 | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 313 | DefaultArgumentPromotion(Expr); |
| 314 | |
Chris Lattner | 312531a | 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 | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 312531a | 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 | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
Chris Lattner | e7a2e91 | 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 | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 338 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 339 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 340 | |
| 341 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 342 | |
Chris Lattner | e7a2e91 | 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 | b77792e | 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 | eb8f306 | 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 | 2d833e3 | 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 | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 367 | QualType destType = UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 368 | if (!isCompAssign) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 369 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 370 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | eb8f306 | 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 | 76a642f | 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 | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 388 | |
Chris Lattner | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 433 | return rhs; |
| 434 | } else { // handle "_Complex double, double". |
Chris Lattner | e7a2e91 | 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 | 5b1f3f0 | 2008-12-10 23:30:05 +0000 | [diff] [blame] | 443 | if (rhs->isIntegerType()) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 444 | // convert rhs to the lhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 445 | return lhs; |
| 446 | } |
Anders Carlsson | 5b1f3f0 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 452 | // convert lhs to the rhs floating point type. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 453 | return rhs; |
| 454 | } |
Anders Carlsson | 5b1f3f0 | 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 | e7a2e91 | 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 | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 462 | if (result > 0) // convert the rhs |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 463 | return lhs; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 464 | assert(result < 0 && "illegal float comparison"); |
| 465 | return rhs; // convert the lhs |
Chris Lattner | e7a2e91 | 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 | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 474 | rhsComplexInt->getElementType()) >= 0) |
| 475 | return lhs; // convert the rhs |
Chris Lattner | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 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 | e7a2e91 | 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 | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 518 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 523 | /// |
| 524 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 525 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | assert(NumStringToks && "Must have at least one string!"); |
| 527 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 528 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 530 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 535 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 536 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 537 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 538 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 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 | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 543 | |
Chris Lattner | a7ad98f | 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 | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 548 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 549 | ArrayType::Normal, 0); |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 550 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | 2085fd6 | 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())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | 639e2d3 | 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 | 17f3a6d | 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 | 639e2d3 | 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 | 17f3a6d | 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 | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 603 | |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 609 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 610 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | 0d755ad | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 611 | /// identifier is used in a function call context. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 612 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 613 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd965b9 | 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 | ebc07d5 | 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 | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 620 | isAddressOfOperand); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Douglas Gregor | 1a49af9 | 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 | ebc07d5 | 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 | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 630 | MarkDeclarationReferenced(Loc, D); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 631 | if (SS && !SS->isEmpty()) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 632 | return new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
| 633 | ValueDependent, SS->getRange(), |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 634 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 635 | } else |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 636 | return new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 639 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 640 | /// variable corresponding to the anonymous union or struct whose type |
| 641 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 642 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 643 | RecordDecl *Record) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 644 | assert(Record->isAnonymousStructOrUnion() && |
| 645 | "Record must be an anonymous struct or union!"); |
| 646 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 647 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 648 | // operation rather than a slow walk through DeclContext's vector (which |
| 649 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 650 | DeclContext *Ctx = Record->getDeclContext(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 651 | for (DeclContext::decl_iterator D = Ctx->decls_begin(Context), |
| 652 | DEnd = Ctx->decls_end(Context); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 653 | D != DEnd; ++D) { |
| 654 | if (*D == Record) { |
| 655 | // The object for the anonymous struct/union directly |
| 656 | // follows its type in the list of declarations. |
| 657 | ++D; |
| 658 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 659 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 660 | return *D; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | assert(false && "Missing object for anonymous record"); |
| 665 | return 0; |
| 666 | } |
| 667 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 668 | /// \brief Given a field that represents a member of an anonymous |
| 669 | /// struct/union, build the path from that field's context to the |
| 670 | /// actual member. |
| 671 | /// |
| 672 | /// Construct the sequence of field member references we'll have to |
| 673 | /// perform to get to the field in the anonymous union/struct. The |
| 674 | /// list of members is built from the field outward, so traverse it |
| 675 | /// backwards to go from an object in the current context to the field |
| 676 | /// we found. |
| 677 | /// |
| 678 | /// \returns The variable from which the field access should begin, |
| 679 | /// for an anonymous struct/union that is not a member of another |
| 680 | /// class. Otherwise, returns NULL. |
| 681 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 682 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 683 | assert(Field->getDeclContext()->isRecord() && |
| 684 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 685 | && "Field must be stored inside an anonymous struct or union"); |
| 686 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 687 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 688 | VarDecl *BaseObject = 0; |
| 689 | DeclContext *Ctx = Field->getDeclContext(); |
| 690 | do { |
| 691 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 692 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 693 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 694 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 695 | else { |
| 696 | BaseObject = cast<VarDecl>(AnonObject); |
| 697 | break; |
| 698 | } |
| 699 | Ctx = Ctx->getParent(); |
| 700 | } while (Ctx->isRecord() && |
| 701 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 702 | |
| 703 | return BaseObject; |
| 704 | } |
| 705 | |
| 706 | Sema::OwningExprResult |
| 707 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 708 | FieldDecl *Field, |
| 709 | Expr *BaseObjectExpr, |
| 710 | SourceLocation OpLoc) { |
| 711 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 712 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
| 713 | AnonFields); |
| 714 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 715 | // Build the expression that refers to the base object, from |
| 716 | // which we will build a sequence of member references to each |
| 717 | // of the anonymous union objects and, eventually, the field we |
| 718 | // found via name lookup. |
| 719 | bool BaseObjectIsPointer = false; |
| 720 | unsigned ExtraQuals = 0; |
| 721 | if (BaseObject) { |
| 722 | // BaseObject is an anonymous struct/union variable (and is, |
| 723 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 724 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 725 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 726 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 727 | SourceLocation()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 728 | ExtraQuals |
| 729 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 730 | } else if (BaseObjectExpr) { |
| 731 | // The caller provided the base object expression. Determine |
| 732 | // whether its a pointer and whether it adds any qualifiers to the |
| 733 | // anonymous struct/union fields we're looking into. |
| 734 | QualType ObjectType = BaseObjectExpr->getType(); |
| 735 | if (const PointerType *ObjectPtr = ObjectType->getAsPointerType()) { |
| 736 | BaseObjectIsPointer = true; |
| 737 | ObjectType = ObjectPtr->getPointeeType(); |
| 738 | } |
| 739 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 740 | } else { |
| 741 | // We've found a member of an anonymous struct/union that is |
| 742 | // inside a non-anonymous struct/union, so in a well-formed |
| 743 | // program our base object expression is "this". |
| 744 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 745 | if (!MD->isStatic()) { |
| 746 | QualType AnonFieldType |
| 747 | = Context.getTagDeclType( |
| 748 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 749 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 750 | if ((Context.getCanonicalType(AnonFieldType) |
| 751 | == Context.getCanonicalType(ThisType)) || |
| 752 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 753 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 754 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 755 | MD->getThisType(Context)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 756 | BaseObjectIsPointer = true; |
| 757 | } |
| 758 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 759 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 760 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 761 | } |
| 762 | ExtraQuals = MD->getTypeQualifiers(); |
| 763 | } |
| 764 | |
| 765 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 766 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 767 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // Build the implicit member references to the field of the |
| 771 | // anonymous struct/union. |
| 772 | Expr *Result = BaseObjectExpr; |
| 773 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 774 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 775 | FI != FIEnd; ++FI) { |
| 776 | QualType MemberType = (*FI)->getType(); |
| 777 | if (!(*FI)->isMutable()) { |
| 778 | unsigned combinedQualifiers |
| 779 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 780 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 781 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 782 | MarkDeclarationReferenced(Loc, *FI); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 783 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 784 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 785 | BaseObjectIsPointer = false; |
| 786 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 789 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 792 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 793 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 794 | /// performs lookup on that name and returns an expression that refers |
| 795 | /// to that name. This routine isn't directly called from the parser, |
| 796 | /// because the parser doesn't know about DeclarationName. Rather, |
| 797 | /// this routine is called by ActOnIdentifierExpr, |
| 798 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 799 | /// which form the DeclarationName from the corresponding syntactic |
| 800 | /// forms. |
| 801 | /// |
| 802 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 803 | /// function call context. LookupCtx is only used for a C++ |
| 804 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 805 | /// the identifier must be a member of. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 806 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 807 | /// isAddressOfOperand means that this expression is the direct operand |
| 808 | /// of an address-of operator. This matters because this is the only |
| 809 | /// situation where a qualified name referencing a non-static member may |
| 810 | /// appear outside a member function of this class. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 811 | Sema::OwningExprResult |
| 812 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 813 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 814 | const CXXScopeSpec *SS, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 815 | bool isAddressOfOperand) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 816 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 817 | if (SS && SS->isInvalid()) |
| 818 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 819 | |
| 820 | // C++ [temp.dep.expr]p3: |
| 821 | // An id-expression is type-dependent if it contains: |
| 822 | // -- a nested-name-specifier that contains a class-name that |
| 823 | // names a dependent type. |
Douglas Gregor | 00c4486 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 824 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 825 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 826 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
| 827 | Loc, SS->getRange(), |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 828 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()))); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 831 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 832 | false, true, Loc); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 834 | if (Lookup.isAmbiguous()) { |
| 835 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 836 | SS && SS->isSet() ? SS->getRange() |
| 837 | : SourceRange()); |
| 838 | return ExprError(); |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | NamedDecl *D = Lookup.getAsDecl(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 843 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 844 | // well. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 845 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 846 | if (II && getCurMethodDecl()) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 847 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 848 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 849 | // found a decl, but that decl is outside the current instance method (i.e. |
| 850 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 851 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 852 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 853 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 854 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 855 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 856 | ClassDeclared)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 857 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 858 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 859 | return ExprError(); |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 860 | |
| 861 | // If we're referencing an invalid decl, just return this as a silent |
| 862 | // error node. The error diagnostic was already emitted on the decl. |
| 863 | if (IV->isInvalidDecl()) |
| 864 | return ExprError(); |
| 865 | |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 866 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 867 | // If a class method attemps to use a free standing ivar, this is |
| 868 | // an error. |
| 869 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 870 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 871 | << IV->getDeclName()); |
| 872 | // If a class method uses a global variable, even if an ivar with |
| 873 | // same name exists, use the global. |
| 874 | if (!IsClsMethod) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 875 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 876 | ClassDeclared != IFace) |
| 877 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 878 | // FIXME: This should use a new expr for a direct reference, don't |
| 879 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 880 | IdentifierInfo &II = Context.Idents.get("self"); |
| 881 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 882 | MarkDeclarationReferenced(Loc, IV); |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 883 | return Owned(new (Context) |
| 884 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 885 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 886 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 889 | else if (getCurMethodDecl()->isInstanceMethod()) { |
| 890 | // We should warn if a local variable hides an ivar. |
| 891 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 892 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 893 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(Context, II, |
| 894 | ClassDeclared)) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 895 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 896 | IFace == ClassDeclared) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 897 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 898 | } |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 899 | } |
Steve Naroff | 76de9d7 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 900 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 901 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 902 | QualType T; |
| 903 | |
| 904 | if (getCurMethodDecl()->isInstanceMethod()) |
| 905 | T = Context.getPointerType(Context.getObjCInterfaceType( |
| 906 | getCurMethodDecl()->getClassInterface())); |
| 907 | else |
| 908 | T = Context.getObjCClassType(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 909 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 910 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 911 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 913 | // Determine whether this name might be a candidate for |
| 914 | // argument-dependent lookup. |
| 915 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 916 | HasTrailingLParen; |
| 917 | |
| 918 | if (ADL && D == 0) { |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 919 | // We've seen something of the form |
| 920 | // |
| 921 | // identifier( |
| 922 | // |
| 923 | // and we did not find any entity by the name |
| 924 | // "identifier". However, this identifier is still subject to |
| 925 | // argument-dependent lookup, so keep track of the name. |
| 926 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 927 | Context.OverloadTy, |
| 928 | Loc)); |
| 929 | } |
| 930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | if (D == 0) { |
| 932 | // Otherwise, this could be an implicitly declared function reference (legal |
| 933 | // in C90, extension in C99). |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 934 | if (HasTrailingLParen && II && |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 935 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 936 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 937 | else { |
| 938 | // If this name wasn't predeclared and if this is not a function call, |
| 939 | // diagnose the problem. |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 940 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 941 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 942 | << Name << SS->getRange()); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 943 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 944 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 945 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 946 | << Name.getAsString()); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 947 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 948 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | } |
| 950 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 951 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 952 | // If this is an expression of the form &Class::member, don't build an |
| 953 | // implicit member ref, because we want a pointer to the member in general, |
| 954 | // not any specific instance's member. |
| 955 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 956 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 957 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 958 | QualType DType; |
| 959 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 960 | DType = FD->getType().getNonReferenceType(); |
| 961 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 962 | DType = Method->getType(); |
| 963 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 964 | DType = Context.OverloadTy; |
| 965 | } |
| 966 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 967 | if (!DType.isNull()) { |
| 968 | // The pointer is type- and value-dependent if it points into something |
| 969 | // dependent. |
Douglas Gregor | 00c4486 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 970 | bool Dependent = DC->isDependentContext(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 971 | return Owned(BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS)); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 976 | // We may have found a field within an anonymous union or struct |
| 977 | // (C++ [class.union]). |
| 978 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 979 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 980 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 982 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 983 | if (!MD->isStatic()) { |
| 984 | // C++ [class.mfct.nonstatic]p2: |
| 985 | // [...] if name lookup (3.4.1) resolves the name in the |
| 986 | // id-expression to a nonstatic nontype member of class X or of |
| 987 | // a base class of X, the id-expression is transformed into a |
| 988 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 989 | // as the postfix-expression to the left of the '.' operator. |
| 990 | DeclContext *Ctx = 0; |
| 991 | QualType MemberType; |
| 992 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 993 | Ctx = FD->getDeclContext(); |
| 994 | MemberType = FD->getType(); |
| 995 | |
| 996 | if (const ReferenceType *RefType = MemberType->getAsReferenceType()) |
| 997 | MemberType = RefType->getPointeeType(); |
| 998 | else if (!FD->isMutable()) { |
| 999 | unsigned combinedQualifiers |
| 1000 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 1001 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 1002 | } |
| 1003 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 1004 | if (!Method->isStatic()) { |
| 1005 | Ctx = Method->getParent(); |
| 1006 | MemberType = Method->getType(); |
| 1007 | } |
| 1008 | } else if (OverloadedFunctionDecl *Ovl |
| 1009 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 1010 | for (OverloadedFunctionDecl::function_iterator |
| 1011 | Func = Ovl->function_begin(), |
| 1012 | FuncEnd = Ovl->function_end(); |
| 1013 | Func != FuncEnd; ++Func) { |
| 1014 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(*Func)) |
| 1015 | if (!DMethod->isStatic()) { |
| 1016 | Ctx = Ovl->getDeclContext(); |
| 1017 | MemberType = Context.OverloadTy; |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1024 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 1025 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 1026 | if ((Context.getCanonicalType(CtxType) |
| 1027 | == Context.getCanonicalType(ThisType)) || |
| 1028 | IsDerivedFrom(ThisType, CtxType)) { |
| 1029 | // Build the implicit member access expression. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1030 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1031 | MD->getThisType(Context)); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1032 | MarkDeclarationReferenced(Loc, D); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1033 | return Owned(new (Context) MemberExpr(This, true, D, |
Eli Friedman | 7252713 | 2009-04-29 17:56:47 +0000 | [diff] [blame] | 1034 | Loc, MemberType)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1040 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1041 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 1042 | if (MD->isStatic()) |
| 1043 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1044 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 1045 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1048 | // Any other ways we could have found the field in a well-formed |
| 1049 | // program would have been turned into implicit member expressions |
| 1050 | // above. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1051 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1052 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1053 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1054 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1056 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1057 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1058 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1059 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1060 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1062 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1063 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1064 | return Owned(BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 1065 | false, false, SS)); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1066 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 1067 | return Owned(BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 1068 | false, false, SS)); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1069 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1071 | // Check whether this declaration can be used. Note that we suppress |
| 1072 | // this check when we're going to perform argument-dependent lookup |
| 1073 | // on this function name, because this might not be the function |
| 1074 | // that overload resolution actually selects. |
| 1075 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 1076 | return ExprError(); |
| 1077 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1078 | if (VarDecl *Var = dyn_cast<VarDecl>(VD)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 1079 | // Warn about constructs like: |
| 1080 | // if (void *X = foo()) { ... } else { X }. |
| 1081 | // In the else block, the pointer is always false. |
Douglas Gregor | 00c4486 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 1082 | |
| 1083 | // FIXME: In a template instantiation, we don't have scope |
| 1084 | // information to check this property. |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1085 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1086 | Scope *CheckS = S; |
| 1087 | while (CheckS) { |
| 1088 | if (CheckS->isWithinElse() && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1089 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1090 | if (Var->getType()->isBooleanType()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1091 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 1092 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1093 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1094 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 1095 | << Var->getDeclName()); |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | // Move up one more control parent to check again. |
| 1100 | CheckS = CheckS->getControlParent(); |
| 1101 | if (CheckS) |
| 1102 | CheckS = CheckS->getParent(); |
| 1103 | } |
| 1104 | } |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1105 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(VD)) { |
| 1106 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1107 | // C99 DR 316 says that, if a function type comes from a |
| 1108 | // function definition (without a prototype), that type is only |
| 1109 | // used for checking compatibility. Therefore, when referencing |
| 1110 | // the function, we pretend that we don't have the full function |
| 1111 | // type. |
| 1112 | QualType T = Func->getType(); |
| 1113 | QualType NoProtoType = T; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1114 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 1115 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
Douglas Gregor | 2224f84 | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 1116 | return Owned(BuildDeclRefExpr(VD, NoProtoType, Loc, false, false, SS)); |
| 1117 | } |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 1118 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Only create DeclRefExpr's for valid Decl's. |
| 1121 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1122 | return ExprError(); |
| 1123 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1124 | // If the identifier reference is inside a block, and it refers to a value |
| 1125 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1126 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1127 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1128 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1129 | // We do not do this for things like enum constants, global variables, etc, |
| 1130 | // as they do not get snapshotted. |
| 1131 | // |
| 1132 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1133 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1134 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1135 | // The BlocksAttr indicates the variable is bound by-reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1136 | if (VD->getAttr<BlocksAttr>(Context)) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1137 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1138 | // This is to record that a 'const' was actually synthesize and added. |
| 1139 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1140 | // Variable will be bound by-copy, make it const within the closure. |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1141 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1142 | ExprTy.addConst(); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1143 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
| 1144 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1145 | } |
| 1146 | // If this reference is not in a block or if the referenced variable is |
| 1147 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1149 | bool TypeDependent = false; |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1150 | bool ValueDependent = false; |
| 1151 | if (getLangOptions().CPlusPlus) { |
| 1152 | // C++ [temp.dep.expr]p3: |
| 1153 | // An id-expression is type-dependent if it contains: |
| 1154 | // - an identifier that was declared with a dependent type, |
| 1155 | if (VD->getType()->isDependentType()) |
| 1156 | TypeDependent = true; |
| 1157 | // - FIXME: a template-id that is dependent, |
| 1158 | // - a conversion-function-id that specifies a dependent type, |
| 1159 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1160 | Name.getCXXNameType()->isDependentType()) |
| 1161 | TypeDependent = true; |
| 1162 | // - a nested-name-specifier that contains a class-name that |
| 1163 | // names a dependent type. |
| 1164 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1165 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1166 | DC; DC = DC->getParent()) { |
| 1167 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1168 | if (DC->isRecord()) { |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1169 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1170 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1171 | TypeDependent = true; |
| 1172 | break; |
| 1173 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1178 | // C++ [temp.dep.constexpr]p2: |
| 1179 | // |
| 1180 | // An identifier is value-dependent if it is: |
| 1181 | // - a name declared with a dependent type, |
| 1182 | if (TypeDependent) |
| 1183 | ValueDependent = true; |
| 1184 | // - the name of a non-type template parameter, |
| 1185 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1186 | ValueDependent = true; |
| 1187 | // - a constant with integral or enumeration type and is |
| 1188 | // initialized with an expression that is value-dependent |
Eli Friedman | c149412 | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1189 | else if (const VarDecl *Dcl = dyn_cast<VarDecl>(VD)) { |
| 1190 | if (Dcl->getType().getCVRQualifiers() == QualType::Const && |
| 1191 | Dcl->getInit()) { |
| 1192 | ValueDependent = Dcl->getInit()->isValueDependent(); |
| 1193 | } |
| 1194 | } |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1195 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1196 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1197 | return Owned(BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1198 | TypeDependent, ValueDependent, SS)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1201 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1202 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1203 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1204 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1206 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1207 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1208 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1209 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1212 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1213 | // string. |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1214 | unsigned Length; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1215 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1216 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1217 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1218 | Length = MD->getSynthesizedMethodSize(); |
| 1219 | else { |
| 1220 | Diag(Loc, diag::ext_predef_outside_function); |
| 1221 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 1222 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 1223 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1224 | |
| 1225 | |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1226 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1227 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1228 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1229 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1232 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1233 | llvm::SmallString<16> CharBuffer; |
| 1234 | CharBuffer.resize(Tok.getLength()); |
| 1235 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1236 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1238 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1239 | Tok.getLocation(), PP); |
| 1240 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1241 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1242 | |
| 1243 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1244 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1245 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1246 | Literal.isWide(), |
| 1247 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1250 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1251 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1253 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1254 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1255 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1256 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1257 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1261 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1262 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1264 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1266 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1268 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1269 | Tok.getLocation(), PP); |
| 1270 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1271 | return ExprError(); |
| 1272 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1273 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1275 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1276 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1277 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1278 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1279 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1280 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1281 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1282 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1283 | |
| 1284 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1285 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1286 | // isExact will be set by GetFloatValue(). |
| 1287 | bool isExact = false; |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1288 | Res = new (Context) FloatingLiteral(Literal.GetFloatValue(Format, &isExact), |
| 1289 | &isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1291 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1292 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1293 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1294 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1295 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1296 | // long long is a C99 feature. |
| 1297 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1298 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1299 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1300 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1302 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1305 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1306 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1307 | Ty = Context.UnsignedLongLongTy; |
| 1308 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1309 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1312 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1313 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1315 | // be an unsigned int. |
| 1316 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1317 | |
| 1318 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1319 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1320 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1321 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1322 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | // Does it fit in a unsigned int? |
| 1325 | if (ResultVal.isIntN(IntSize)) { |
| 1326 | // Does it fit in a signed int? |
| 1327 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1328 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1330 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1331 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1332 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1335 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1336 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1337 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1339 | // Does it fit in a unsigned long? |
| 1340 | if (ResultVal.isIntN(LongSize)) { |
| 1341 | // Does it fit in a signed long? |
| 1342 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1343 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1345 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1346 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1347 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1350 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1351 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1352 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | // Does it fit in a unsigned long long? |
| 1355 | if (ResultVal.isIntN(LongLongSize)) { |
| 1356 | // Does it fit in a signed long long? |
| 1357 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1358 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1359 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1360 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1361 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | } |
| 1363 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | // If we still couldn't decide a type, we probably have something that |
| 1366 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1367 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1368 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1369 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1370 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1373 | if (ResultVal.getBitWidth() != Width) |
| 1374 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1376 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1377 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1379 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1380 | if (Literal.isImaginary) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1381 | Res = new (Context) ImaginaryLiteral(Res, |
| 1382 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1383 | |
| 1384 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1387 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1388 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1389 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1390 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1391 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1395 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1396 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1397 | SourceLocation OpLoc, |
| 1398 | const SourceRange &ExprRange, |
| 1399 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1400 | if (exprType->isDependentType()) |
| 1401 | return false; |
| 1402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1403 | // C99 6.5.3.4p1: |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1404 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1405 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1406 | if (isSizeof) |
| 1407 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1408 | return false; |
| 1409 | } |
| 1410 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1411 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1412 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1413 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1414 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1415 | return false; |
| 1416 | } |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1418 | if (RequireCompleteType(OpLoc, exprType, |
| 1419 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1420 | diag::err_alignof_incomplete_type, |
| 1421 | ExprRange)) |
| 1422 | return true; |
| 1423 | |
| 1424 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1425 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1426 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1427 | << exprType << isSizeof << ExprRange; |
| 1428 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1431 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1434 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1435 | const SourceRange &ExprRange) { |
| 1436 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1437 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1438 | // alignof decl is always ok. |
| 1439 | if (isa<DeclRefExpr>(E)) |
| 1440 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1441 | |
| 1442 | // Cannot know anything else if the expression is dependent. |
| 1443 | if (E->isTypeDependent()) |
| 1444 | return false; |
| 1445 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1446 | if (E->getBitField()) { |
| 1447 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1448 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1449 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1450 | |
| 1451 | // Alignment of a field access is always okay, so long as it isn't a |
| 1452 | // bit-field. |
| 1453 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 1454 | if (dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 1455 | return false; |
| 1456 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1457 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1458 | } |
| 1459 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1460 | /// \brief Build a sizeof or alignof expression given a type operand. |
| 1461 | Action::OwningExprResult |
| 1462 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
| 1463 | bool isSizeOf, SourceRange R) { |
| 1464 | if (T.isNull()) |
| 1465 | return ExprError(); |
| 1466 | |
| 1467 | if (!T->isDependentType() && |
| 1468 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1469 | return ExprError(); |
| 1470 | |
| 1471 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1472 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1473 | Context.getSizeType(), OpLoc, |
| 1474 | R.getEnd())); |
| 1475 | } |
| 1476 | |
| 1477 | /// \brief Build a sizeof or alignof expression given an expression |
| 1478 | /// operand. |
| 1479 | Action::OwningExprResult |
| 1480 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1481 | bool isSizeOf, SourceRange R) { |
| 1482 | // Verify that the operand is valid. |
| 1483 | bool isInvalid = false; |
| 1484 | if (E->isTypeDependent()) { |
| 1485 | // Delay type-checking for type-dependent expressions. |
| 1486 | } else if (!isSizeOf) { |
| 1487 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1488 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1489 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1490 | isInvalid = true; |
| 1491 | } else { |
| 1492 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1493 | } |
| 1494 | |
| 1495 | if (isInvalid) |
| 1496 | return ExprError(); |
| 1497 | |
| 1498 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1499 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1500 | Context.getSizeType(), OpLoc, |
| 1501 | R.getEnd())); |
| 1502 | } |
| 1503 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1504 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1505 | /// the same for @c alignof and @c __alignof |
| 1506 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1507 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1508 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1509 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1511 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1512 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1513 | if (isType) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1514 | QualType ArgTy = QualType::getFromOpaquePtr(TyOrEx); |
| 1515 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
| 1516 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1517 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1518 | // Get the end location. |
| 1519 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1520 | Action::OwningExprResult Result |
| 1521 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1522 | |
| 1523 | if (Result.isInvalid()) |
| 1524 | DeleteExpr(ArgEx); |
| 1525 | |
| 1526 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1529 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1530 | if (V->isTypeDependent()) |
| 1531 | return Context.DependentTy; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1533 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1534 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1535 | return CT->getElementType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1536 | |
| 1537 | // Otherwise they pass through real integer and floating point types here. |
| 1538 | if (V->getType()->isArithmeticType()) |
| 1539 | return V->getType(); |
| 1540 | |
| 1541 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1542 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1543 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1544 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1548 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1549 | Action::OwningExprResult |
| 1550 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1551 | tok::TokenKind Kind, ExprArg Input) { |
| 1552 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1553 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | UnaryOperator::Opcode Opc; |
| 1555 | switch (Kind) { |
| 1556 | default: assert(0 && "Unknown unary op!"); |
| 1557 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1558 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1559 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1561 | if (getLangOptions().CPlusPlus && |
| 1562 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1563 | // Which overloaded operator? |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1564 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1565 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1566 | |
| 1567 | // C++ [over.inc]p1: |
| 1568 | // |
| 1569 | // [...] If the function is a member function with one |
| 1570 | // parameter (which shall be of type int) or a non-member |
| 1571 | // function with two parameters (the second of which shall be |
| 1572 | // of type int), it defines the postfix increment operator ++ |
| 1573 | // for objects of that type. When the postfix increment is |
| 1574 | // called as a result of using the ++ operator, the int |
| 1575 | // argument will have value zero. |
| 1576 | Expr *Args[2] = { |
| 1577 | Arg, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1578 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1579 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1580 | }; |
| 1581 | |
| 1582 | // Build the candidate set for overloading |
| 1583 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1584 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1585 | |
| 1586 | // Perform overload resolution. |
| 1587 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1588 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1589 | case OR_Success: { |
| 1590 | // We found a built-in operator or an overloaded operator. |
| 1591 | FunctionDecl *FnDecl = Best->Function; |
| 1592 | |
| 1593 | if (FnDecl) { |
| 1594 | // We matched an overloaded operator. Build a call to that |
| 1595 | // operator. |
| 1596 | |
| 1597 | // Convert the arguments. |
| 1598 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1599 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1600 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1601 | } else { |
| 1602 | // Convert the arguments. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1603 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1604 | FnDecl->getParamDecl(0)->getType(), |
| 1605 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1606 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1610 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1611 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1612 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1614 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1615 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 488e25b | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1616 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1617 | UsualUnaryConversions(FnExpr); |
| 1618 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1619 | Input.release(); |
Douglas Gregor | 7ff6926 | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1620 | Args[0] = Arg; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1621 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
| 1622 | Args, 2, ResultTy, |
| 1623 | OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1624 | } else { |
| 1625 | // We matched a built-in operator. Convert the arguments, then |
| 1626 | // break out so that we will build the appropriate built-in |
| 1627 | // operator node. |
| 1628 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1629 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1630 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1631 | |
| 1632 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1633 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | case OR_No_Viable_Function: |
| 1637 | // No viable function; fall through to handling this as a |
| 1638 | // built-in operator, which will produce an error message for us. |
| 1639 | break; |
| 1640 | |
| 1641 | case OR_Ambiguous: |
| 1642 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1643 | << UnaryOperator::getOpcodeStr(Opc) |
| 1644 | << Arg->getSourceRange(); |
| 1645 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1646 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1647 | |
| 1648 | case OR_Deleted: |
| 1649 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1650 | << Best->Function->isDeleted() |
| 1651 | << UnaryOperator::getOpcodeStr(Opc) |
| 1652 | << Arg->getSourceRange(); |
| 1653 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1654 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | // Either we found no viable overloaded operator or we matched a |
| 1658 | // built-in operator. In either case, fall through to trying to |
| 1659 | // build a built-in operation. |
| 1660 | } |
| 1661 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 1662 | QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, |
| 1663 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | if (result.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1665 | return ExprError(); |
| 1666 | Input.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1667 | return Owned(new (Context) UnaryOperator(Arg, Opc, result, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1670 | Action::OwningExprResult |
| 1671 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1672 | ExprArg Idx, SourceLocation RLoc) { |
| 1673 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1674 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1676 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1677 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1678 | Base.release(); |
| 1679 | Idx.release(); |
| 1680 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1681 | Context.DependentTy, RLoc)); |
| 1682 | } |
| 1683 | |
| 1684 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1685 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1686 | LHSExp->getType()->isEnumeralType() || |
| 1687 | RHSExp->getType()->isRecordType() || |
| 1688 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1689 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1690 | // to the candidate set. |
| 1691 | OverloadCandidateSet CandidateSet; |
| 1692 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1693 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1694 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1695 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1696 | // Perform overload resolution. |
| 1697 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1698 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1699 | case OR_Success: { |
| 1700 | // We found a built-in operator or an overloaded operator. |
| 1701 | FunctionDecl *FnDecl = Best->Function; |
| 1702 | |
| 1703 | if (FnDecl) { |
| 1704 | // We matched an overloaded operator. Build a call to that |
| 1705 | // operator. |
| 1706 | |
| 1707 | // Convert the arguments. |
| 1708 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1709 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1710 | PerformCopyInitialization(RHSExp, |
| 1711 | FnDecl->getParamDecl(0)->getType(), |
| 1712 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1713 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1714 | } else { |
| 1715 | // Convert the arguments. |
| 1716 | if (PerformCopyInitialization(LHSExp, |
| 1717 | FnDecl->getParamDecl(0)->getType(), |
| 1718 | "passing") || |
| 1719 | PerformCopyInitialization(RHSExp, |
| 1720 | FnDecl->getParamDecl(1)->getType(), |
| 1721 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1722 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1726 | QualType ResultTy |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1727 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1728 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1730 | // Build the actual expression node. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1731 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1732 | SourceLocation()); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1733 | UsualUnaryConversions(FnExpr); |
| 1734 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1735 | Base.release(); |
| 1736 | Idx.release(); |
Douglas Gregor | 7ff6926 | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1737 | Args[0] = LHSExp; |
| 1738 | Args[1] = RHSExp; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1739 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 1740 | FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1741 | ResultTy, LLoc)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1742 | } else { |
| 1743 | // We matched a built-in operator. Convert the arguments, then |
| 1744 | // break out so that we will build the appropriate built-in |
| 1745 | // operator node. |
| 1746 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1747 | "passing") || |
| 1748 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1749 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1750 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1751 | |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | case OR_No_Viable_Function: |
| 1757 | // No viable function; fall through to handling this as a |
| 1758 | // built-in operator, which will produce an error message for us. |
| 1759 | break; |
| 1760 | |
| 1761 | case OR_Ambiguous: |
| 1762 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1763 | << "[]" |
| 1764 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1765 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1766 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1767 | |
| 1768 | case OR_Deleted: |
| 1769 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1770 | << Best->Function->isDeleted() |
| 1771 | << "[]" |
| 1772 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1773 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1774 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | // Either we found no viable overloaded operator or we matched a |
| 1778 | // built-in operator. In either case, fall through to trying to |
| 1779 | // build a built-in operation. |
| 1780 | } |
| 1781 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1782 | // Perform default conversions. |
| 1783 | DefaultFunctionArrayConversion(LHSExp); |
| 1784 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1785 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1786 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1788 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1789 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1790 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1791 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1792 | Expr *BaseExpr, *IndexExpr; |
| 1793 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1794 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1795 | BaseExpr = LHSExp; |
| 1796 | IndexExpr = RHSExp; |
| 1797 | ResultType = Context.DependentTy; |
| 1798 | } else if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1799 | BaseExpr = LHSExp; |
| 1800 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1801 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1802 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1803 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1804 | BaseExpr = RHSExp; |
| 1805 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1806 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1807 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1808 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1809 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1810 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1811 | // FIXME: need to deal with const... |
| 1812 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1813 | } else if (LHSTy->isArrayType()) { |
| 1814 | // If we see an array that wasn't promoted by |
| 1815 | // DefaultFunctionArrayConversion, it must be an array that |
| 1816 | // wasn't promoted because of the C90 rule that doesn't |
| 1817 | // allow promoting non-lvalue arrays. Warn, then |
| 1818 | // force the promotion here. |
| 1819 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1820 | LHSExp->getSourceRange(); |
| 1821 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy)); |
| 1822 | LHSTy = LHSExp->getType(); |
| 1823 | |
| 1824 | BaseExpr = LHSExp; |
| 1825 | IndexExpr = RHSExp; |
| 1826 | ResultType = LHSTy->getAsPointerType()->getPointeeType(); |
| 1827 | } else if (RHSTy->isArrayType()) { |
| 1828 | // Same as previous, except for 123[f().a] case |
| 1829 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1830 | RHSExp->getSourceRange(); |
| 1831 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy)); |
| 1832 | RHSTy = RHSExp->getType(); |
| 1833 | |
| 1834 | BaseExpr = RHSExp; |
| 1835 | IndexExpr = LHSExp; |
| 1836 | ResultType = RHSTy->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1837 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1838 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1839 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1840 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | // C99 6.5.2.1p1 |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1842 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1843 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1844 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1846 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| 1847 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1848 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| 1849 | // incomplete types are not object types. |
| 1850 | if (ResultType->isFunctionType()) { |
| 1851 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1852 | << ResultType << BaseExpr->getSourceRange(); |
| 1853 | return ExprError(); |
| 1854 | } |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1856 | if (!ResultType->isDependentType() && |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1857 | RequireCompleteType(LLoc, ResultType, diag::err_subscript_incomplete_type, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1858 | BaseExpr->getSourceRange())) |
| 1859 | return ExprError(); |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1860 | |
| 1861 | // Diagnose bad cases where we step over interface counts. |
| 1862 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1863 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1864 | << ResultType << BaseExpr->getSourceRange(); |
| 1865 | return ExprError(); |
| 1866 | } |
| 1867 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1868 | Base.release(); |
| 1869 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1870 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1871 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1874 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1875 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1876 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1877 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1879 | // The vector accessor can't exceed the number of elements. |
| 1880 | const char *compStr = CompName.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1881 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1882 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1883 | // special names that indicate a subset of exactly half the elements are |
| 1884 | // to be selected. |
| 1885 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1886 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1887 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1888 | // indicating that it is a string of hex values to be used as vector indices. |
| 1889 | bool HexSwizzle = *compStr == 's'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1890 | |
| 1891 | // Check that we've found one of the special components, or that the component |
| 1892 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1893 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1894 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1895 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1896 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1897 | do |
| 1898 | compStr++; |
| 1899 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1900 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1901 | do |
| 1902 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1903 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1904 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1905 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1906 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1907 | // We didn't get to the end of the string. This means the component names |
| 1908 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1909 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1910 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1911 | return QualType(); |
| 1912 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1913 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1914 | // Ensure no component accessor exceeds the width of the vector type it |
| 1915 | // operates on. |
| 1916 | if (!HalvingSwizzle) { |
| 1917 | compStr = CompName.getName(); |
| 1918 | |
| 1919 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1920 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1921 | |
| 1922 | while (*compStr) { |
| 1923 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1924 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1925 | << baseType << SourceRange(CompLoc); |
| 1926 | return QualType(); |
| 1927 | } |
| 1928 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1929 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1930 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1931 | // If this is a halving swizzle, verify that the base type has an even |
| 1932 | // number of elements. |
| 1933 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1934 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1935 | << baseType << SourceRange(CompLoc); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1936 | return QualType(); |
| 1937 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1938 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1939 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1940 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1941 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1942 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1943 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1944 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1945 | : CompName.getLength(); |
| 1946 | if (HexSwizzle) |
| 1947 | CompSize--; |
| 1948 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1949 | if (CompSize == 1) |
| 1950 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1951 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1952 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1953 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1954 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1955 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1956 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1957 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1958 | } |
| 1959 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1962 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 1963 | IdentifierInfo &Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1964 | const Selector &Sel, |
| 1965 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1967 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Context, &Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1968 | return PD; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1969 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Context, Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1970 | return OMD; |
| 1971 | |
| 1972 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1973 | E = PDecl->protocol_end(); I != E; ++I) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1974 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
| 1975 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1976 | return D; |
| 1977 | } |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1981 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1982 | IdentifierInfo &Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1983 | const Selector &Sel, |
| 1984 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1985 | // Check protocols on qualified interfaces. |
| 1986 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1987 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1988 | E = QIdTy->qual_end(); I != E; ++I) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1989 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, &Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1990 | GDecl = PD; |
| 1991 | break; |
| 1992 | } |
| 1993 | // Also must look for a getter name which uses property syntax. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1994 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Context, Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1995 | GDecl = OMD; |
| 1996 | break; |
| 1997 | } |
| 1998 | } |
| 1999 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2000 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2001 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2002 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2003 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2004 | if (GDecl) |
| 2005 | return GDecl; |
| 2006 | } |
| 2007 | } |
| 2008 | return GDecl; |
| 2009 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2010 | |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2011 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 2012 | /// all base class implementations. |
| 2013 | /// |
| 2014 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 2015 | const ObjCInterfaceDecl *IFace, |
| 2016 | const Selector &Sel) { |
| 2017 | ObjCMethodDecl *Method = 0; |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 2018 | if (ObjCImplementationDecl *ImpDecl |
| 2019 | = LookupObjCImplementation(IFace->getIdentifier())) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2020 | Method = ImpDecl->getInstanceMethod(Context, Sel); |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2021 | |
| 2022 | if (!Method && IFace->getSuperClass()) |
| 2023 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 2024 | return Method; |
| 2025 | } |
| 2026 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2027 | Action::OwningExprResult |
| 2028 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2029 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2030 | IdentifierInfo &Member, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2031 | DeclPtrTy ObjCImpDecl) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2032 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2033 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2034 | |
| 2035 | // Perform default conversions. |
| 2036 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2037 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2038 | QualType BaseType = BaseExpr->getType(); |
| 2039 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2040 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2041 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2042 | // must have pointer type, and the accessed type is the pointee. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2043 | if (OpKind == tok::arrow) { |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2044 | if (BaseType->isDependentType()) |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2045 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2046 | BaseExpr, true, |
| 2047 | OpLoc, |
| 2048 | DeclarationName(&Member), |
| 2049 | MemberLoc)); |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2050 | else if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2051 | BaseType = PT->getPointeeType(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 2052 | else if (getLangOptions().CPlusPlus && BaseType->isRecordType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2053 | return Owned(BuildOverloadedArrowExpr(S, BaseExpr, OpLoc, |
| 2054 | MemberLoc, Member)); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2055 | else |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2056 | return ExprError(Diag(MemberLoc, |
| 2057 | diag::err_typecheck_member_reference_arrow) |
| 2058 | << BaseType << BaseExpr->getSourceRange()); |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2059 | } else { |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2060 | if (BaseType->isDependentType()) { |
| 2061 | // Require that the base type isn't a pointer type |
| 2062 | // (so we'll report an error for) |
| 2063 | // T* t; |
| 2064 | // t.f; |
| 2065 | // |
| 2066 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2067 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2068 | // allows this, while still reporting an error if T is a struct pointer. |
| 2069 | const PointerType *PT = BaseType->getAsPointerType(); |
| 2070 | |
| 2071 | if (!PT || (getLangOptions().ObjC1 && |
| 2072 | !PT->getPointeeType()->isRecordType())) |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2073 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2074 | BaseExpr, false, |
| 2075 | OpLoc, |
| 2076 | DeclarationName(&Member), |
| 2077 | MemberLoc)); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2078 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2079 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2080 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2081 | // Handle field access to simple records. This also handles access to fields |
| 2082 | // of the ObjC 'id' struct. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2083 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2084 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2085 | if (RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2086 | diag::err_typecheck_incomplete_tag, |
| 2087 | BaseExpr->getSourceRange())) |
| 2088 | return ExprError(); |
| 2089 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2090 | // The record definition is complete, now make sure the member is valid. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2091 | // FIXME: Qualified name lookup for C++ is a bit more complicated than this. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2092 | LookupResult Result |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2093 | = LookupQualifiedName(RDecl, DeclarationName(&Member), |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 2094 | LookupMemberName, false); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2095 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2096 | if (!Result) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2097 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 2098 | << &Member << BaseExpr->getSourceRange()); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2099 | if (Result.isAmbiguous()) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2100 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 2101 | MemberLoc, BaseExpr->getSourceRange()); |
| 2102 | return ExprError(); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2106 | |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2107 | // If the decl being referenced had an error, return an error for this |
| 2108 | // sub-expr without emitting another error, in order to avoid cascading |
| 2109 | // error cases. |
| 2110 | if (MemberDecl->isInvalidDecl()) |
| 2111 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2113 | // Check the use of this field |
| 2114 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 2115 | return ExprError(); |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2117 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2118 | // We may have found a field within an anonymous union or struct |
| 2119 | // (C++ [class.union]). |
| 2120 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2121 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2122 | BaseExpr, OpLoc); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2124 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2125 | // FIXME: Handle address space modifiers |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2126 | QualType MemberType = FD->getType(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2127 | if (const ReferenceType *Ref = MemberType->getAsReferenceType()) |
| 2128 | MemberType = Ref->getPointeeType(); |
| 2129 | else { |
| 2130 | unsigned combinedQualifiers = |
| 2131 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2132 | if (FD->isMutable()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2133 | combinedQualifiers &= ~QualType::Const; |
| 2134 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 2135 | } |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2136 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2137 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 2138 | MemberLoc, MemberType)); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2142 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2143 | Var, MemberLoc, |
| 2144 | Var->getType().getNonReferenceType())); |
| 2145 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2146 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2147 | MemberFn, MemberLoc, |
| 2148 | MemberFn->getType())); |
| 2149 | if (OverloadedFunctionDecl *Ovl |
| 2150 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2151 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2152 | MemberLoc, Context.OverloadTy)); |
| 2153 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2154 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 2155 | Enum, MemberLoc, Enum->getType())); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2156 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2157 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2158 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2160 | // We found a declaration kind that we didn't expect. This is a |
| 2161 | // generic error message that tells the user that she can't refer |
| 2162 | // to this member with '.' or '->'. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2163 | return ExprError(Diag(MemberLoc, |
| 2164 | diag::err_typecheck_member_reference_unknown) |
| 2165 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2166 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2168 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2169 | // (*Obj).ivar. |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2170 | if (const ObjCInterfaceType *IFTy = BaseType->getAsObjCInterfaceType()) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2171 | ObjCInterfaceDecl *ClassDeclared; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2172 | if (ObjCIvarDecl *IV = IFTy->getDecl()->lookupInstanceVariable(Context, |
| 2173 | &Member, |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2174 | ClassDeclared)) { |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2175 | // If the decl being referenced had an error, return an error for this |
| 2176 | // sub-expr without emitting another error, in order to avoid cascading |
| 2177 | // error cases. |
| 2178 | if (IV->isInvalidDecl()) |
| 2179 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Check whether we can reference this field. |
| 2182 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2183 | return ExprError(); |
Steve Naroff | 8bfd1b8 | 2009-03-26 16:01:08 +0000 | [diff] [blame] | 2184 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2185 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2186 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2187 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2188 | ClassOfMethodDecl = MD->getClassInterface(); |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2189 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2190 | // Case of a c-function declared inside an objc implementation. |
| 2191 | // FIXME: For a c-style function nested inside an objc implementation |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2192 | // class, there is no implementation context available, so we pass |
| 2193 | // down the context as argument to this routine. Ideally, this context |
| 2194 | // need be passed down in the AST node and somehow calculated from the |
| 2195 | // AST for a function decl. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2196 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2197 | if (ObjCImplementationDecl *IMPD = |
| 2198 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2199 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2200 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2201 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2202 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2203 | } |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2204 | |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2205 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2206 | if (ClassDeclared != IFTy->getDecl() || |
Fariborz Jahanian | a6e3ac5 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 2207 | ClassOfMethodDecl != ClassDeclared) |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2208 | Diag(MemberLoc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2209 | } |
| 2210 | // @protected |
| 2211 | else if (!IFTy->getDecl()->isSuperClassOf(ClassOfMethodDecl)) |
| 2212 | Diag(MemberLoc, diag::error_protected_ivar_access) << IV->getDeclName(); |
| 2213 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2214 | |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2215 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2216 | MemberLoc, BaseExpr, |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2217 | OpKind == tok::arrow)); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2218 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2219 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 2220 | << IFTy->getDecl()->getDeclName() << &Member |
| 2221 | << BaseExpr->getSourceRange()); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2222 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2224 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2225 | // pointer to a (potentially qualified) interface type. |
| 2226 | const PointerType *PTy; |
| 2227 | const ObjCInterfaceType *IFTy; |
| 2228 | if (OpKind == tok::period && (PTy = BaseType->getAsPointerType()) && |
| 2229 | (IFTy = PTy->getPointeeType()->getAsObjCInterfaceType())) { |
| 2230 | ObjCInterfaceDecl *IFace = IFTy->getDecl(); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2231 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2232 | // Search for a declared property first. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2233 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Context, |
| 2234 | &Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2235 | // Check whether we can reference this property. |
| 2236 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2237 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2238 | QualType ResTy = PD->getType(); |
| 2239 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2240 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2241 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2242 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2243 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2244 | MemberLoc, BaseExpr)); |
| 2245 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2246 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2247 | // Check protocols on qualified interfaces. |
Chris Lattner | 9baefc2 | 2008-07-21 05:20:01 +0000 | [diff] [blame] | 2248 | for (ObjCInterfaceType::qual_iterator I = IFTy->qual_begin(), |
| 2249 | E = IFTy->qual_end(); I != E; ++I) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2250 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Context, |
| 2251 | &Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2252 | // Check whether we can reference this property. |
| 2253 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2254 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2255 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2256 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2257 | MemberLoc, BaseExpr)); |
| 2258 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2259 | |
| 2260 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2261 | // selector is implemented. |
| 2262 | |
| 2263 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2264 | // shared with the code in ActOnInstanceMessage. |
| 2265 | |
| 2266 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2267 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Context, Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2268 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2269 | // If this reference is in an @implementation, check for 'private' methods. |
| 2270 | if (!Getter) |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2271 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2272 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2273 | // Look through local category implementations associated with the class. |
| 2274 | if (!Getter) { |
| 2275 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Getter; i++) { |
| 2276 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2277 | Getter = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel); |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2280 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2281 | // Check if we can reference this property. |
| 2282 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2283 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2284 | } |
| 2285 | // If we found a getter then this may be a valid dot-reference, we |
| 2286 | // will look for the matching setter, in case it is needed. |
| 2287 | Selector SetterSel = |
| 2288 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2289 | PP.getSelectorTable(), &Member); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2290 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(Context, SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2291 | if (!Setter) { |
| 2292 | // If this reference is in an @implementation, also check for 'private' |
| 2293 | // methods. |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2294 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2295 | } |
| 2296 | // Look through local category implementations associated with the class. |
| 2297 | if (!Setter) { |
| 2298 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2299 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2300 | Setter = ObjCCategoryImpls[i]->getInstanceMethod(Context, SetterSel); |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 2301 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2302 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2303 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2304 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2305 | return ExprError(); |
| 2306 | |
| 2307 | if (Getter || Setter) { |
| 2308 | QualType PType; |
| 2309 | |
| 2310 | if (Getter) |
| 2311 | PType = Getter->getResultType(); |
| 2312 | else { |
| 2313 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2314 | E = Setter->param_end(); PI != E; ++PI) |
| 2315 | PType = (*PI)->getType(); |
| 2316 | } |
| 2317 | // FIXME: we must check that the setter has property type. |
| 2318 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2319 | Setter, MemberLoc, BaseExpr)); |
| 2320 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2321 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2322 | << &Member << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2323 | } |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 2324 | // Handle properties on qualified "id" protocols. |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2325 | const ObjCObjectPointerType *QIdTy; |
Steve Naroff | 18bc164 | 2008-10-20 22:53:06 +0000 | [diff] [blame] | 2326 | if (OpKind == tok::period && (QIdTy = BaseType->getAsObjCQualifiedIdType())) { |
| 2327 | // Check protocols on qualified interfaces. |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2328 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2329 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2330 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2331 | // Check the use of this declaration |
| 2332 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2333 | return ExprError(); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2334 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2335 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2336 | MemberLoc, BaseExpr)); |
| 2337 | } |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2338 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2339 | // Check the use of this method. |
| 2340 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2341 | return ExprError(); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2342 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2343 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2344 | OMD->getResultType(), |
| 2345 | OMD, OpLoc, MemberLoc, |
| 2346 | NULL, 0)); |
Fariborz Jahanian | 391d895 | 2008-12-10 00:21:50 +0000 | [diff] [blame] | 2347 | } |
| 2348 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2349 | |
| 2350 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2351 | << &Member << BaseType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2352 | } |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2353 | // Handle properties on ObjC 'Class' types. |
| 2354 | if (OpKind == tok::period && (BaseType == Context.getObjCClassType())) { |
| 2355 | // Also must look for a getter name which uses property syntax. |
| 2356 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2357 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2358 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2359 | ObjCMethodDecl *Getter; |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2360 | // FIXME: need to also look locally in the implementation. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2361 | if ((Getter = IFace->lookupClassMethod(Context, Sel))) { |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2362 | // Check the use of this method. |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2363 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2364 | return ExprError(); |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2365 | } |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2366 | // If we found a getter then this may be a valid dot-reference, we |
| 2367 | // will look for the matching setter, in case it is needed. |
| 2368 | Selector SetterSel = |
| 2369 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2370 | PP.getSelectorTable(), &Member); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2371 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel); |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2372 | if (!Setter) { |
| 2373 | // If this reference is in an @implementation, also check for 'private' |
| 2374 | // methods. |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2375 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2376 | } |
| 2377 | // Look through local category implementations associated with the class. |
| 2378 | if (!Setter) { |
| 2379 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 2380 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2381 | Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel); |
Steve Naroff | 335c680 | 2009-03-11 20:12:18 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2386 | return ExprError(); |
| 2387 | |
| 2388 | if (Getter || Setter) { |
| 2389 | QualType PType; |
| 2390 | |
| 2391 | if (Getter) |
| 2392 | PType = Getter->getResultType(); |
| 2393 | else { |
| 2394 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 2395 | E = Setter->param_end(); PI != E; ++PI) |
| 2396 | PType = (*PI)->getType(); |
| 2397 | } |
| 2398 | // FIXME: we must check that the setter has property type. |
| 2399 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, |
| 2400 | Setter, MemberLoc, BaseExpr)); |
| 2401 | } |
| 2402 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2403 | << &Member << BaseType); |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2407 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2408 | if (BaseType->isExtVectorType()) { |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2409 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2410 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2411 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2412 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2413 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2414 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2415 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2416 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2417 | << BaseType << BaseExpr->getSourceRange(); |
| 2418 | |
| 2419 | // If the user is trying to apply -> or . to a function or function |
| 2420 | // pointer, it's probably because they forgot parentheses to call |
| 2421 | // the function. Suggest the addition of those parentheses. |
| 2422 | if (BaseType == Context.OverloadTy || |
| 2423 | BaseType->isFunctionType() || |
| 2424 | (BaseType->isPointerType() && |
| 2425 | BaseType->getAsPointerType()->isFunctionType())) { |
| 2426 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2427 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2428 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2429 | } |
| 2430 | |
| 2431 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2434 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2435 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2436 | /// function prototype Proto. Call is the call expression itself, and |
| 2437 | /// Fn is the function expression. For a C++ member function, this |
| 2438 | /// routine does not attempt to convert the object argument. Returns |
| 2439 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2440 | bool |
| 2441 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2442 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2443 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2444 | Expr **Args, unsigned NumArgs, |
| 2445 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2446 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2447 | // assignment, to the types of the corresponding parameter, ... |
| 2448 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2449 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2450 | bool Invalid = false; |
| 2451 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2452 | // If too few arguments are available (and we don't have default |
| 2453 | // arguments for the remaining parameters), don't make the call. |
| 2454 | if (NumArgs < NumArgsInProto) { |
| 2455 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2456 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2457 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2458 | // Use default arguments for missing arguments |
| 2459 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2460 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | // If too many are passed and not variadic, error on the extras and drop |
| 2464 | // them. |
| 2465 | if (NumArgs > NumArgsInProto) { |
| 2466 | if (!Proto->isVariadic()) { |
| 2467 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2468 | diag::err_typecheck_call_too_many_args) |
| 2469 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2470 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2471 | Args[NumArgs-1]->getLocEnd()); |
| 2472 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2473 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2474 | Invalid = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2475 | } |
| 2476 | NumArgsToCheck = NumArgsInProto; |
| 2477 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2479 | // Continue to check argument types (even if we have too few/many args). |
| 2480 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2481 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2483 | Expr *Arg; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2484 | if (i < NumArgs) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2485 | Arg = Args[i]; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2486 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2487 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2488 | ProtoArgType, |
| 2489 | diag::err_call_incomplete_argument, |
| 2490 | Arg->getSourceRange())) |
| 2491 | return true; |
| 2492 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2493 | // Pass the argument. |
| 2494 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2495 | return true; |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2496 | } else { |
| 2497 | if (FDecl->getParamDecl(i)->hasUnparsedDefaultArg()) { |
| 2498 | Diag (Call->getSourceRange().getBegin(), |
| 2499 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2500 | FDecl << cast<CXXRecordDecl>(FDecl->getDeclContext())->getDeclName(); |
| 2501 | Diag(UnparsedDefaultArgLocs[FDecl->getParamDecl(i)], |
| 2502 | diag::note_default_argument_declared_here); |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 2503 | } else { |
| 2504 | Expr *DefaultExpr = FDecl->getParamDecl(i)->getDefaultArg(); |
| 2505 | |
| 2506 | // If the default expression creates temporaries, we need to |
| 2507 | // push them to the current stack of expression temporaries so they'll |
| 2508 | // be properly destroyed. |
| 2509 | if (CXXExprWithTemporaries *E |
| 2510 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
| 2511 | assert(!E->shouldDestroyTemporaries() && |
| 2512 | "Can't destroy temporaries in a default argument expr!"); |
| 2513 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2514 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2515 | } |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2516 | } |
Anders Carlsson | f54741e | 2009-06-16 03:37:31 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2518 | // We already type-checked the argument, so we know it works. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2519 | Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i)); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2522 | QualType ArgType = Arg->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2524 | Call->setArg(i, Arg); |
| 2525 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2527 | // If this is a variadic call, handle args passed through "...". |
| 2528 | if (Proto->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2529 | VariadicCallType CallType = VariadicFunction; |
| 2530 | if (Fn->getType()->isBlockPointerType()) |
| 2531 | CallType = VariadicBlock; // Block |
| 2532 | else if (isa<MemberExpr>(Fn)) |
| 2533 | CallType = VariadicMethod; |
| 2534 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2535 | // Promote the arguments (C99 6.5.2.2p7). |
| 2536 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2537 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2538 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2539 | Call->setArg(i, Arg); |
| 2540 | } |
| 2541 | } |
| 2542 | |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2543 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2546 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2547 | /// This provides the location of the left/right parens and a list of comma |
| 2548 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2549 | Action::OwningExprResult |
| 2550 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2551 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2552 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2553 | unsigned NumArgs = args.size(); |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2554 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2555 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 2556 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2557 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2558 | NamedDecl *NDecl = NULL; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2559 | DeclarationName UnqualifiedName; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2561 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2562 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2563 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2564 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2565 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2566 | bool Dependent = false; |
| 2567 | if (Fn->isTypeDependent()) |
| 2568 | Dependent = true; |
| 2569 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2570 | Dependent = true; |
| 2571 | |
| 2572 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2573 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2574 | Context.DependentTy, RParenLoc)); |
| 2575 | |
| 2576 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2577 | if (Fn->getType()->isRecordType()) |
| 2578 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2579 | CommaLocs, RParenLoc)); |
| 2580 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2581 | // Determine whether this is a call to a member function. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2582 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) |
| 2583 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 2584 | isa<CXXMethodDecl>(MemExpr->getMemberDecl())) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2585 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2586 | CommaLocs, RParenLoc)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2589 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 2590 | DeclRefExpr *DRExpr = NULL; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2591 | Expr *FnExpr = Fn; |
| 2592 | bool ADL = true; |
| 2593 | while (true) { |
| 2594 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2595 | FnExpr = IcExpr->getSubExpr(); |
| 2596 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2597 | // Parentheses around a function disable ADL |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2598 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2599 | ADL = false; |
| 2600 | FnExpr = PExpr->getSubExpr(); |
| 2601 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2602 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2603 | == UnaryOperator::AddrOf) { |
| 2604 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2605 | } else if ((DRExpr = dyn_cast<DeclRefExpr>(FnExpr))) { |
| 2606 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2607 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
| 2608 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2609 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2610 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2611 | UnqualifiedName = DepName->getName(); |
| 2612 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2613 | } else { |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2614 | // Any kind of name that does not refer to a declaration (or |
| 2615 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2616 | ADL = false; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2619 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2621 | OverloadedFunctionDecl *Ovl = 0; |
| 2622 | if (DRExpr) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2623 | FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2624 | Ovl = dyn_cast<OverloadedFunctionDecl>(DRExpr->getDecl()); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2625 | NDecl = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2626 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2628 | if (Ovl || (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2629 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2630 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2631 | ADL = false; |
| 2632 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2633 | // We don't perform ADL in C. |
| 2634 | if (!getLangOptions().CPlusPlus) |
| 2635 | ADL = false; |
| 2636 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2637 | if (Ovl || ADL) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2638 | FDecl = ResolveOverloadedCallFn(Fn, DRExpr? DRExpr->getDecl() : 0, |
| 2639 | UnqualifiedName, LParenLoc, Args, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2640 | NumArgs, CommaLocs, RParenLoc, ADL); |
| 2641 | if (!FDecl) |
| 2642 | return ExprError(); |
| 2643 | |
| 2644 | // Update Fn to refer to the actual function selected. |
| 2645 | Expr *NewFn = 0; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2646 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2647 | = dyn_cast_or_null<QualifiedDeclRefExpr>(DRExpr)) |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2648 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2649 | QDRExpr->getLocation(), |
| 2650 | false, false, |
| 2651 | QDRExpr->getQualifierRange(), |
| 2652 | QDRExpr->getQualifier()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2653 | else |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2654 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2655 | Fn->getSourceRange().getBegin()); |
| 2656 | Fn->Destroy(Context); |
| 2657 | Fn = NewFn; |
| 2658 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2659 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Promote the function operand. |
| 2662 | UsualUnaryConversions(Fn); |
| 2663 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2664 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2665 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2666 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2667 | Args, NumArgs, |
| 2668 | Context.BoolTy, |
| 2669 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2670 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2671 | const FunctionType *FuncT; |
| 2672 | if (!Fn->getType()->isBlockPointerType()) { |
| 2673 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2674 | // have type pointer to function". |
| 2675 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
| 2676 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2677 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2678 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2679 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2680 | } else { // This is a block call. |
| 2681 | FuncT = Fn->getType()->getAsBlockPointerType()->getPointeeType()-> |
| 2682 | getAsFunctionType(); |
| 2683 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2684 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2685 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2686 | << Fn->getType() << Fn->getSourceRange()); |
| 2687 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2688 | // Check for a valid return type |
| 2689 | if (!FuncT->getResultType()->isVoidType() && |
| 2690 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2691 | FuncT->getResultType(), |
| 2692 | diag::err_call_incomplete_return, |
| 2693 | TheCall->getSourceRange())) |
| 2694 | return ExprError(); |
| 2695 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2696 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2697 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2699 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2700 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2701 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2702 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2703 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2704 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2706 | if (FDecl) { |
| 2707 | // Check if we have too few/too many template arguments, based |
| 2708 | // on our knowledge of the function definition. |
| 2709 | const FunctionDecl *Def = 0; |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2710 | if (FDecl->getBody(Context, Def) && NumArgs != Def->param_size()) { |
| 2711 | const FunctionProtoType *Proto = |
| 2712 | Def->getType()->getAsFunctionProtoType(); |
| 2713 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2714 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2715 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2716 | } |
| 2717 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2720 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2721 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2722 | Expr *Arg = Args[i]; |
| 2723 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2724 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2725 | Arg->getType(), |
| 2726 | diag::err_call_incomplete_argument, |
| 2727 | Arg->getSourceRange())) |
| 2728 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2729 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2730 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2733 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2734 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2735 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2736 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2737 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2738 | // Check for sentinels |
| 2739 | if (NDecl) |
| 2740 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2741 | // Do special checking on direct calls to functions. |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2742 | if (FDecl) |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2743 | return CheckFunctionCall(FDecl, TheCall.take()); |
Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 2744 | if (NDecl) |
| 2745 | return CheckBlockCall(NDecl, TheCall.take()); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2746 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2747 | return Owned(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2750 | Action::OwningExprResult |
| 2751 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2752 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2753 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2754 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 2755 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2756 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2757 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2758 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2759 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2760 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2761 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2762 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2763 | } else if (!literalType->isDependentType() && |
| 2764 | RequireCompleteType(LParenLoc, literalType, |
| 2765 | diag::err_typecheck_decl_incomplete_type, |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2766 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2767 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2768 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2769 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2770 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2771 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2772 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2773 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2774 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2775 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2776 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2777 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2778 | InitExpr.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2779 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2780 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2783 | Action::OwningExprResult |
| 2784 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2785 | SourceLocation RBraceLoc) { |
| 2786 | unsigned NumInit = initlist.size(); |
| 2787 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2788 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2789 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2790 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2791 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2792 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2793 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2794 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2795 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2798 | /// CheckCastTypes - Check type constraints for casting between types. |
Daniel Dunbar | 58d5ebb | 2008-08-20 03:55:42 +0000 | [diff] [blame] | 2799 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2800 | UsualUnaryConversions(castExpr); |
| 2801 | |
| 2802 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2803 | // type needs to be scalar. |
| 2804 | if (castType->isVoidType()) { |
| 2805 | // Cast to void allows any expr type. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2806 | } else if (castType->isDependentType() || castExpr->isTypeDependent()) { |
| 2807 | // We can't check any more until template instantiation time. |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2808 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2809 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2810 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2811 | (castType->isStructureType() || castType->isUnionType())) { |
| 2812 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2813 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2814 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2815 | << castType << castExpr->getSourceRange(); |
| 2816 | } else if (castType->isUnionType()) { |
| 2817 | // GCC cast to union extension |
| 2818 | RecordDecl *RD = castType->getAsRecordType()->getDecl(); |
| 2819 | RecordDecl::field_iterator Field, FieldEnd; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2820 | for (Field = RD->field_begin(Context), FieldEnd = RD->field_end(Context); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2821 | Field != FieldEnd; ++Field) { |
| 2822 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2823 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2824 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2825 | << castExpr->getSourceRange(); |
| 2826 | break; |
| 2827 | } |
| 2828 | } |
| 2829 | if (Field == FieldEnd) |
| 2830 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2831 | << castExpr->getType() << castExpr->getSourceRange(); |
| 2832 | } else { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2833 | // Reject any other conversions to non-scalar types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2834 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2835 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2836 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2837 | } else if (!castExpr->getType()->isScalarType() && |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2838 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2839 | return Diag(castExpr->getLocStart(), |
| 2840 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2841 | << castExpr->getType() << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2842 | } else if (castExpr->getType()->isVectorType()) { |
| 2843 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2844 | return true; |
| 2845 | } else if (castType->isVectorType()) { |
| 2846 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2847 | return true; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2848 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 2849 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 2850 | } else if (!castType->isArithmeticType()) { |
| 2851 | QualType castExprType = castExpr->getType(); |
| 2852 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 2853 | return Diag(castExpr->getLocStart(), |
| 2854 | diag::err_cast_pointer_from_non_pointer_int) |
| 2855 | << castExprType << castExpr->getSourceRange(); |
| 2856 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 2857 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 2858 | return Diag(castExpr->getLocStart(), |
| 2859 | diag::err_cast_pointer_to_non_pointer_int) |
| 2860 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2861 | } |
Fariborz Jahanian | b5ff6bf | 2009-05-22 21:42:52 +0000 | [diff] [blame] | 2862 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 2863 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2864 | return false; |
| 2865 | } |
| 2866 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2867 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2868 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2869 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2870 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2871 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2872 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2873 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2874 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2875 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2876 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2877 | } else |
| 2878 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2879 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2880 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2881 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2882 | return false; |
| 2883 | } |
| 2884 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2885 | Action::OwningExprResult |
| 2886 | Sema::ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 2887 | SourceLocation RParenLoc, ExprArg Op) { |
| 2888 | assert((Ty != 0) && (Op.get() != 0) && |
| 2889 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2890 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2891 | Expr *castExpr = Op.takeAs<Expr>(); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 2892 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 2893 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2894 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2895 | return ExprError(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2896 | return Owned(new (Context) CStyleCastExpr(castType, castExpr, castType, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2897 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2900 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 2901 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 2902 | /// C99 6.5.15 |
| 2903 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 2904 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2905 | // C++ is sufficiently different to merit its own checker. |
| 2906 | if (getLangOptions().CPlusPlus) |
| 2907 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 2908 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2909 | UsualUnaryConversions(Cond); |
| 2910 | UsualUnaryConversions(LHS); |
| 2911 | UsualUnaryConversions(RHS); |
| 2912 | QualType CondTy = Cond->getType(); |
| 2913 | QualType LHSTy = LHS->getType(); |
| 2914 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 2915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2916 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2917 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 2918 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 2919 | << CondTy; |
| 2920 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2921 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2922 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2923 | // Now check the two expressions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2925 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 2926 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2927 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 2928 | UsualArithmeticConversions(LHS, RHS); |
| 2929 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 2930 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2932 | // If both operands are the same structure or union type, the result is that |
| 2933 | // type. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2934 | if (const RecordType *LHSRT = LHSTy->getAsRecordType()) { // C99 6.5.15p3 |
| 2935 | if (const RecordType *RHSRT = RHSTy->getAsRecordType()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 2936 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2937 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2938 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2939 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2940 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2941 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2942 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 2943 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2944 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2945 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 2946 | if (!LHSTy->isVoidType()) |
| 2947 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2948 | << RHS->getSourceRange(); |
| 2949 | if (!RHSTy->isVoidType()) |
| 2950 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 2951 | << LHS->getSourceRange(); |
| 2952 | ImpCastExprToType(LHS, Context.VoidTy); |
| 2953 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 2954 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 2955 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2956 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 2957 | // the type of the other operand." |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2958 | if ((LHSTy->isPointerType() || LHSTy->isBlockPointerType() || |
| 2959 | Context.isObjCObjectPointerType(LHSTy)) && |
| 2960 | RHS->isNullPointerConstant(Context)) { |
| 2961 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 2962 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2963 | } |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 2964 | if ((RHSTy->isPointerType() || RHSTy->isBlockPointerType() || |
| 2965 | Context.isObjCObjectPointerType(RHSTy)) && |
| 2966 | LHS->isNullPointerConstant(Context)) { |
| 2967 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 2968 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 2969 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2970 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2971 | const PointerType *LHSPT = LHSTy->getAsPointerType(); |
| 2972 | const PointerType *RHSPT = RHSTy->getAsPointerType(); |
| 2973 | const BlockPointerType *LHSBPT = LHSTy->getAsBlockPointerType(); |
| 2974 | const BlockPointerType *RHSBPT = RHSTy->getAsBlockPointerType(); |
| 2975 | |
Chris Lattner | bd57d36 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 2976 | // Handle the case where both operands are pointers before we handle null |
| 2977 | // pointer constants in case both operands are null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2978 | if ((LHSPT || LHSBPT) && (RHSPT || RHSBPT)) { // C99 6.5.15p3,6 |
| 2979 | // get the "pointed to" types |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 2980 | QualType lhptee = (LHSPT ? LHSPT->getPointeeType() |
| 2981 | : LHSBPT->getPointeeType()); |
| 2982 | QualType rhptee = (RHSPT ? RHSPT->getPointeeType() |
| 2983 | : RHSBPT->getPointeeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2984 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 2985 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 2986 | if (lhptee->isVoidType() |
| 2987 | && (RHSBPT || rhptee->isIncompleteOrObjectType())) { |
| 2988 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 2989 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 2990 | QualType destType = Context.getPointerType(destPointee); |
| 2991 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 2992 | ImpCastExprToType(RHS, destType); // promote to void* |
| 2993 | return destType; |
| 2994 | } |
| 2995 | if (rhptee->isVoidType() |
| 2996 | && (LHSBPT || lhptee->isIncompleteOrObjectType())) { |
| 2997 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 2998 | QualType destType = Context.getPointerType(destPointee); |
| 2999 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3000 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3001 | return destType; |
| 3002 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3003 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3004 | bool sameKind = (LHSPT && RHSPT) || (LHSBPT && RHSBPT); |
| 3005 | if (sameKind |
| 3006 | && Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3007 | // Two identical pointer types are always compatible. |
| 3008 | return LHSTy; |
| 3009 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3010 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3011 | QualType compositeType = LHSTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3012 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3013 | // If either type is an Objective-C object type then check |
| 3014 | // compatibility according to Objective-C. |
| 3015 | if (Context.isObjCObjectPointerType(LHSTy) || |
| 3016 | Context.isObjCObjectPointerType(RHSTy)) { |
| 3017 | // If both operands are interfaces and either operand can be |
| 3018 | // assigned to the other, use that type as the composite |
| 3019 | // type. This allows |
| 3020 | // xxx ? (A*) a : (B*) b |
| 3021 | // where B is a subclass of A. |
| 3022 | // |
| 3023 | // Additionally, as for assignment, if either type is 'id' |
| 3024 | // allow silent coercion. Finally, if the types are |
| 3025 | // incompatible then make sure to use 'id' as the composite |
| 3026 | // type so the result is acceptable for sending messages to. |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3027 | |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3028 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3029 | // It could return the composite type. |
| 3030 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 3031 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 3032 | if (LHSIface && RHSIface && |
| 3033 | Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { |
| 3034 | compositeType = LHSTy; |
| 3035 | } else if (LHSIface && RHSIface && |
| 3036 | Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { |
| 3037 | compositeType = RHSTy; |
| 3038 | } else if (Context.isObjCIdStructType(lhptee) || |
| 3039 | Context.isObjCIdStructType(rhptee)) { |
| 3040 | compositeType = Context.getObjCIdType(); |
| 3041 | } else if (LHSBPT || RHSBPT) { |
| 3042 | if (!sameKind |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3043 | || !Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3044 | rhptee.getUnqualifiedType())) |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3045 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3046 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3047 | return QualType(); |
| 3048 | } else { |
| 3049 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3050 | << LHSTy << RHSTy |
| 3051 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3052 | QualType incompatTy = Context.getObjCIdType(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3053 | ImpCastExprToType(LHS, incompatTy); |
| 3054 | ImpCastExprToType(RHS, incompatTy); |
Daniel Dunbar | a56f746 | 2008-08-26 00:41:39 +0000 | [diff] [blame] | 3055 | return incompatTy; |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 3056 | } |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3057 | } else if (!sameKind |
| 3058 | || !Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3059 | rhptee.getUnqualifiedType())) { |
| 3060 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3061 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3062 | // In this situation, we assume void* type. No especially good |
| 3063 | // reason, but this is what gcc does, and we do have to pick |
| 3064 | // to get a consistent AST. |
| 3065 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3066 | ImpCastExprToType(LHS, incompatTy); |
| 3067 | ImpCastExprToType(RHS, incompatTy); |
| 3068 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3069 | } |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3070 | // The pointer types are compatible. |
| 3071 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3072 | // differently qualified versions of compatible types, the result type is |
| 3073 | // a pointer to an appropriately qualified version of the *composite* |
| 3074 | // type. |
| 3075 | // FIXME: Need to calculate the composite type. |
| 3076 | // FIXME: Need to add qualifiers |
| 3077 | ImpCastExprToType(LHS, compositeType); |
| 3078 | ImpCastExprToType(RHS, compositeType); |
| 3079 | return compositeType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3080 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3081 | |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3082 | // GCC compatibility: soften pointer/integer mismatch. |
| 3083 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3084 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3085 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3086 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 3087 | return RHSTy; |
| 3088 | } |
| 3089 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3090 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3091 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3092 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 3093 | return LHSTy; |
| 3094 | } |
| 3095 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3096 | // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type |
| 3097 | // evaluates to "struct objc_object *" (and is handled above when comparing |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3098 | // id with statically typed objects). |
| 3099 | if (LHSTy->isObjCQualifiedIdType() || RHSTy->isObjCQualifiedIdType()) { |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3100 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3101 | // id. Currently localizing to here until clear this should be |
| 3102 | // part of ObjCQualifiedIdTypesAreCompatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3103 | if (ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true) || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3104 | (LHSTy->isObjCQualifiedIdType() && |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3105 | Context.isObjCObjectPointerType(RHSTy)) || |
| 3106 | (RHSTy->isObjCQualifiedIdType() && |
| 3107 | Context.isObjCObjectPointerType(LHSTy))) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3108 | // FIXME: This is not the correct composite type. This only happens to |
| 3109 | // work because id can more or less be used anywhere, however this may |
| 3110 | // change the type of method sends. |
| 3111 | |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3112 | // FIXME: gcc adds some type-checking of the arguments and emits |
| 3113 | // (confusing) incompatible comparison warnings in some |
| 3114 | // cases. Investigate. |
| 3115 | QualType compositeType = Context.getObjCIdType(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3116 | ImpCastExprToType(LHS, compositeType); |
| 3117 | ImpCastExprToType(RHS, compositeType); |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3118 | return compositeType; |
| 3119 | } |
| 3120 | } |
| 3121 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3122 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3123 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3124 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3125 | return QualType(); |
| 3126 | } |
| 3127 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3128 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3129 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3130 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3131 | SourceLocation ColonLoc, |
| 3132 | ExprArg Cond, ExprArg LHS, |
| 3133 | ExprArg RHS) { |
| 3134 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3135 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3136 | |
| 3137 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3138 | // was the condition. |
| 3139 | bool isLHSNull = LHSExpr == 0; |
| 3140 | if (isLHSNull) |
| 3141 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3142 | |
| 3143 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 3144 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3146 | return ExprError(); |
| 3147 | |
| 3148 | Cond.release(); |
| 3149 | LHS.release(); |
| 3150 | RHS.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3151 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3152 | isLHSNull ? 0 : LHSExpr, |
| 3153 | RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3156 | |
| 3157 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3158 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3159 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3160 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3161 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3162 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3163 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 3164 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3165 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3166 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 3167 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 3168 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3169 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3170 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3171 | lhptee = Context.getCanonicalType(lhptee); |
| 3172 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3173 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3174 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3175 | |
| 3176 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3177 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3178 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3179 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3180 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3181 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3182 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3183 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3184 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3185 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3186 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3187 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3188 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3190 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3191 | assert(rhptee->isFunctionType()); |
| 3192 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3193 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3194 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3195 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3196 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3197 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3198 | |
| 3199 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3200 | assert(lhptee->isFunctionType()); |
| 3201 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3202 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3203 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3204 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3205 | lhptee = lhptee.getUnqualifiedType(); |
| 3206 | rhptee = rhptee.getUnqualifiedType(); |
| 3207 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3208 | // Check if the pointee types are compatible ignoring the sign. |
| 3209 | // We explicitly check for char so that we catch "char" vs |
| 3210 | // "unsigned char" on systems where "char" is unsigned. |
| 3211 | if (lhptee->isCharType()) { |
| 3212 | lhptee = Context.UnsignedCharTy; |
| 3213 | } else if (lhptee->isSignedIntegerType()) { |
| 3214 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 3215 | } |
| 3216 | if (rhptee->isCharType()) { |
| 3217 | rhptee = Context.UnsignedCharTy; |
| 3218 | } else if (rhptee->isSignedIntegerType()) { |
| 3219 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 3220 | } |
| 3221 | if (lhptee == rhptee) { |
| 3222 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3223 | // takes priority over sign incompatibility because the sign |
| 3224 | // warning can be disabled. |
| 3225 | if (ConvTy != Compatible) |
| 3226 | return ConvTy; |
| 3227 | return IncompatiblePointerSign; |
| 3228 | } |
| 3229 | // General pointer incompatibility takes priority over qualifiers. |
| 3230 | return IncompatiblePointer; |
| 3231 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3232 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3235 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3236 | /// block pointer types are compatible or whether a block and normal pointer |
| 3237 | /// are compatible. It is more restrict than comparing two function pointer |
| 3238 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3239 | Sema::AssignConvertType |
| 3240 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3241 | QualType rhsType) { |
| 3242 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3243 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3244 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| 3245 | lhptee = lhsType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3246 | rhptee = rhsType->getAsBlockPointerType()->getPointeeType(); |
| 3247 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3248 | // make sure we operate on the canonical type |
| 3249 | lhptee = Context.getCanonicalType(lhptee); |
| 3250 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3251 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3252 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3253 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3254 | // For blocks we enforce that qualifiers are identical. |
| 3255 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 3256 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3257 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3258 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3259 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3260 | return ConvTy; |
| 3261 | } |
| 3262 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3263 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3264 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3265 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3266 | /// |
| 3267 | /// int a, *pint; |
| 3268 | /// short *pshort; |
| 3269 | /// struct foo *pfoo; |
| 3270 | /// |
| 3271 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3272 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3273 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3274 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3275 | /// |
| 3276 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3277 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3278 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3279 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3280 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3281 | // Get canonical types. We're not formatting these types, just comparing |
| 3282 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3283 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3284 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3285 | |
| 3286 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3287 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3289 | // If the left-hand side is a reference type, then we are in a |
| 3290 | // (rare!) case where we've allowed the use of references in C, |
| 3291 | // e.g., as a parameter type in a built-in function. In this case, |
| 3292 | // just make sure that the type referenced is compatible with the |
| 3293 | // right-hand side type. The caller is responsible for adjusting |
| 3294 | // lhsType so that the resulting expression does not have reference |
| 3295 | // type. |
| 3296 | if (const ReferenceType *lhsTypeRef = lhsType->getAsReferenceType()) { |
| 3297 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3298 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3299 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3300 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3301 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3302 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { |
| 3303 | if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3304 | return Compatible; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 3305 | // Relax integer conversions like we do for pointers below. |
| 3306 | if (rhsType->isIntegerType()) |
| 3307 | return IntToPointer; |
| 3308 | if (lhsType->isIntegerType()) |
| 3309 | return PointerToInt; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 3310 | return IncompatibleObjCQualifiedId; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3311 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3312 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3313 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3314 | // For ExtVector, allow vector splats; float -> <n x float> |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3315 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) |
| 3316 | if (LV->getElementType() == rhsType) |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3317 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3318 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3319 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3320 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3321 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3322 | if (getLangOptions().LaxVectorConversions && |
| 3323 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3324 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3325 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3326 | } |
| 3327 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3328 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3330 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3332 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3333 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3334 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3335 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3337 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3338 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3339 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3340 | if (rhsType->getAsBlockPointerType()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3341 | if (lhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3342 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3343 | |
| 3344 | // Treat block pointers as objects. |
| 3345 | if (getLangOptions().ObjC1 && |
| 3346 | lhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 3347 | return Compatible; |
| 3348 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3349 | return Incompatible; |
| 3350 | } |
| 3351 | |
| 3352 | if (isa<BlockPointerType>(lhsType)) { |
| 3353 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3354 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3355 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3356 | // Treat block pointers as objects. |
| 3357 | if (getLangOptions().ObjC1 && |
| 3358 | rhsType == Context.getCanonicalType(Context.getObjCIdType())) |
| 3359 | return Compatible; |
| 3360 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3361 | if (rhsType->isBlockPointerType()) |
| 3362 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3363 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3364 | if (const PointerType *RHSPT = rhsType->getAsPointerType()) { |
| 3365 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3366 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3367 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3368 | return Incompatible; |
| 3369 | } |
| 3370 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3371 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3372 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3373 | if (lhsType == Context.BoolTy) |
| 3374 | return Compatible; |
| 3375 | |
| 3376 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3377 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3378 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3379 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3380 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3381 | |
| 3382 | if (isa<BlockPointerType>(lhsType) && |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3383 | rhsType->getAsPointerType()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3384 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3385 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3386 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3388 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3389 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3390 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3391 | } |
| 3392 | return Incompatible; |
| 3393 | } |
| 3394 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3395 | /// \brief Constructs a transparent union from an expression that is |
| 3396 | /// used to initialize the transparent union. |
| 3397 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
| 3398 | QualType UnionType, FieldDecl *Field) { |
| 3399 | // Build an initializer list that designates the appropriate member |
| 3400 | // of the transparent union. |
| 3401 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3402 | &E, 1, |
| 3403 | SourceLocation()); |
| 3404 | Initializer->setType(UnionType); |
| 3405 | Initializer->setInitializedFieldInUnion(Field); |
| 3406 | |
| 3407 | // Build a compound literal constructing a value of the transparent |
| 3408 | // union type from this initializer list. |
| 3409 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3410 | false); |
| 3411 | } |
| 3412 | |
| 3413 | Sema::AssignConvertType |
| 3414 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3415 | QualType FromType = rExpr->getType(); |
| 3416 | |
| 3417 | // If the ArgType is a Union type, we want to handle a potential |
| 3418 | // transparent_union GCC extension. |
| 3419 | const RecordType *UT = ArgType->getAsUnionType(); |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 3420 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>(Context)) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3421 | return Incompatible; |
| 3422 | |
| 3423 | // The field to initialize within the transparent union. |
| 3424 | RecordDecl *UD = UT->getDecl(); |
| 3425 | FieldDecl *InitField = 0; |
| 3426 | // It's compatible if the expression matches any of the fields. |
| 3427 | for (RecordDecl::field_iterator it = UD->field_begin(Context), |
| 3428 | itend = UD->field_end(Context); |
| 3429 | it != itend; ++it) { |
| 3430 | if (it->getType()->isPointerType()) { |
| 3431 | // If the transparent union contains a pointer type, we allow: |
| 3432 | // 1) void pointer |
| 3433 | // 2) null pointer constant |
| 3434 | if (FromType->isPointerType()) |
| 3435 | if (FromType->getAsPointerType()->getPointeeType()->isVoidType()) { |
| 3436 | ImpCastExprToType(rExpr, it->getType()); |
| 3437 | InitField = *it; |
| 3438 | break; |
| 3439 | } |
| 3440 | |
| 3441 | if (rExpr->isNullPointerConstant(Context)) { |
| 3442 | ImpCastExprToType(rExpr, it->getType()); |
| 3443 | InitField = *it; |
| 3444 | break; |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 3449 | == Compatible) { |
| 3450 | InitField = *it; |
| 3451 | break; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | if (!InitField) |
| 3456 | return Incompatible; |
| 3457 | |
| 3458 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 3459 | return Compatible; |
| 3460 | } |
| 3461 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3462 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3463 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3464 | if (getLangOptions().CPlusPlus) { |
| 3465 | if (!lhsType->isRecordType()) { |
| 3466 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3467 | // expression is implicitly converted (C++ 4) to the |
| 3468 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3469 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3470 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3471 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 3472 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3476 | // structures. |
| 3477 | } |
| 3478 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3479 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3480 | // a null pointer constant. |
Steve Naroff | f7f52e7 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 3481 | if ((lhsType->isPointerType() || |
| 3482 | lhsType->isObjCQualifiedIdType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3483 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3484 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3485 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3486 | return Compatible; |
| 3487 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3489 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3490 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3491 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3492 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3493 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3494 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3495 | if (!lhsType->isReferenceType()) |
| 3496 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3497 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3498 | Sema::AssignConvertType result = |
| 3499 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3500 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3501 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3502 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3503 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3504 | // so that we can use references in built-in functions even in C. |
| 3505 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3506 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3507 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3508 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3509 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3512 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3513 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3514 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3515 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3516 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3519 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3520 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3521 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3522 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3523 | QualType lhsType = |
| 3524 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3525 | QualType rhsType = |
| 3526 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3527 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3528 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3529 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3530 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3531 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3532 | // Handle the case of a vector & extvector type of the same size and element |
| 3533 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3534 | if (getLangOptions().LaxVectorConversions) { |
| 3535 | // FIXME: Should we warn here? |
| 3536 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3537 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3538 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3539 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3540 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3541 | } |
| 3542 | } |
| 3543 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3544 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3545 | // If the lhs is an extended vector and the rhs is a scalar of the same type |
| 3546 | // or a literal, promote the rhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3547 | if (const ExtVectorType *V = lhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3548 | QualType eltType = V->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3549 | |
| 3550 | if ((eltType->getAsBuiltinType() == rhsType->getAsBuiltinType()) || |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3551 | (eltType->isIntegerType() && isa<IntegerLiteral>(rex)) || |
| 3552 | (eltType->isFloatingType() && isa<FloatingLiteral>(rex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3553 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3554 | return lhsType; |
| 3555 | } |
| 3556 | } |
| 3557 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3558 | // If the rhs is an extended vector and the lhs is a scalar of the same type, |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3559 | // promote the lhs to the vector type. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3560 | if (const ExtVectorType *V = rhsType->getAsExtVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3561 | QualType eltType = V->getElementType(); |
| 3562 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3563 | if ((eltType->getAsBuiltinType() == lhsType->getAsBuiltinType()) || |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3564 | (eltType->isIntegerType() && isa<IntegerLiteral>(lex)) || |
| 3565 | (eltType->isFloatingType() && isa<FloatingLiteral>(lex))) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3566 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3567 | return rhsType; |
| 3568 | } |
| 3569 | } |
| 3570 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3571 | // You cannot convert between vector values of different size. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3572 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3573 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3574 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3578 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3579 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3580 | { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 3581 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3582 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3583 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3584 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3585 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3586 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3587 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3588 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3592 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3593 | { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 3594 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3595 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 3596 | return CheckVectorOperands(Loc, lex, rex); |
| 3597 | return InvalidOperands(Loc, lex, rex); |
| 3598 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3599 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3600 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3601 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3602 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3603 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3604 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3608 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3609 | { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3610 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3611 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3612 | if (CompLHSTy) *CompLHSTy = compType; |
| 3613 | return compType; |
| 3614 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3615 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3616 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3618 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3619 | if (lex->getType()->isArithmeticType() && |
| 3620 | rex->getType()->isArithmeticType()) { |
| 3621 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3622 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3623 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3624 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3625 | // Put any potential pointer into PExp |
| 3626 | Expr* PExp = lex, *IExp = rex; |
| 3627 | if (IExp->getType()->isPointerType()) |
| 3628 | std::swap(PExp, IExp); |
| 3629 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3630 | if (const PointerType *PTy = PExp->getType()->getAsPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3631 | if (IExp->getType()->isIntegerType()) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3632 | QualType PointeeTy = PTy->getPointeeType(); |
| 3633 | // Check for arithmetic on pointers to incomplete types. |
| 3634 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3635 | if (getLangOptions().CPlusPlus) { |
| 3636 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3637 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3638 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3639 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3640 | |
| 3641 | // GNU extension: arithmetic on pointer to void |
| 3642 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3643 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3644 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3645 | if (getLangOptions().CPlusPlus) { |
| 3646 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3647 | << lex->getType() << lex->getSourceRange(); |
| 3648 | return QualType(); |
| 3649 | } |
| 3650 | |
| 3651 | // GNU extension: arithmetic on pointer to function |
| 3652 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3653 | << lex->getType() << lex->getSourceRange(); |
| 3654 | } else if (!PTy->isDependentType() && |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3655 | RequireCompleteType(Loc, PointeeTy, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3656 | diag::err_typecheck_arithmetic_incomplete_type, |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3657 | PExp->getSourceRange(), SourceRange(), |
| 3658 | PExp->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3659 | return QualType(); |
| 3660 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3661 | // Diagnose bad cases where we step over interface counts. |
| 3662 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 3663 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 3664 | << PointeeTy << PExp->getSourceRange(); |
| 3665 | return QualType(); |
| 3666 | } |
| 3667 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3668 | if (CompLHSTy) { |
| 3669 | QualType LHSTy = lex->getType(); |
| 3670 | if (LHSTy->isPromotableIntegerType()) |
| 3671 | LHSTy = Context.IntTy; |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 3672 | else { |
| 3673 | QualType T = isPromotableBitField(lex, Context); |
| 3674 | if (!T.isNull()) |
| 3675 | LHSTy = T; |
| 3676 | } |
| 3677 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3678 | *CompLHSTy = LHSTy; |
| 3679 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3680 | return PExp->getType(); |
| 3681 | } |
| 3682 | } |
| 3683 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3684 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3687 | // C99 6.5.6 |
| 3688 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3689 | SourceLocation Loc, QualType* CompLHSTy) { |
| 3690 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3691 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3692 | if (CompLHSTy) *CompLHSTy = compType; |
| 3693 | return compType; |
| 3694 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3695 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3696 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3697 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3698 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3699 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3700 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 3701 | if (lex->getType()->isArithmeticType() |
| 3702 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3703 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3704 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3705 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3706 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3707 | // Either ptr - int or ptr - ptr. |
| 3708 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 2565eef | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 3709 | QualType lpointee = LHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3710 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3711 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3713 | bool ComplainAboutVoid = false; |
| 3714 | Expr *ComplainAboutFunc = 0; |
| 3715 | if (lpointee->isVoidType()) { |
| 3716 | if (getLangOptions().CPlusPlus) { |
| 3717 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3718 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3719 | return QualType(); |
| 3720 | } |
| 3721 | |
| 3722 | // GNU C extension: arithmetic on pointer to void |
| 3723 | ComplainAboutVoid = true; |
| 3724 | } else if (lpointee->isFunctionType()) { |
| 3725 | if (getLangOptions().CPlusPlus) { |
| 3726 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3727 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3728 | return QualType(); |
| 3729 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3730 | |
| 3731 | // GNU C extension: arithmetic on pointer to function |
| 3732 | ComplainAboutFunc = lex; |
| 3733 | } else if (!lpointee->isDependentType() && |
| 3734 | RequireCompleteType(Loc, lpointee, |
| 3735 | diag::err_typecheck_sub_ptr_object, |
| 3736 | lex->getSourceRange(), |
| 3737 | SourceRange(), |
| 3738 | lex->getType())) |
| 3739 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3740 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3741 | // Diagnose bad cases where we step over interface counts. |
| 3742 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 3743 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 3744 | << lpointee << lex->getSourceRange(); |
| 3745 | return QualType(); |
| 3746 | } |
| 3747 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3748 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3749 | if (rex->getType()->isIntegerType()) { |
| 3750 | if (ComplainAboutVoid) |
| 3751 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3752 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3753 | if (ComplainAboutFunc) |
| 3754 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3755 | << ComplainAboutFunc->getType() |
| 3756 | << ComplainAboutFunc->getSourceRange(); |
| 3757 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3758 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3759 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3760 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3762 | // Handle pointer-pointer subtractions. |
| 3763 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 3764 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3766 | // RHS must be a completely-type object type. |
| 3767 | // Handle the GNU void* extension. |
| 3768 | if (rpointee->isVoidType()) { |
| 3769 | if (getLangOptions().CPlusPlus) { |
| 3770 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 3771 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3772 | return QualType(); |
| 3773 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3775 | ComplainAboutVoid = true; |
| 3776 | } else if (rpointee->isFunctionType()) { |
| 3777 | if (getLangOptions().CPlusPlus) { |
| 3778 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3779 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3780 | return QualType(); |
| 3781 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3782 | |
| 3783 | // GNU extension: arithmetic on pointer to function |
| 3784 | if (!ComplainAboutFunc) |
| 3785 | ComplainAboutFunc = rex; |
| 3786 | } else if (!rpointee->isDependentType() && |
| 3787 | RequireCompleteType(Loc, rpointee, |
| 3788 | diag::err_typecheck_sub_ptr_object, |
| 3789 | rex->getSourceRange(), |
| 3790 | SourceRange(), |
| 3791 | rex->getType())) |
| 3792 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3793 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 3794 | if (getLangOptions().CPlusPlus) { |
| 3795 | // Pointee types must be the same: C++ [expr.add] |
| 3796 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 3797 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 3798 | << lex->getType() << rex->getType() |
| 3799 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3800 | return QualType(); |
| 3801 | } |
| 3802 | } else { |
| 3803 | // Pointee types must be compatible C99 6.5.6p3 |
| 3804 | if (!Context.typesAreCompatible( |
| 3805 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 3806 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 3807 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 3808 | << lex->getType() << rex->getType() |
| 3809 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3810 | return QualType(); |
| 3811 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3812 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3813 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3814 | if (ComplainAboutVoid) |
| 3815 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3816 | << lex->getSourceRange() << rex->getSourceRange(); |
| 3817 | if (ComplainAboutFunc) |
| 3818 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3819 | << ComplainAboutFunc->getType() |
| 3820 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3821 | |
| 3822 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 3823 | return Context.getPointerDiffType(); |
| 3824 | } |
| 3825 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3826 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3827 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3830 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3831 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3832 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3833 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 3834 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3835 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3836 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3837 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 3838 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3839 | QualType LHSTy; |
| 3840 | if (lex->getType()->isPromotableIntegerType()) |
| 3841 | LHSTy = Context.IntTy; |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 3842 | else { |
| 3843 | LHSTy = isPromotableBitField(lex, Context); |
| 3844 | if (LHSTy.isNull()) |
| 3845 | LHSTy = lex->getType(); |
| 3846 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 3847 | if (!isCompAssign) |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3848 | ImpCastExprToType(lex, LHSTy); |
| 3849 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3850 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3851 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3852 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3853 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3856 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3857 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3858 | unsigned OpaqueOpc, bool isRelational) { |
| 3859 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 3860 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3861 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3862 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3863 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3864 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 3865 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 3866 | UsualArithmeticConversions(lex, rex); |
| 3867 | else { |
| 3868 | UsualUnaryConversions(lex); |
| 3869 | UsualUnaryConversions(rex); |
| 3870 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3871 | QualType lType = lex->getType(); |
| 3872 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3873 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 3874 | if (!lType->isFloatingType() |
| 3875 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3876 | // For non-floating point types, check for self-comparisons of the form |
| 3877 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 3878 | // often indicate logic errors in the program. |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 3879 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
| 3880 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3881 | Expr *LHSStripped = lex->IgnoreParens(); |
| 3882 | Expr *RHSStripped = rex->IgnoreParens(); |
| 3883 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 3884 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 3885 | if (DRL->getDecl() == DRR->getDecl() && |
| 3886 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3887 | Diag(Loc, diag::warn_selfcomparison); |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3888 | |
| 3889 | if (isa<CastExpr>(LHSStripped)) |
| 3890 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 3891 | if (isa<CastExpr>(RHSStripped)) |
| 3892 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| 3893 | |
| 3894 | // Warn about comparisons against a string constant (unless the other |
| 3895 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3896 | Expr *literalString = 0; |
| 3897 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3898 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3899 | !RHSStripped->isNullPointerConstant(Context)) { |
| 3900 | literalString = lex; |
| 3901 | literalStringStripped = LHSStripped; |
| 3902 | } |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3903 | else if ((isa<StringLiteral>(RHSStripped) || |
| 3904 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3905 | !LHSStripped->isNullPointerConstant(Context)) { |
| 3906 | literalString = rex; |
| 3907 | literalStringStripped = RHSStripped; |
| 3908 | } |
| 3909 | |
| 3910 | if (literalString) { |
| 3911 | std::string resultComparison; |
| 3912 | switch (Opc) { |
| 3913 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 3914 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 3915 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 3916 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 3917 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 3918 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 3919 | default: assert(false && "Invalid comparison operator"); |
| 3920 | } |
| 3921 | Diag(Loc, diag::warn_stringcompare) |
| 3922 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 3923 | << literalString->getSourceRange() |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3924 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 3925 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 3926 | "strcmp(") |
| 3927 | << CodeModificationHint::CreateInsertion( |
| 3928 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 3929 | resultComparison); |
| 3930 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 3931 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3933 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3934 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3935 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3936 | if (isRelational) { |
| 3937 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3938 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3939 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3940 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 3941 | if (lType->isFloatingType()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 3942 | assert(rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3943 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 3944 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3946 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3947 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3948 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3949 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 3950 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 3951 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3952 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 3953 | // All of the following pointer related warnings are GCC extensions, except |
| 3954 | // when handling null pointer constants. One day, we can consider making them |
| 3955 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 3956 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3957 | QualType LCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3958 | Context.getCanonicalType(lType->getAsPointerType()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3959 | QualType RCanPointeeTy = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3960 | Context.getCanonicalType(rType->getAsPointerType()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3961 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3962 | // Simple check: if the pointee types are identical, we're done. |
| 3963 | if (LCanPointeeTy == RCanPointeeTy) |
| 3964 | return ResultTy; |
| 3965 | |
| 3966 | if (getLangOptions().CPlusPlus) { |
| 3967 | // C++ [expr.rel]p2: |
| 3968 | // [...] Pointer conversions (4.10) and qualification |
| 3969 | // conversions (4.4) are performed on pointer operands (or on |
| 3970 | // a pointer operand and a null pointer constant) to bring |
| 3971 | // them to their composite pointer type. [...] |
| 3972 | // |
| 3973 | // C++ [expr.eq]p2 uses the same notion for (in)equality |
| 3974 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 3975 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 3976 | if (T.isNull()) { |
| 3977 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 3978 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 3979 | return QualType(); |
| 3980 | } |
| 3981 | |
| 3982 | ImpCastExprToType(lex, T); |
| 3983 | ImpCastExprToType(rex, T); |
| 3984 | return ResultTy; |
| 3985 | } |
| 3986 | |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 3987 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 3988 | !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() && |
| 3989 | !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3990 | RCanPointeeTy.getUnqualifiedType()) && |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3991 | !Context.areComparableObjCPointerTypes(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3992 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3993 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3994 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3995 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 3996 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 3997 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3998 | // C++ allows comparison of pointers with null pointer constants. |
| 3999 | if (getLangOptions().CPlusPlus) { |
| 4000 | if (lType->isPointerType() && RHSIsNull) { |
| 4001 | ImpCastExprToType(rex, lType); |
| 4002 | return ResultTy; |
| 4003 | } |
| 4004 | if (rType->isPointerType() && LHSIsNull) { |
| 4005 | ImpCastExprToType(lex, rType); |
| 4006 | return ResultTy; |
| 4007 | } |
| 4008 | // And comparison of nullptr_t with itself. |
| 4009 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4010 | return ResultTy; |
| 4011 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4012 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4013 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4014 | QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); |
| 4015 | QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4016 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4017 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4018 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4019 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4020 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4021 | } |
| 4022 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4023 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4024 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4025 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4026 | if (!isRelational |
| 4027 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4028 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4029 | if (!LHSIsNull && !RHSIsNull) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4030 | if (!((rType->isPointerType() && rType->getAsPointerType() |
| 4031 | ->getPointeeType()->isVoidType()) |
| 4032 | || (lType->isPointerType() && lType->getAsPointerType() |
| 4033 | ->getPointeeType()->isVoidType()))) |
| 4034 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4035 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4036 | } |
| 4037 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4038 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4039 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4040 | |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4041 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4042 | if (lType->isPointerType() || rType->isPointerType()) { |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4043 | const PointerType *LPT = lType->getAsPointerType(); |
| 4044 | const PointerType *RPT = rType->getAsPointerType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4045 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4046 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4047 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4048 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4049 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4050 | if (!LPtrToVoid && !RPtrToVoid && |
| 4051 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4052 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4053 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4054 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4055 | return ResultTy; |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4056 | } |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4057 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4058 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4059 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4060 | if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
| 4061 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4062 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4063 | } else { |
| 4064 | if ((lType->isObjCQualifiedIdType() && rType->isObjCQualifiedIdType())) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4065 | Diag(Loc, diag::warn_incompatible_qualified_id_operands) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4066 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4067 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4068 | return ResultTy; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 4069 | } |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4070 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4071 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4072 | if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4073 | rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4074 | if (!RHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4075 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4076 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4077 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4078 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4079 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4080 | if (lType->isIntegerType() && |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4081 | (rType->isPointerType() || rType->isObjCQualifiedIdType())) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4082 | if (!LHSIsNull) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4083 | Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4084 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4085 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4086 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4087 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4088 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4089 | if (!isRelational && RHSIsNull |
| 4090 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4091 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4092 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4093 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4094 | if (!isRelational && LHSIsNull |
| 4095 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4096 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4097 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4098 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4099 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4102 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4103 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4104 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4105 | /// types. |
| 4106 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4107 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4108 | bool isRelational) { |
| 4109 | // Check to make sure we're operating on vectors of the same type and width, |
| 4110 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4111 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4112 | if (vType.isNull()) |
| 4113 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4114 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4115 | QualType lType = lex->getType(); |
| 4116 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4117 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4118 | // For non-floating point types, check for self-comparisons of the form |
| 4119 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4120 | // often indicate logic errors in the program. |
| 4121 | if (!lType->isFloatingType()) { |
| 4122 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4123 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4124 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4125 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4126 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4127 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4128 | // Check for comparisons of floating point operands using != and ==. |
| 4129 | if (!isRelational && lType->isFloatingType()) { |
| 4130 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4131 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4132 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4133 | |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4134 | // FIXME: Vector compare support in the LLVM backend is not fully reliable, |
| 4135 | // just reject all vector comparisons for now. |
| 4136 | if (1) { |
| 4137 | Diag(Loc, diag::err_typecheck_vector_comparison) |
| 4138 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4139 | return QualType(); |
| 4140 | } |
| 4141 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4142 | // Return the type for the comparison, which is the same as vector type for |
| 4143 | // integer vectors, or an integer type of identical size and number of |
| 4144 | // elements for floating point vectors. |
| 4145 | if (lType->isIntegerType()) |
| 4146 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4147 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4148 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4149 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4150 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4151 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4152 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4153 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4154 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4155 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4156 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4157 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4158 | } |
| 4159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4160 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4161 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4162 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 4163 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4164 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4165 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4166 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4167 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4168 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4169 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4170 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4174 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4175 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4176 | UsualUnaryConversions(lex); |
| 4177 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4178 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 4179 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4180 | return Context.IntTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4181 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4184 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4185 | /// is a read-only property; return true if so. A readonly property expression |
| 4186 | /// depends on various declarations and thus must be treated specially. |
| 4187 | /// |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4188 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4189 | { |
| 4190 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4191 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4192 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4193 | QualType BaseType = PropExpr->getBase()->getType(); |
| 4194 | if (const PointerType *PTy = BaseType->getAsPointerType()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4195 | if (const ObjCInterfaceType *IFTy = |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4196 | PTy->getPointeeType()->getAsObjCInterfaceType()) |
| 4197 | if (ObjCInterfaceDecl *IFace = IFTy->getDecl()) |
| 4198 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4199 | return true; |
| 4200 | } |
| 4201 | } |
| 4202 | return false; |
| 4203 | } |
| 4204 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4205 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4206 | /// emit an error and return true. If so, return false. |
| 4207 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4208 | SourceLocation OrigLoc = Loc; |
| 4209 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
| 4210 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4211 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4212 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4213 | if (IsLV == Expr::MLV_Valid) |
| 4214 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4215 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4216 | unsigned Diag = 0; |
| 4217 | bool NeedType = false; |
| 4218 | switch (IsLV) { // C99 6.5.16p2 |
| 4219 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4220 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4221 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4222 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4223 | NeedType = true; |
| 4224 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4225 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4226 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4227 | NeedType = true; |
| 4228 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4229 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4230 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4231 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4232 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4233 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4234 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4235 | case Expr::MLV_IncompleteType: |
| 4236 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4237 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4238 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 4239 | E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4240 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4241 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4242 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4243 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4244 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4245 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4246 | case Expr::MLV_ReadonlyProperty: |
| 4247 | Diag = diag::error_readonly_property_assignment; |
| 4248 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4249 | case Expr::MLV_NoSetterProperty: |
| 4250 | Diag = diag::error_nosetter_property_assignment; |
| 4251 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4252 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4253 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4254 | SourceRange Assign; |
| 4255 | if (Loc != OrigLoc) |
| 4256 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4257 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4258 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4259 | else |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4260 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4261 | return true; |
| 4262 | } |
| 4263 | |
| 4264 | |
| 4265 | |
| 4266 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4267 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4268 | SourceLocation Loc, |
| 4269 | QualType CompoundType) { |
| 4270 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4271 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4272 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4273 | |
| 4274 | QualType LHSType = LHS->getType(); |
| 4275 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4276 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4277 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4278 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4279 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4280 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4281 | // Special case of NSObject attributes on c-style pointer types. |
| 4282 | if (ConvTy == IncompatiblePointer && |
| 4283 | ((Context.isObjCNSObjectType(LHSType) && |
| 4284 | Context.isObjCObjectPointerType(RHSType)) || |
| 4285 | (Context.isObjCNSObjectType(RHSType) && |
| 4286 | Context.isObjCObjectPointerType(LHSType)))) |
| 4287 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4288 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4289 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 4290 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 4291 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4292 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4293 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 4294 | RHSCheck = ICE->getSubExpr(); |
| 4295 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 4296 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 4297 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4298 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4299 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4300 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 4301 | // And there is a space or other character before the subexpr of the |
| 4302 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 4303 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 4304 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4305 | Diag(Loc, diag::warn_not_compound_assign) |
| 4306 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 4307 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4308 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4309 | } |
| 4310 | } else { |
| 4311 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 4312 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4313 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4314 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4315 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 4316 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4317 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4319 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 4320 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4321 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4322 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 4323 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4324 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4325 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4326 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4329 | // C99 6.5.17 |
| 4330 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 4331 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4332 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4333 | |
| 4334 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 4335 | // incomplete in C++). |
| 4336 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4337 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4340 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 4341 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4342 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 4343 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4344 | if (Op->isTypeDependent()) |
| 4345 | return Context.DependentTy; |
| 4346 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4347 | QualType ResType = Op->getType(); |
| 4348 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4349 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4350 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 4351 | // Decrement of bool is not allowed. |
| 4352 | if (!isInc) { |
| 4353 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 4354 | return QualType(); |
| 4355 | } |
| 4356 | // Increment of bool sets it to true, but is deprecated. |
| 4357 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 4358 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4359 | // OK! |
| 4360 | } else if (const PointerType *PT = ResType->getAsPointerType()) { |
| 4361 | // C99 6.5.2.4p2, 6.5.6p2 |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 4362 | if (PT->getPointeeType()->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4363 | if (getLangOptions().CPlusPlus) { |
| 4364 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 4365 | << Op->getSourceRange(); |
| 4366 | return QualType(); |
| 4367 | } |
| 4368 | |
| 4369 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4370 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4371 | } else if (PT->getPointeeType()->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4372 | if (getLangOptions().CPlusPlus) { |
| 4373 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 4374 | << Op->getType() << Op->getSourceRange(); |
| 4375 | return QualType(); |
| 4376 | } |
| 4377 | |
| 4378 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4379 | << ResType << Op->getSourceRange(); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 4380 | } else if (RequireCompleteType(OpLoc, PT->getPointeeType(), |
| 4381 | diag::err_typecheck_arithmetic_incomplete_type, |
| 4382 | Op->getSourceRange(), SourceRange(), |
| 4383 | ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4384 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4385 | } else if (ResType->isComplexType()) { |
| 4386 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 4387 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4388 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4389 | } else { |
| 4390 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4391 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4392 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4393 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4394 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 4395 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4396 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4397 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4398 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4401 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4402 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4403 | /// where the declaration is needed for type checking. We only need to |
| 4404 | /// handle cases when the expression references a function designator |
| 4405 | /// or is an lvalue. Here are some examples: |
| 4406 | /// - &(x) => x |
| 4407 | /// - &*****f => f for f a function designator. |
| 4408 | /// - &s.xx => s |
| 4409 | /// - &s.zz[1].yy -> s, if zz is an array |
| 4410 | /// - *(x + 1) -> x, if x is an array |
| 4411 | /// - &"123"[2] -> 0 |
| 4412 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4413 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4414 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4415 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 4416 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4417 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4418 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4419 | // If this is an arrow operator, the address is an offset from |
| 4420 | // the base's value, so the object the base refers to is |
| 4421 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4422 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4423 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4424 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4425 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4426 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4427 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 4428 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4429 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 4430 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 4431 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 4432 | return getPrimaryDecl(ICE->getSubExpr()); |
| 4433 | } |
| 4434 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4435 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4436 | case Stmt::UnaryOperatorClass: { |
| 4437 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4438 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4439 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4440 | case UnaryOperator::Real: |
| 4441 | case UnaryOperator::Imag: |
| 4442 | case UnaryOperator::Extension: |
| 4443 | return getPrimaryDecl(UO->getSubExpr()); |
| 4444 | default: |
| 4445 | return 0; |
| 4446 | } |
| 4447 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4448 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4449 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4450 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4451 | // If the result of an implicit cast is an l-value, we care about |
| 4452 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4453 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4454 | default: |
| 4455 | return 0; |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4460 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4461 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4462 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4463 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4464 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4465 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4466 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4467 | // Make sure to ignore parentheses in subsequent checks |
| 4468 | op = op->IgnoreParens(); |
| 4469 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 4470 | if (op->isTypeDependent()) |
| 4471 | return Context.DependentTy; |
| 4472 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4473 | if (getLangOptions().C99) { |
| 4474 | // Implement C99-only parts of addressof rules. |
| 4475 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 4476 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 4477 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 4478 | // (assuming the deref expression is valid). |
| 4479 | return uOp->getSubExpr()->getType(); |
| 4480 | } |
| 4481 | // Technically, there should be a check for array subscript |
| 4482 | // expressions here, but the result of one is always an lvalue anyway. |
| 4483 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4484 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 4485 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 4486 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4487 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 4488 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4489 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4490 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4491 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4492 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 4493 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4494 | return QualType(); |
| 4495 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 4496 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4497 | // The operand cannot be a bit-field |
| 4498 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4499 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 4500 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4501 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 4502 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4503 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4504 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4505 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4506 | return QualType(); |
| 4507 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4508 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4509 | // with the register storage-class specifier. |
| 4510 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 4511 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4512 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4513 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4514 | return QualType(); |
| 4515 | } |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4516 | } else if (isa<OverloadedFunctionDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4517 | return Context.OverloadTy; |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4518 | } else if (isa<FieldDecl>(dcl)) { |
| 4519 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4520 | // Could be a pointer to member, though, if there is an explicit |
| 4521 | // scope qualifier for the class. |
| 4522 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 4523 | DeclContext *Ctx = dcl->getDeclContext(); |
| 4524 | if (Ctx && Ctx->isRecord()) |
| 4525 | return Context.getMemberPointerType(op->getType(), |
| 4526 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| 4527 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4528 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 4529 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4530 | // As above. |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4531 | if (isa<QualifiedDeclRefExpr>(op) && MD->isInstance()) |
| 4532 | return Context.getMemberPointerType(op->getType(), |
| 4533 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 4534 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4535 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4536 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4537 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4538 | if (lval == Expr::LV_IncompleteVoidType) { |
| 4539 | // Taking the address of a void variable is technically illegal, but we |
| 4540 | // allow it in cases which are otherwise valid. |
| 4541 | // Example: "extern void x; void* y = &x;". |
| 4542 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 4543 | } |
| 4544 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4545 | // If the operand has type "type", the result has type "pointer to type". |
| 4546 | return Context.getPointerType(op->getType()); |
| 4547 | } |
| 4548 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4549 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4550 | if (Op->isTypeDependent()) |
| 4551 | return Context.DependentTy; |
| 4552 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4553 | UsualUnaryConversions(Op); |
| 4554 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4555 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4556 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 4557 | // incomplete type or void. It would be possible to warn about dereferencing |
| 4558 | // a void pointer, but it's completely well-defined, and such a warning is |
| 4559 | // unlikely to catch any mistakes. |
| 4560 | if (const PointerType *PT = Ty->getAsPointerType()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4561 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4562 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4563 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4564 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4565 | return QualType(); |
| 4566 | } |
| 4567 | |
| 4568 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 4569 | tok::TokenKind Kind) { |
| 4570 | BinaryOperator::Opcode Opc; |
| 4571 | switch (Kind) { |
| 4572 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4573 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 4574 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4575 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 4576 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 4577 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 4578 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 4579 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 4580 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 4581 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 4582 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 4583 | case tok::less: Opc = BinaryOperator::LT; break; |
| 4584 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 4585 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 4586 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 4587 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 4588 | case tok::amp: Opc = BinaryOperator::And; break; |
| 4589 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 4590 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 4591 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 4592 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 4593 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 4594 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 4595 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 4596 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 4597 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 4598 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 4599 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 4600 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 4601 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 4602 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 4603 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 4604 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 4605 | } |
| 4606 | return Opc; |
| 4607 | } |
| 4608 | |
| 4609 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 4610 | tok::TokenKind Kind) { |
| 4611 | UnaryOperator::Opcode Opc; |
| 4612 | switch (Kind) { |
| 4613 | default: assert(0 && "Unknown unary op!"); |
| 4614 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 4615 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 4616 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 4617 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 4618 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 4619 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 4620 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 4621 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4622 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 4623 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 4624 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 4625 | } |
| 4626 | return Opc; |
| 4627 | } |
| 4628 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4629 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 4630 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 4631 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4632 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 4633 | unsigned Op, |
| 4634 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4635 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4636 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4637 | // The following two variables are used for compound assignment operators |
| 4638 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 4639 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4640 | |
| 4641 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4642 | case BinaryOperator::Assign: |
| 4643 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 4644 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4645 | case BinaryOperator::PtrMemD: |
| 4646 | case BinaryOperator::PtrMemI: |
| 4647 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 4648 | Opc == BinaryOperator::PtrMemI); |
| 4649 | break; |
| 4650 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4651 | case BinaryOperator::Div: |
| 4652 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 4653 | break; |
| 4654 | case BinaryOperator::Rem: |
| 4655 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 4656 | break; |
| 4657 | case BinaryOperator::Add: |
| 4658 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 4659 | break; |
| 4660 | case BinaryOperator::Sub: |
| 4661 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 4662 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4663 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4664 | case BinaryOperator::Shr: |
| 4665 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 4666 | break; |
| 4667 | case BinaryOperator::LE: |
| 4668 | case BinaryOperator::LT: |
| 4669 | case BinaryOperator::GE: |
| 4670 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4671 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4672 | break; |
| 4673 | case BinaryOperator::EQ: |
| 4674 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4675 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | case BinaryOperator::And: |
| 4678 | case BinaryOperator::Xor: |
| 4679 | case BinaryOperator::Or: |
| 4680 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 4681 | break; |
| 4682 | case BinaryOperator::LAnd: |
| 4683 | case BinaryOperator::LOr: |
| 4684 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 4685 | break; |
| 4686 | case BinaryOperator::MulAssign: |
| 4687 | case BinaryOperator::DivAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4688 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 4689 | CompLHSTy = CompResultTy; |
| 4690 | if (!CompResultTy.isNull()) |
| 4691 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4692 | break; |
| 4693 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4694 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 4695 | CompLHSTy = CompResultTy; |
| 4696 | if (!CompResultTy.isNull()) |
| 4697 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4698 | break; |
| 4699 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4700 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4701 | if (!CompResultTy.isNull()) |
| 4702 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4703 | break; |
| 4704 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4705 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 4706 | if (!CompResultTy.isNull()) |
| 4707 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4708 | break; |
| 4709 | case BinaryOperator::ShlAssign: |
| 4710 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4711 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 4712 | CompLHSTy = CompResultTy; |
| 4713 | if (!CompResultTy.isNull()) |
| 4714 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4715 | break; |
| 4716 | case BinaryOperator::AndAssign: |
| 4717 | case BinaryOperator::XorAssign: |
| 4718 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4719 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 4720 | CompLHSTy = CompResultTy; |
| 4721 | if (!CompResultTy.isNull()) |
| 4722 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4723 | break; |
| 4724 | case BinaryOperator::Comma: |
| 4725 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 4726 | break; |
| 4727 | } |
| 4728 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4729 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4730 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4731 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 4732 | else |
| 4733 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4734 | CompLHSTy, CompResultTy, |
| 4735 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4738 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4739 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 4740 | tok::TokenKind Kind, |
| 4741 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4742 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 4743 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4744 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4745 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 4746 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4747 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4748 | if (getLangOptions().CPlusPlus && |
| 4749 | (lhs->getType()->isOverloadableType() || |
| 4750 | rhs->getType()->isOverloadableType())) { |
| 4751 | // Find all of the overloaded operators visible from this |
| 4752 | // point. We perform both an operator-name lookup from the local |
| 4753 | // scope and an argument-dependent lookup based on the types of |
| 4754 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 4755 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4756 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 4757 | if (OverOp != OO_None) { |
| 4758 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 4759 | Functions); |
| 4760 | Expr *Args[2] = { lhs, rhs }; |
| 4761 | DeclarationName OpName |
| 4762 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4763 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 4764 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4766 | // Build the (potentially-overloaded, potentially-dependent) |
| 4767 | // binary operation. |
| 4768 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 4771 | // Build a built-in binary operation. |
| 4772 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4775 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| 4776 | unsigned OpcIn, |
| 4777 | ExprArg InputArg) { |
| 4778 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4779 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4780 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4781 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4782 | QualType resultType; |
| 4783 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4784 | case UnaryOperator::PostInc: |
| 4785 | case UnaryOperator::PostDec: |
| 4786 | case UnaryOperator::OffsetOf: |
| 4787 | assert(false && "Invalid unary operator"); |
| 4788 | break; |
| 4789 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4790 | case UnaryOperator::PreInc: |
| 4791 | case UnaryOperator::PreDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4792 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
| 4793 | Opc == UnaryOperator::PreInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4794 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4795 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4796 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 4797 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4798 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 4799 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4800 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 4801 | break; |
| 4802 | case UnaryOperator::Plus: |
| 4803 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4804 | UsualUnaryConversions(Input); |
| 4805 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4806 | if (resultType->isDependentType()) |
| 4807 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 4808 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 4809 | break; |
| 4810 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 4811 | resultType->isEnumeralType()) |
| 4812 | break; |
| 4813 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 4814 | Opc == UnaryOperator::Plus && |
| 4815 | resultType->isPointerType()) |
| 4816 | break; |
| 4817 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4818 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4819 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4820 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4821 | UsualUnaryConversions(Input); |
| 4822 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4823 | if (resultType->isDependentType()) |
| 4824 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4825 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 4826 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 4827 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4828 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4829 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 4830 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4831 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4832 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4833 | break; |
| 4834 | case UnaryOperator::LNot: // logical negation |
| 4835 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4836 | DefaultFunctionArrayConversion(Input); |
| 4837 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4838 | if (resultType->isDependentType()) |
| 4839 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4840 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4841 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 4842 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4843 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4844 | // In C++, it's bool. C++ 5.3.1p8 |
| 4845 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4846 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4847 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4848 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 4849 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 4850 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4851 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4852 | resultType = Input->getType(); |
| 4853 | break; |
| 4854 | } |
| 4855 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4856 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4857 | |
| 4858 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4859 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4862 | // Unary Operators. 'Tok' is the token for the operator. |
| 4863 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 4864 | tok::TokenKind Op, ExprArg input) { |
| 4865 | Expr *Input = (Expr*)input.get(); |
| 4866 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 4867 | |
| 4868 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 4869 | // Find all of the overloaded operators visible from this |
| 4870 | // point. We perform both an operator-name lookup from the local |
| 4871 | // scope and an argument-dependent lookup based on the types of |
| 4872 | // the arguments. |
| 4873 | FunctionSet Functions; |
| 4874 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 4875 | if (OverOp != OO_None) { |
| 4876 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 4877 | Functions); |
| 4878 | DeclarationName OpName |
| 4879 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 4880 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 4881 | } |
| 4882 | |
| 4883 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 4884 | } |
| 4885 | |
| 4886 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 4887 | } |
| 4888 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 4889 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4890 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 4891 | SourceLocation LabLoc, |
| 4892 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4893 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 4894 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4895 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 4896 | // If we haven't seen this label yet, create a forward reference. It |
| 4897 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 4898 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4899 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4901 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4902 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 4903 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4906 | Sema::OwningExprResult |
| 4907 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 4908 | SourceLocation RPLoc) { // "({..})" |
| 4909 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4910 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 4911 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 4912 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4913 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 4914 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4915 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 4916 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4917 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 4918 | // example, it is not possible to goto into a stmt expression apparently. |
| 4919 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4920 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4921 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 4922 | // as the type of the stmtexpr. |
| 4923 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4924 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4925 | if (!Compound->body_empty()) { |
| 4926 | Stmt *LastStmt = Compound->body_back(); |
| 4927 | // If LastStmt is a label, skip down through into the body. |
| 4928 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 4929 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4930 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4931 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4932 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 4933 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4934 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4935 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 4936 | // expressions are not lvalues. |
| 4937 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4938 | substmt.release(); |
| 4939 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 4940 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 4941 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4942 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 4943 | SourceLocation BuiltinLoc, |
| 4944 | SourceLocation TypeLoc, |
| 4945 | TypeTy *argty, |
| 4946 | OffsetOfComponent *CompPtr, |
| 4947 | unsigned NumComponents, |
| 4948 | SourceLocation RPLoc) { |
| 4949 | // FIXME: This function leaks all expressions in the offset components on |
| 4950 | // error. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4951 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 4952 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4953 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4954 | bool Dependent = ArgTy->isDependentType(); |
| 4955 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4956 | // We must have at least one component that refers to the type, and the first |
| 4957 | // one is known to be a field designator. Verify that the ArgTy represents |
| 4958 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4959 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4960 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4961 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4962 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 4963 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 4964 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4965 | // Otherwise, create a null pointer as the base, and iteratively process |
| 4966 | // the offsetof designators. |
| 4967 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 4968 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4969 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4970 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4972 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 4973 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4974 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 4975 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 4976 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4977 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 4978 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4979 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4980 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 4981 | bool DidWarnAboutNonPOD = false; |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 4982 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4983 | // FIXME: Dependent case loses a lot of information here. And probably |
| 4984 | // leaks like a sieve. |
| 4985 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 4986 | const OffsetOfComponent &OC = CompPtr[i]; |
| 4987 | if (OC.isBrackets) { |
| 4988 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 4989 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 4990 | if (!AT) { |
| 4991 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4992 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 4993 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
| 4996 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 4997 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 4998 | // Promote the array so it looks more like a normal array subscript |
| 4999 | // expression. |
| 5000 | DefaultFunctionArrayConversion(Res); |
| 5001 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5002 | // C99 6.5.2.1p1 |
| 5003 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5004 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5005 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5006 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5007 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5008 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5009 | |
| 5010 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5011 | OC.LocEnd); |
| 5012 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5013 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5014 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5015 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 5016 | if (!RC) { |
| 5017 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5018 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5019 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5020 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5021 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5022 | // Get the decl corresponding to this. |
| 5023 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5024 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5025 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | f9b8bc6 | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5026 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5027 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5028 | << Res->getType()); |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5029 | DidWarnAboutNonPOD = true; |
| 5030 | } |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5033 | FieldDecl *MemberDecl |
| 5034 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 5035 | LookupMemberName) |
| 5036 | .getAsDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5037 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5038 | if (!MemberDecl) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5039 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 5040 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5041 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5042 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5043 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5044 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5045 | Res = BuildAnonymousStructUnionMemberReference( |
| 5046 | SourceLocation(), MemberDecl, Res, SourceLocation()).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5047 | } else { |
| 5048 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5049 | // doesn't matter here. |
| 5050 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5051 | MemberDecl->getType().getNonReferenceType()); |
| 5052 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5053 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5054 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5055 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5056 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5057 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5061 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5062 | TypeTy *arg1,TypeTy *arg2, |
| 5063 | SourceLocation RPLoc) { |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5064 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 5065 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5066 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5067 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5068 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5069 | if (getLangOptions().CPlusPlus) { |
| 5070 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5071 | << SourceRange(BuiltinLoc, RPLoc); |
| 5072 | return ExprError(); |
| 5073 | } |
| 5074 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5075 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5076 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5079 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5080 | ExprArg cond, |
| 5081 | ExprArg expr1, ExprArg expr2, |
| 5082 | SourceLocation RPLoc) { |
| 5083 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5084 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5085 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5086 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5087 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5088 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5089 | QualType resType; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5090 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5091 | resType = Context.DependentTy; |
| 5092 | } else { |
| 5093 | // The conditional expression is required to be a constant expression. |
| 5094 | llvm::APSInt condEval(32); |
| 5095 | SourceLocation ExpLoc; |
| 5096 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5097 | return ExprError(Diag(ExpLoc, |
| 5098 | diag::err_typecheck_choose_expr_requires_constant) |
| 5099 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5100 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5101 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5102 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 5103 | } |
| 5104 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5105 | cond.release(); expr1.release(); expr2.release(); |
| 5106 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 5107 | resType, RPLoc)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5110 | //===----------------------------------------------------------------------===// |
| 5111 | // Clang Extensions. |
| 5112 | //===----------------------------------------------------------------------===// |
| 5113 | |
| 5114 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5115 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5116 | // Analyze block parameters. |
| 5117 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5118 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5119 | // Add BSI to CurBlock. |
| 5120 | BSI->PrevBlockInfo = CurBlock; |
| 5121 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5122 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5123 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5124 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5125 | BSI->hasBlockDeclRefExprs = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5126 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5127 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5128 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5129 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5130 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5133 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5134 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5135 | |
| 5136 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5137 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5138 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5139 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5140 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5141 | if (T->isArrayType()) { |
| 5142 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5143 | diag::err_block_returns_array); |
| 5144 | return; |
| 5145 | } |
| 5146 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5147 | // The parameter list is optional, if there was none, assume (). |
| 5148 | if (!T->isFunctionType()) |
| 5149 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 5150 | |
| 5151 | CurBlock->hasPrototype = true; |
| 5152 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5153 | // Check for a valid sentinel attribute on this block. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5154 | if (CurBlock->TheDecl->getAttr<SentinelAttr>(Context)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5155 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5156 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5157 | // FIXME: remove the attribute. |
| 5158 | } |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5159 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
| 5160 | |
| 5161 | // Do not allow returning a objc interface by-value. |
| 5162 | if (RetTy->isObjCInterfaceType()) { |
| 5163 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5164 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5165 | return; |
| 5166 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5167 | return; |
| 5168 | } |
| 5169 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5170 | // Analyze arguments to block. |
| 5171 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 5172 | "Not a function declarator!"); |
| 5173 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5174 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5175 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 5176 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5177 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5178 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 5179 | // no arguments, not a function that takes a single void argument. |
| 5180 | if (FTI.hasPrototype && |
| 5181 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5182 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 5183 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5184 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5185 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5186 | } else if (FTI.hasPrototype) { |
| 5187 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5188 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5189 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5190 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5191 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5192 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 5193 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5194 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5195 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 5196 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 5197 | // If this has an identifier, add it to the scope stack. |
| 5198 | if ((*AI)->getIdentifier()) |
| 5199 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5200 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5201 | // Check for a valid sentinel attribute on this block. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5202 | if (!CurBlock->isVariadic && |
| 5203 | CurBlock->TheDecl->getAttr<SentinelAttr>(Context)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5204 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5205 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5206 | // FIXME: remove the attribute. |
| 5207 | } |
| 5208 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5209 | // Analyze the return type. |
| 5210 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5211 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
| 5212 | |
| 5213 | // Do not allow returning a objc interface by-value. |
| 5214 | if (RetTy->isObjCInterfaceType()) { |
| 5215 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5216 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5217 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5218 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 5222 | /// is invoked to pop the information about the block from the action impl. |
| 5223 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 5224 | // Ensure that CurBlock is deleted. |
| 5225 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5226 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5227 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 5228 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5229 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 5230 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5231 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5232 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 5236 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5237 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 5238 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5239 | // If blocks are disabled, emit an error. |
| 5240 | if (!LangOpts.Blocks) |
| 5241 | Diag(CaretLoc, diag::err_blocks_disable); |
| 5242 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5243 | // Ensure that CurBlock is deleted. |
| 5244 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5245 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5246 | PopDeclContext(); |
| 5247 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5248 | // Pop off CurBlock, handle nested blocks. |
| 5249 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5250 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5251 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5252 | if (!BSI->ReturnType.isNull()) |
| 5253 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5254 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5255 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 5256 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 5257 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5258 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5259 | QualType BlockTy; |
| 5260 | if (!BSI->hasPrototype) |
Eli Friedman | 687abff | 2009-06-08 04:24:21 +0000 | [diff] [blame] | 5261 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5262 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5263 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 5264 | BSI->isVariadic, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5265 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5266 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5267 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5268 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5269 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5270 | // If needed, diagnose invalid gotos and switches in the block. |
| 5271 | if (CurFunctionNeedsScopeChecking) |
| 5272 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 5273 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
| 5274 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5275 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5276 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 5277 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5280 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 5281 | ExprArg expr, TypeTy *type, |
| 5282 | SourceLocation RPLoc) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5283 | QualType T = QualType::getFromOpaquePtr(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5284 | Expr *E = static_cast<Expr*>(expr.get()); |
| 5285 | Expr *OrigExpr = E; |
| 5286 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5287 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5288 | |
| 5289 | // Get the va_list type |
| 5290 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5291 | if (VaListType->isArrayType()) { |
| 5292 | // Deal with implicit array decay; for example, on x86-64, |
| 5293 | // va_list is an array, but it's supposed to decay to |
| 5294 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5295 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5296 | // Make sure the input expression also decays appropriately. |
| 5297 | UsualUnaryConversions(E); |
| 5298 | } else { |
| 5299 | // Otherwise, the va_list argument must be an l-value because |
| 5300 | // it is modified by va_arg. |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5301 | if (!E->isTypeDependent() && |
| 5302 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5303 | return ExprError(); |
| 5304 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5305 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5306 | if (!E->isTypeDependent() && |
| 5307 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5308 | return ExprError(Diag(E->getLocStart(), |
| 5309 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5310 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 5311 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5312 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5313 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5314 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5315 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5316 | expr.release(); |
| 5317 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 5318 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5321 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5322 | // The type of __null will be int or long, depending on the size of |
| 5323 | // pointers on the target. |
| 5324 | QualType Ty; |
| 5325 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 5326 | Ty = Context.IntTy; |
| 5327 | else |
| 5328 | Ty = Context.LongTy; |
| 5329 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5330 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5333 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 5334 | SourceLocation Loc, |
| 5335 | QualType DstType, QualType SrcType, |
| 5336 | Expr *SrcExpr, const char *Flavor) { |
| 5337 | // Decode the result (notice that AST's are still created for extensions). |
| 5338 | bool isInvalid = false; |
| 5339 | unsigned DiagKind; |
| 5340 | switch (ConvTy) { |
| 5341 | default: assert(0 && "Unknown conversion type"); |
| 5342 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5343 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5344 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 5345 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5346 | case IntToPointer: |
| 5347 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 5348 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5349 | case IncompatiblePointer: |
| 5350 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 5351 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5352 | case IncompatiblePointerSign: |
| 5353 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 5354 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5355 | case FunctionVoidPointer: |
| 5356 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 5357 | break; |
| 5358 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 5359 | // If the qualifiers lost were because we were applying the |
| 5360 | // (deprecated) C++ conversion from a string literal to a char* |
| 5361 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 5362 | // Ideally, this check would be performed in |
| 5363 | // CheckPointerTypesForAssignment. However, that would require a |
| 5364 | // bit of refactoring (so that the second argument is an |
| 5365 | // expression, rather than a type), which should be done as part |
| 5366 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 5367 | // C++ semantics. |
| 5368 | if (getLangOptions().CPlusPlus && |
| 5369 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 5370 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5371 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 5372 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5373 | case IntToBlockPointer: |
| 5374 | DiagKind = diag::err_int_to_block_pointer; |
| 5375 | break; |
| 5376 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 5377 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5378 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5379 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5380 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5381 | // it can give a more specific diagnostic. |
| 5382 | DiagKind = diag::warn_incompatible_qualified_id; |
| 5383 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5384 | case IncompatibleVectors: |
| 5385 | DiagKind = diag::warn_incompatible_vectors; |
| 5386 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5387 | case Incompatible: |
| 5388 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 5389 | isInvalid = true; |
| 5390 | break; |
| 5391 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5392 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5393 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 5394 | << SrcExpr->getSourceRange(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5395 | return isInvalid; |
| 5396 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5397 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 5398 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5399 | llvm::APSInt ICEResult; |
| 5400 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 5401 | if (Result) |
| 5402 | *Result = ICEResult; |
| 5403 | return false; |
| 5404 | } |
| 5405 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5406 | Expr::EvalResult EvalResult; |
| 5407 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5408 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5409 | EvalResult.HasSideEffects) { |
| 5410 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 5411 | |
| 5412 | if (EvalResult.Diag) { |
| 5413 | // We only show the note if it's not the usual "invalid subexpression" |
| 5414 | // or if it's actually in a subexpression. |
| 5415 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 5416 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 5417 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 5418 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5419 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5420 | return true; |
| 5421 | } |
| 5422 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5423 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 5424 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5425 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5426 | if (EvalResult.Diag && |
| 5427 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 5428 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5429 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5430 | if (Result) |
| 5431 | *Result = EvalResult.Val.getInt(); |
| 5432 | return false; |
| 5433 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5435 | Sema::ExpressionEvaluationContext |
| 5436 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
| 5437 | // Introduce a new set of potentially referenced declarations to the stack. |
| 5438 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 5439 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
| 5440 | |
| 5441 | std::swap(ExprEvalContext, NewContext); |
| 5442 | return NewContext; |
| 5443 | } |
| 5444 | |
| 5445 | void |
| 5446 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 5447 | ExpressionEvaluationContext NewContext) { |
| 5448 | ExprEvalContext = NewContext; |
| 5449 | |
| 5450 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 5451 | // Mark any remaining declarations in the current position of the stack |
| 5452 | // as "referenced". If they were not meant to be referenced, semantic |
| 5453 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 5454 | PotentiallyReferencedDecls RemainingDecls; |
| 5455 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 5456 | PotentiallyReferencedDeclStack.pop_back(); |
| 5457 | |
| 5458 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 5459 | IEnd = RemainingDecls.end(); |
| 5460 | I != IEnd; ++I) |
| 5461 | MarkDeclarationReferenced(I->first, I->second); |
| 5462 | } |
| 5463 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5464 | |
| 5465 | /// \brief Note that the given declaration was referenced in the source code. |
| 5466 | /// |
| 5467 | /// This routine should be invoke whenever a given declaration is referenced |
| 5468 | /// in the source code, and where that reference occurred. If this declaration |
| 5469 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 5470 | /// C99 6.9p3), then the declaration will be marked as used. |
| 5471 | /// |
| 5472 | /// \param Loc the location where the declaration was referenced. |
| 5473 | /// |
| 5474 | /// \param D the declaration that has been referenced by the source code. |
| 5475 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 5476 | assert(D && "No declaration?"); |
| 5477 | |
| 5478 | // Mark a parameter declaration "used", regardless of whether we're in a |
| 5479 | // template or not. |
| 5480 | if (isa<ParmVarDecl>(D)) |
| 5481 | D->setUsed(true); |
| 5482 | |
| 5483 | // Do not mark anything as "used" within a dependent context; wait for |
| 5484 | // an instantiation. |
| 5485 | if (CurContext->isDependentContext()) |
| 5486 | return; |
| 5487 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5488 | switch (ExprEvalContext) { |
| 5489 | case Unevaluated: |
| 5490 | // We are in an expression that is not potentially evaluated; do nothing. |
| 5491 | return; |
| 5492 | |
| 5493 | case PotentiallyEvaluated: |
| 5494 | // We are in a potentially-evaluated expression, so this declaration is |
| 5495 | // "used"; handle this below. |
| 5496 | break; |
| 5497 | |
| 5498 | case PotentiallyPotentiallyEvaluated: |
| 5499 | // We are in an expression that may be potentially evaluated; queue this |
| 5500 | // declaration reference until we know whether the expression is |
| 5501 | // potentially evaluated. |
| 5502 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 5503 | return; |
| 5504 | } |
| 5505 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5506 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 5507 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 5508 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 5509 | if (!Constructor->isUsed()) |
| 5510 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5511 | } |
| 5512 | // FIXME: more checking for other implicits go here. |
| 5513 | else |
| 5514 | Constructor->setUsed(true); |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 5515 | return; |
| 5516 | } |
| 5517 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5518 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 5519 | // FIXME: implicit template instantiation |
| 5520 | // FIXME: keep track of references to static functions |
| 5521 | (void)Function; |
| 5522 | Function->setUsed(true); |
| 5523 | return; |
| 5524 | } |
| 5525 | |
| 5526 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 5527 | (void)Var; |
| 5528 | // FIXME: implicit template instantiation |
| 5529 | // FIXME: keep track of references to static data? |
| 5530 | D->setUsed(true); |
| 5531 | } |
| 5532 | } |
| 5533 | |