Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 279272e | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/LiteralSupport.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 24 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Designator.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 30 | /// \brief Determine whether the use of this declaration is valid, and |
| 31 | /// emit any corresponding diagnostics. |
| 32 | /// |
| 33 | /// This routine diagnoses various problems with referencing |
| 34 | /// declarations that can occur when using a declaration. For example, |
| 35 | /// it might warn if a deprecated or unavailable declaration is being |
| 36 | /// used, or produce an error (and return true) if a C++0x deleted |
| 37 | /// function is being used. |
| 38 | /// |
| 39 | /// \returns true if there was an error (this declaration cannot be |
| 40 | /// referenced), false otherwise. |
| 41 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 42 | // See if the decl is deprecated. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 43 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 44 | // Implementing deprecated stuff requires referencing deprecated |
| 45 | // stuff. Don't warn if we are implementing a deprecated |
| 46 | // construct. |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 47 | bool isSilenced = false; |
| 48 | |
| 49 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 50 | // If this reference happens *in* a deprecated function or method, don't |
| 51 | // warn. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 52 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 53 | |
| 54 | // If this is an Objective-C method implementation, check to see if the |
| 55 | // method was deprecated on the declaration, not the definition. |
| 56 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 57 | // The semantic decl context of a ObjCMethodDecl is the |
| 58 | // ObjCImplementationDecl. |
| 59 | if (ObjCImplementationDecl *Impl |
| 60 | = dyn_cast<ObjCImplementationDecl>(MD->getParent())) { |
| 61 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 62 | MD = Impl->getClassInterface()->getMethod(MD->getSelector(), |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 63 | MD->isInstanceMethod()); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 64 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (!isSilenced) |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 70 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 71 | } |
| 72 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 73 | // See if this is a deleted function. |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 74 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 75 | if (FD->isDeleted()) { |
| 76 | Diag(Loc, diag::err_deleted_function_use); |
| 77 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 78 | return true; |
| 79 | } |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 80 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 81 | |
| 82 | // See if the decl is unavailable |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 83 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 84 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 86 | } |
| 87 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 91 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
| 92 | /// (and other functions in future), which have been declared with sentinel |
| 93 | /// attribute. It warns if call does not have the sentinel argument. |
| 94 | /// |
| 95 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
| 96 | Expr **Args, unsigned NumArgs) |
| 97 | { |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 98 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 99 | if (!attr) |
| 100 | return; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 101 | int sentinelPos = attr->getSentinel(); |
| 102 | int nullPos = attr->getNullPos(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 103 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 104 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 105 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 106 | unsigned int i = 0; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 107 | bool warnNotEnoughArgs = false; |
| 108 | int isMethod = 0; |
| 109 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 110 | // skip over named parameters. |
| 111 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 112 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 113 | if (nullPos) |
| 114 | --nullPos; |
| 115 | else |
| 116 | ++i; |
| 117 | } |
| 118 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 119 | isMethod = 1; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 120 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 121 | // skip over named parameters. |
| 122 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 123 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 124 | if (nullPos) |
| 125 | --nullPos; |
| 126 | else |
| 127 | ++i; |
| 128 | } |
| 129 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 130 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 131 | // block or function pointer call. |
| 132 | QualType Ty = V->getType(); |
| 133 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| 134 | const FunctionType *FT = Ty->isFunctionPointerType() |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 135 | ? Ty->getAs<PointerType>()->getPointeeType()->getAsFunctionType() |
| 136 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType(); |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 137 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 138 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 139 | unsigned k; |
| 140 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 141 | if (nullPos) |
| 142 | --nullPos; |
| 143 | else |
| 144 | ++i; |
| 145 | } |
| 146 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 147 | } |
| 148 | if (Ty->isBlockPointerType()) |
| 149 | isMethod = 2; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 150 | } else |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 151 | return; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 152 | } else |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
| 155 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 156 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 157 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | int sentinel = i; |
| 161 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 162 | --sentinelPos; |
| 163 | ++i; |
| 164 | } |
| 165 | if (sentinelPos > 0) { |
| 166 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 167 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | while (i < NumArgs-1) { |
| 171 | ++i; |
| 172 | ++sentinel; |
| 173 | } |
| 174 | Expr *sentinelExpr = Args[sentinel]; |
| 175 | if (sentinelExpr && (!sentinelExpr->getType()->isPointerType() || |
| 176 | !sentinelExpr->isNullPointerConstant(Context))) { |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 177 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 178 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 179 | } |
| 180 | return; |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 183 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 184 | Expr *Ex = (Expr *)E; |
| 185 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 186 | } |
| 187 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // Standard Promotions and Conversions |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 192 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 193 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 194 | QualType Ty = E->getType(); |
| 195 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 196 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 197 | if (Ty->isFunctionType()) |
| 198 | ImpCastExprToType(E, Context.getPointerType(Ty)); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 199 | else if (Ty->isArrayType()) { |
| 200 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 201 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 202 | // type 'array of type' is converted to an expression that has type 'pointer |
| 203 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 204 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 205 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 206 | // |
| 207 | // C++ 4.2p1: |
| 208 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 209 | // T" can be converted to an rvalue of type "pointer to T". |
| 210 | // |
| 211 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 212 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 5c09af0 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 213 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 214 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 219 | /// operators (C99 6.3). The conversions of array and function types are |
| 220 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 221 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 222 | /// In these instances, this routine should *not* be called. |
| 223 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 224 | QualType Ty = Expr->getType(); |
| 225 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 226 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 227 | // C99 6.3.1.1p2: |
| 228 | // |
| 229 | // The following may be used in an expression wherever an int or |
| 230 | // unsigned int may be used: |
| 231 | // - an object or expression with an integer type whose integer |
| 232 | // conversion rank is less than or equal to the rank of int |
| 233 | // and unsigned int. |
| 234 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 235 | // |
| 236 | // If an int can represent all values of the original type, the |
| 237 | // value is converted to an int; otherwise, it is converted to an |
| 238 | // unsigned int. These are called the integer promotions. All |
| 239 | // other types are unchanged by the integer promotions. |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 240 | QualType PTy = Context.isPromotableBitField(Expr); |
| 241 | if (!PTy.isNull()) { |
| 242 | ImpCastExprToType(Expr, PTy); |
| 243 | return Expr; |
| 244 | } |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 245 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | 6ae7d11 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 246 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 247 | ImpCastExprToType(Expr, PT); |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 248 | return Expr; |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 251 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 252 | return Expr; |
| 253 | } |
| 254 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 255 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 256 | /// do not have a prototype. Arguments that have type float are promoted to |
| 257 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 258 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 259 | QualType Ty = Expr->getType(); |
| 260 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 261 | |
| 262 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 263 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 264 | if (BT->getKind() == BuiltinType::Float) |
| 265 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 266 | |
| 267 | UsualUnaryConversions(Expr); |
| 268 | } |
| 269 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 270 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 271 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 272 | /// interfaces passed by value. This returns true if the argument type is |
| 273 | /// completely illegal. |
| 274 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 275 | DefaultArgumentPromotion(Expr); |
| 276 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 277 | if (Expr->getType()->isObjCInterfaceType()) { |
| 278 | Diag(Expr->getLocStart(), |
| 279 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 280 | << Expr->getType() << CT; |
| 281 | return true; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 283 | |
| 284 | if (!Expr->getType()->isPODType()) |
| 285 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 286 | << Expr->getType() << CT; |
| 287 | |
| 288 | return false; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 292 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 293 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 294 | /// routine returns the first non-arithmetic type found. The client is |
| 295 | /// responsible for emitting appropriate error diagnostics. |
| 296 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 297 | /// GCC. |
| 298 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 299 | bool isCompAssign) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 300 | if (!isCompAssign) |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 301 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 302 | |
| 303 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 305 | // For conversion purposes, we ignore any qualifiers. |
| 306 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 307 | QualType lhs = |
| 308 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 309 | QualType rhs = |
| 310 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 311 | |
| 312 | // If both types are identical, no conversion is needed. |
| 313 | if (lhs == rhs) |
| 314 | return lhs; |
| 315 | |
| 316 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 317 | // The caller can deal with this (e.g. pointer + int). |
| 318 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 319 | return lhs; |
| 320 | |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 321 | // Perform bitfield promotions. |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 322 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 323 | if (!LHSBitfieldPromoteTy.isNull()) |
| 324 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 325 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 326 | if (!RHSBitfieldPromoteTy.isNull()) |
| 327 | rhs = RHSBitfieldPromoteTy; |
| 328 | |
Eli Friedman | 6ae7d11 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 329 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 330 | if (!isCompAssign) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 331 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 332 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 333 | return destType; |
| 334 | } |
| 335 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // Semantic Analysis for various Expression Types |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
| 340 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 341 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 342 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 343 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 344 | /// multiple tokens. However, the common case is that StringToks points to one |
| 345 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 346 | /// |
| 347 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 348 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 349 | assert(NumStringToks && "Must have at least one string!"); |
| 350 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 351 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 352 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 353 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 354 | |
| 355 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 356 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 357 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 358 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 359 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 360 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 361 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 362 | |
| 363 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 364 | if (getLangOptions().CPlusPlus) |
| 365 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 366 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 367 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 368 | // the nul terminator character as well as the string length for pascal |
| 369 | // strings. |
| 370 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 371 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 372 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 374 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 375 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 376 | Literal.GetStringLength(), |
| 377 | Literal.AnyWide, StrTy, |
| 378 | &StringTokLocs[0], |
| 379 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 382 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 383 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 384 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 385 | /// for values inside the block or for globals). |
| 386 | /// |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 387 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 388 | /// up-to-date. |
| 389 | /// |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 390 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 391 | ValueDecl *VD) { |
| 392 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 393 | // we wanted to. |
| 394 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 395 | return false; |
| 396 | |
| 397 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 398 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 399 | return false; |
| 400 | |
| 401 | // If this is a reference to an extern, static, or global variable, no need to |
| 402 | // snapshot it. |
| 403 | // FIXME: What about 'const' variables in C++? |
| 404 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 405 | if (!Var->hasLocalStorage()) |
| 406 | return false; |
| 407 | |
| 408 | // Blocks that have these can't be constant. |
| 409 | CurBlock->hasBlockDeclRefExprs = true; |
| 410 | |
| 411 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 412 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 413 | // be defined outside all of the current blocks (in which case the blocks do |
| 414 | // all get the bit). Walk the nesting chain. |
| 415 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 416 | NextBlock = NextBlock->PrevBlockInfo) { |
| 417 | // If we found the defining block for the variable, don't mark the block as |
| 418 | // having a reference outside it. |
| 419 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 420 | break; |
| 421 | |
| 422 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 423 | // a snapshot as well. |
| 424 | NextBlock->hasBlockDeclRefExprs = true; |
| 425 | } |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 426 | |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 432 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 433 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 434 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 435 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 436 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 437 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 438 | IdentifierInfo &II, |
| 439 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 440 | const CXXScopeSpec *SS, |
| 441 | bool isAddressOfOperand) { |
| 442 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 443 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 446 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 447 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 448 | /// nested-name-specifier. |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 449 | Sema::OwningExprResult |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 450 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 451 | bool TypeDependent, bool ValueDependent, |
| 452 | const CXXScopeSpec *SS) { |
Anders Carlsson | 9bd4866 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 453 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 454 | Diag(Loc, |
| 455 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 456 | << D->getDeclName(); |
| 457 | return ExprError(); |
| 458 | } |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 459 | |
| 460 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 461 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 462 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 463 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
| 464 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 465 | << D->getIdentifier() << FD->getDeclName(); |
| 466 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
| 467 | << D->getIdentifier(); |
| 468 | return ExprError(); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 474 | MarkDeclarationReferenced(Loc, D); |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 475 | |
| 476 | Expr *E; |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 477 | if (SS && !SS->isEmpty()) { |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 478 | E = new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
| 479 | ValueDependent, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 480 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 481 | } else |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 482 | E = new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
| 483 | |
| 484 | return Owned(E); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 487 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 488 | /// variable corresponding to the anonymous union or struct whose type |
| 489 | /// is Record. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 490 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 491 | RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 492 | assert(Record->isAnonymousStructOrUnion() && |
| 493 | "Record must be an anonymous struct or union!"); |
| 494 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 495 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 496 | // operation rather than a slow walk through DeclContext's vector (which |
| 497 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 498 | DeclContext *Ctx = Record->getDeclContext(); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 499 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 500 | DEnd = Ctx->decls_end(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 501 | D != DEnd; ++D) { |
| 502 | if (*D == Record) { |
| 503 | // The object for the anonymous struct/union directly |
| 504 | // follows its type in the list of declarations. |
| 505 | ++D; |
| 506 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 507 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 508 | return *D; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | assert(false && "Missing object for anonymous record"); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 516 | /// \brief Given a field that represents a member of an anonymous |
| 517 | /// struct/union, build the path from that field's context to the |
| 518 | /// actual member. |
| 519 | /// |
| 520 | /// Construct the sequence of field member references we'll have to |
| 521 | /// perform to get to the field in the anonymous union/struct. The |
| 522 | /// list of members is built from the field outward, so traverse it |
| 523 | /// backwards to go from an object in the current context to the field |
| 524 | /// we found. |
| 525 | /// |
| 526 | /// \returns The variable from which the field access should begin, |
| 527 | /// for an anonymous struct/union that is not a member of another |
| 528 | /// class. Otherwise, returns NULL. |
| 529 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 530 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 531 | assert(Field->getDeclContext()->isRecord() && |
| 532 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 533 | && "Field must be stored inside an anonymous struct or union"); |
| 534 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 535 | Path.push_back(Field); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 536 | VarDecl *BaseObject = 0; |
| 537 | DeclContext *Ctx = Field->getDeclContext(); |
| 538 | do { |
| 539 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 540 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 541 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 542 | Path.push_back(AnonField); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 543 | else { |
| 544 | BaseObject = cast<VarDecl>(AnonObject); |
| 545 | break; |
| 546 | } |
| 547 | Ctx = Ctx->getParent(); |
| 548 | } while (Ctx->isRecord() && |
| 549 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 550 | |
| 551 | return BaseObject; |
| 552 | } |
| 553 | |
| 554 | Sema::OwningExprResult |
| 555 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 556 | FieldDecl *Field, |
| 557 | Expr *BaseObjectExpr, |
| 558 | SourceLocation OpLoc) { |
| 559 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 560 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
| 561 | AnonFields); |
| 562 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 563 | // Build the expression that refers to the base object, from |
| 564 | // which we will build a sequence of member references to each |
| 565 | // of the anonymous union objects and, eventually, the field we |
| 566 | // found via name lookup. |
| 567 | bool BaseObjectIsPointer = false; |
| 568 | unsigned ExtraQuals = 0; |
| 569 | if (BaseObject) { |
| 570 | // BaseObject is an anonymous struct/union variable (and is, |
| 571 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 572 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 573 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 574 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 575 | SourceLocation()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 576 | ExtraQuals |
| 577 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 578 | } else if (BaseObjectExpr) { |
| 579 | // The caller provided the base object expression. Determine |
| 580 | // whether its a pointer and whether it adds any qualifiers to the |
| 581 | // anonymous struct/union fields we're looking into. |
| 582 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 583 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 584 | BaseObjectIsPointer = true; |
| 585 | ObjectType = ObjectPtr->getPointeeType(); |
| 586 | } |
| 587 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 588 | } else { |
| 589 | // We've found a member of an anonymous struct/union that is |
| 590 | // inside a non-anonymous struct/union, so in a well-formed |
| 591 | // program our base object expression is "this". |
| 592 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 593 | if (!MD->isStatic()) { |
| 594 | QualType AnonFieldType |
| 595 | = Context.getTagDeclType( |
| 596 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 597 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 598 | if ((Context.getCanonicalType(AnonFieldType) |
| 599 | == Context.getCanonicalType(ThisType)) || |
| 600 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 601 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 602 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 603 | MD->getThisType(Context)); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 604 | BaseObjectIsPointer = true; |
| 605 | } |
| 606 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 607 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 608 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 609 | } |
| 610 | ExtraQuals = MD->getTypeQualifiers(); |
| 611 | } |
| 612 | |
| 613 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 614 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 615 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // Build the implicit member references to the field of the |
| 619 | // anonymous struct/union. |
| 620 | Expr *Result = BaseObjectExpr; |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 621 | unsigned BaseAddrSpace = BaseObjectExpr->getType().getAddressSpace(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 622 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 623 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 624 | FI != FIEnd; ++FI) { |
| 625 | QualType MemberType = (*FI)->getType(); |
| 626 | if (!(*FI)->isMutable()) { |
| 627 | unsigned combinedQualifiers |
| 628 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 629 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 630 | } |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 631 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 632 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 633 | MarkDeclarationReferenced(Loc, *FI); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 634 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 635 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 636 | BaseObjectIsPointer = false; |
| 637 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 640 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 643 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 644 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 645 | /// performs lookup on that name and returns an expression that refers |
| 646 | /// to that name. This routine isn't directly called from the parser, |
| 647 | /// because the parser doesn't know about DeclarationName. Rather, |
| 648 | /// this routine is called by ActOnIdentifierExpr, |
| 649 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 650 | /// which form the DeclarationName from the corresponding syntactic |
| 651 | /// forms. |
| 652 | /// |
| 653 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 654 | /// function call context. LookupCtx is only used for a C++ |
| 655 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 656 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 657 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 658 | /// isAddressOfOperand means that this expression is the direct operand |
| 659 | /// of an address-of operator. This matters because this is the only |
| 660 | /// situation where a qualified name referencing a non-static member may |
| 661 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 662 | Sema::OwningExprResult |
| 663 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 664 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 665 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 666 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 667 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 668 | if (SS && SS->isInvalid()) |
| 669 | return ExprError(); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 670 | |
| 671 | // C++ [temp.dep.expr]p3: |
| 672 | // An id-expression is type-dependent if it contains: |
| 673 | // -- a nested-name-specifier that contains a class-name that |
| 674 | // names a dependent type. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 675 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 676 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 677 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
| 678 | Loc, SS->getRange(), |
Anders Carlsson | 4e8d569 | 2009-07-09 00:05:08 +0000 | [diff] [blame] | 679 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()), |
| 680 | isAddressOfOperand)); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 683 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 684 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 685 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 686 | if (Lookup.isAmbiguous()) { |
| 687 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 688 | SS && SS->isSet() ? SS->getRange() |
| 689 | : SourceRange()); |
| 690 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | NamedDecl *D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 694 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 695 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 696 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 697 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 698 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 699 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 700 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 701 | // found a decl, but that decl is outside the current instance method (i.e. |
| 702 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 703 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 704 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 705 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 706 | ObjCInterfaceDecl *ClassDeclared; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 707 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 708 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 709 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 710 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 711 | |
| 712 | // If we're referencing an invalid decl, just return this as a silent |
| 713 | // error node. The error diagnostic was already emitted on the decl. |
| 714 | if (IV->isInvalidDecl()) |
| 715 | return ExprError(); |
| 716 | |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 717 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 718 | // If a class method attemps to use a free standing ivar, this is |
| 719 | // an error. |
| 720 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 721 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 722 | << IV->getDeclName()); |
| 723 | // If a class method uses a global variable, even if an ivar with |
| 724 | // same name exists, use the global. |
| 725 | if (!IsClsMethod) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 726 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 727 | ClassDeclared != IFace) |
| 728 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 729 | // FIXME: This should use a new expr for a direct reference, don't |
| 730 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 731 | IdentifierInfo &II = Context.Idents.get("self"); |
Argiris Kirtzidis | 3bb4904 | 2009-07-18 08:49:37 +0000 | [diff] [blame] | 732 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, SourceLocation(), |
| 733 | II, false); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 734 | MarkDeclarationReferenced(Loc, IV); |
Daniel Dunbar | f5254bd | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 735 | return Owned(new (Context) |
| 736 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 737 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 739 | } |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 740 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 741 | // We should warn if a local variable hides an ivar. |
| 742 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 743 | ObjCInterfaceDecl *ClassDeclared; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 744 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 745 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 746 | IFace == ClassDeclared) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 747 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 748 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 749 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 750 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 751 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 752 | QualType T; |
| 753 | |
| 754 | if (getCurMethodDecl()->isInstanceMethod()) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 755 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 756 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 757 | else |
| 758 | T = Context.getObjCClassType(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 759 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 760 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 761 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 763 | // Determine whether this name might be a candidate for |
| 764 | // argument-dependent lookup. |
| 765 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 766 | HasTrailingLParen; |
| 767 | |
| 768 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 769 | // We've seen something of the form |
| 770 | // |
| 771 | // identifier( |
| 772 | // |
| 773 | // and we did not find any entity by the name |
| 774 | // "identifier". However, this identifier is still subject to |
| 775 | // argument-dependent lookup, so keep track of the name. |
| 776 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 777 | Context.OverloadTy, |
| 778 | Loc)); |
| 779 | } |
| 780 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 781 | if (D == 0) { |
| 782 | // Otherwise, this could be an implicitly declared function reference (legal |
| 783 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 784 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 785 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 786 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 787 | else { |
| 788 | // If this name wasn't predeclared and if this is not a function call, |
| 789 | // diagnose the problem. |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 790 | if (SS && !SS->isEmpty()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 791 | return ExprError(Diag(Loc, diag::err_typecheck_no_member) |
| 792 | << Name << SS->getRange()); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 793 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 794 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 795 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 796 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 797 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 798 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 801 | |
| 802 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 803 | // Warn about constructs like: |
| 804 | // if (void *X = foo()) { ... } else { X }. |
| 805 | // In the else block, the pointer is always false. |
| 806 | |
| 807 | // FIXME: In a template instantiation, we don't have scope |
| 808 | // information to check this property. |
| 809 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 810 | Scope *CheckS = S; |
| 811 | while (CheckS) { |
| 812 | if (CheckS->isWithinElse() && |
| 813 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
| 814 | if (Var->getType()->isBooleanType()) |
| 815 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 816 | << Var->getDeclName()); |
| 817 | else |
| 818 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 819 | << Var->getDeclName()); |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | // Move up one more control parent to check again. |
| 824 | CheckS = CheckS->getControlParent(); |
| 825 | if (CheckS) |
| 826 | CheckS = CheckS->getParent(); |
| 827 | } |
| 828 | } |
| 829 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 830 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 831 | // C99 DR 316 says that, if a function type comes from a |
| 832 | // function definition (without a prototype), that type is only |
| 833 | // used for checking compatibility. Therefore, when referencing |
| 834 | // the function, we pretend that we don't have the full function |
| 835 | // type. |
| 836 | if (DiagnoseUseOfDecl(Func, Loc)) |
| 837 | return ExprError(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 839 | QualType T = Func->getType(); |
| 840 | QualType NoProtoType = T; |
| 841 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 842 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
| 843 | return BuildDeclRefExpr(Func, NoProtoType, Loc, false, false, SS); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return BuildDeclarationNameExpr(Loc, D, HasTrailingLParen, SS, isAddressOfOperand); |
| 848 | } |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 849 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 850 | bool |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 851 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 852 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
| 853 | if (CXXRecordDecl *RD = |
| 854 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
| 855 | QualType DestType = |
| 856 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 857 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 858 | return false; |
| 859 | QualType FromRecordType = From->getType(); |
| 860 | QualType DestRecordType = DestType; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 861 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 862 | DestType = Context.getPointerType(DestType); |
| 863 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 864 | } |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 865 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 866 | CheckDerivedToBaseConversion(FromRecordType, |
| 867 | DestRecordType, |
| 868 | From->getSourceRange().getBegin(), |
| 869 | From->getSourceRange())) |
| 870 | return true; |
Anders Carlsson | 8518694 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 871 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 872 | /*isLvalue=*/true); |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 873 | } |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 874 | return false; |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 875 | } |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 876 | |
| 877 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 878 | Sema::OwningExprResult |
| 879 | Sema::BuildDeclarationNameExpr(SourceLocation Loc, NamedDecl *D, |
| 880 | bool HasTrailingLParen, |
| 881 | const CXXScopeSpec *SS, |
| 882 | bool isAddressOfOperand) { |
| 883 | assert(D && "Cannot refer to a NULL declaration"); |
| 884 | DeclarationName Name = D->getDeclName(); |
| 885 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 886 | // If this is an expression of the form &Class::member, don't build an |
| 887 | // implicit member ref, because we want a pointer to the member in general, |
| 888 | // not any specific instance's member. |
| 889 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 890 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 891 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 892 | QualType DType; |
| 893 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 894 | DType = FD->getType().getNonReferenceType(); |
| 895 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 896 | DType = Method->getType(); |
| 897 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 898 | DType = Context.OverloadTy; |
| 899 | } |
| 900 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 901 | if (!DType.isNull()) { |
| 902 | // The pointer is type- and value-dependent if it points into something |
| 903 | // dependent. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 904 | bool Dependent = DC->isDependentContext(); |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 905 | return BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 910 | // We may have found a field within an anonymous union or struct |
| 911 | // (C++ [class.union]). |
| 912 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 913 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 914 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 916 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 917 | if (!MD->isStatic()) { |
| 918 | // C++ [class.mfct.nonstatic]p2: |
| 919 | // [...] if name lookup (3.4.1) resolves the name in the |
| 920 | // id-expression to a nonstatic nontype member of class X or of |
| 921 | // a base class of X, the id-expression is transformed into a |
| 922 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 923 | // as the postfix-expression to the left of the '.' operator. |
| 924 | DeclContext *Ctx = 0; |
| 925 | QualType MemberType; |
| 926 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 927 | Ctx = FD->getDeclContext(); |
| 928 | MemberType = FD->getType(); |
| 929 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 930 | if (const ReferenceType *RefType = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 931 | MemberType = RefType->getPointeeType(); |
| 932 | else if (!FD->isMutable()) { |
| 933 | unsigned combinedQualifiers |
| 934 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 935 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 936 | } |
| 937 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 938 | if (!Method->isStatic()) { |
| 939 | Ctx = Method->getParent(); |
| 940 | MemberType = Method->getType(); |
| 941 | } |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 942 | } else if (FunctionTemplateDecl *FunTmpl |
| 943 | = dyn_cast<FunctionTemplateDecl>(D)) { |
| 944 | if (CXXMethodDecl *Method |
| 945 | = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())) { |
| 946 | if (!Method->isStatic()) { |
| 947 | Ctx = Method->getParent(); |
| 948 | MemberType = Context.OverloadTy; |
| 949 | } |
| 950 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 951 | } else if (OverloadedFunctionDecl *Ovl |
| 952 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 953 | // FIXME: We need an abstraction for iterating over one or more function |
| 954 | // templates or functions. This code is far too repetitive! |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 955 | for (OverloadedFunctionDecl::function_iterator |
| 956 | Func = Ovl->function_begin(), |
| 957 | FuncEnd = Ovl->function_end(); |
| 958 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 959 | CXXMethodDecl *DMethod = 0; |
| 960 | if (FunctionTemplateDecl *FunTmpl |
| 961 | = dyn_cast<FunctionTemplateDecl>(*Func)) |
| 962 | DMethod = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 963 | else |
| 964 | DMethod = dyn_cast<CXXMethodDecl>(*Func); |
| 965 | |
| 966 | if (DMethod && !DMethod->isStatic()) { |
| 967 | Ctx = DMethod->getDeclContext(); |
| 968 | MemberType = Context.OverloadTy; |
| 969 | break; |
| 970 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 973 | |
| 974 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 975 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 976 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 977 | if ((Context.getCanonicalType(CtxType) |
| 978 | == Context.getCanonicalType(ThisType)) || |
| 979 | IsDerivedFrom(ThisType, CtxType)) { |
| 980 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 981 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 982 | MD->getThisType(Context)); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 983 | MarkDeclarationReferenced(Loc, D); |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 984 | if (PerformObjectMemberConversion(This, D)) |
| 985 | return ExprError(); |
Anders Carlsson | 9fbe687 | 2009-08-08 16:55:18 +0000 | [diff] [blame] | 986 | if (DiagnoseUseOfDecl(D, Loc)) |
| 987 | return ExprError(); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 988 | return Owned(new (Context) MemberExpr(This, true, D, |
Eli Friedman | 1653e23 | 2009-04-29 17:56:47 +0000 | [diff] [blame] | 989 | Loc, MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 995 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 996 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 997 | if (MD->isStatic()) |
| 998 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 999 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 1000 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1003 | // Any other ways we could have found the field in a well-formed |
| 1004 | // program would have been turned into implicit member expressions |
| 1005 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1006 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1007 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1008 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1010 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1011 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1012 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1013 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1014 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1015 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1016 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1017 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1018 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1019 | return BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 1020 | false, false, SS); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1021 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1022 | return BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 1023 | false, false, SS); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1024 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1026 | // Check whether this declaration can be used. Note that we suppress |
| 1027 | // this check when we're going to perform argument-dependent lookup |
| 1028 | // on this function name, because this might not be the function |
| 1029 | // that overload resolution actually selects. |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1030 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 1031 | HasTrailingLParen; |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1032 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 1033 | return ExprError(); |
| 1034 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1035 | // Only create DeclRefExpr's for valid Decl's. |
| 1036 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1037 | return ExprError(); |
| 1038 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1039 | // If the identifier reference is inside a block, and it refers to a value |
| 1040 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1041 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1042 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1043 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1044 | // We do not do this for things like enum constants, global variables, etc, |
| 1045 | // as they do not get snapshotted. |
| 1046 | // |
| 1047 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1048 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1049 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1050 | // The BlocksAttr indicates the variable is bound by-reference. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1051 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1052 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1053 | // This is to record that a 'const' was actually synthesize and added. |
| 1054 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1055 | // Variable will be bound by-copy, make it const within the closure. |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1056 | |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1057 | ExprTy.addConst(); |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1058 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
| 1059 | constAdded)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1060 | } |
| 1061 | // If this reference is not in a block or if the referenced variable is |
| 1062 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1063 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1064 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1065 | bool ValueDependent = false; |
| 1066 | if (getLangOptions().CPlusPlus) { |
| 1067 | // C++ [temp.dep.expr]p3: |
| 1068 | // An id-expression is type-dependent if it contains: |
| 1069 | // - an identifier that was declared with a dependent type, |
| 1070 | if (VD->getType()->isDependentType()) |
| 1071 | TypeDependent = true; |
| 1072 | // - FIXME: a template-id that is dependent, |
| 1073 | // - a conversion-function-id that specifies a dependent type, |
| 1074 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1075 | Name.getCXXNameType()->isDependentType()) |
| 1076 | TypeDependent = true; |
| 1077 | // - a nested-name-specifier that contains a class-name that |
| 1078 | // names a dependent type. |
| 1079 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1080 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1081 | DC; DC = DC->getParent()) { |
| 1082 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1083 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1084 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1085 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1086 | TypeDependent = true; |
| 1087 | break; |
| 1088 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1093 | // C++ [temp.dep.constexpr]p2: |
| 1094 | // |
| 1095 | // An identifier is value-dependent if it is: |
| 1096 | // - a name declared with a dependent type, |
| 1097 | if (TypeDependent) |
| 1098 | ValueDependent = true; |
| 1099 | // - the name of a non-type template parameter, |
| 1100 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1101 | ValueDependent = true; |
| 1102 | // - a constant with integral or enumeration type and is |
| 1103 | // initialized with an expression that is value-dependent |
Eli Friedman | 1f7744a | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1104 | else if (const VarDecl *Dcl = dyn_cast<VarDecl>(VD)) { |
| 1105 | if (Dcl->getType().getCVRQualifiers() == QualType::Const && |
| 1106 | Dcl->getInit()) { |
| 1107 | ValueDependent = Dcl->getInit()->isValueDependent(); |
| 1108 | } |
| 1109 | } |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1110 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1111 | |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1112 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1113 | TypeDependent, ValueDependent, SS); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1116 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1117 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1118 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1119 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1121 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1122 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1123 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1124 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1125 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1127 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1128 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1129 | unsigned Length; |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1130 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 1131 | Length = FD->getIdentifier()->getLength(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1132 | else if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 1133 | Length = MD->getSynthesizedMethodSize(); |
| 1134 | else { |
| 1135 | Diag(Loc, diag::ext_predef_outside_function); |
| 1136 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 1137 | Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0; |
| 1138 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1141 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1142 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1143 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1144 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1147 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1148 | llvm::SmallString<16> CharBuffer; |
| 1149 | CharBuffer.resize(Tok.getLength()); |
| 1150 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1151 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1153 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1154 | Tok.getLocation(), PP); |
| 1155 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1156 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1157 | |
| 1158 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1159 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1160 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1161 | Literal.isWide(), |
| 1162 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1165 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1166 | // Fast path for a single digit (which is quite common). A single digit |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1167 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1168 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1169 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1170 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1171 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1172 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1173 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1175 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1176 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1177 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1178 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1179 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1180 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1181 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1183 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1184 | Tok.getLocation(), PP); |
| 1185 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1186 | return ExprError(); |
| 1187 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1188 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1190 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1191 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1192 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1193 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1194 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1195 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1196 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1197 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1198 | |
| 1199 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1200 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1201 | // isExact will be set by GetFloatValue(). |
| 1202 | bool isExact = false; |
Chris Lattner | ff1bf1a | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1203 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1204 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1205 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1206 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1207 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1208 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1209 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1210 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1211 | // long long is a C99 feature. |
| 1212 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1213 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1214 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1215 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1216 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1217 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1219 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1220 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1221 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1222 | Ty = Context.UnsignedLongLongTy; |
| 1223 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1224 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1225 | } else { |
| 1226 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1227 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1229 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1230 | // be an unsigned int. |
| 1231 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1232 | |
| 1233 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1234 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1235 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1236 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1237 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1239 | // Does it fit in a unsigned int? |
| 1240 | if (ResultVal.isIntN(IntSize)) { |
| 1241 | // Does it fit in a signed int? |
| 1242 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1243 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1244 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1245 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1246 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1247 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1248 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1250 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1251 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1252 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1253 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1254 | // Does it fit in a unsigned long? |
| 1255 | if (ResultVal.isIntN(LongSize)) { |
| 1256 | // Does it fit in a signed long? |
| 1257 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1258 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1259 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1260 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1261 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1262 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1265 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1266 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1267 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1269 | // Does it fit in a unsigned long long? |
| 1270 | if (ResultVal.isIntN(LongLongSize)) { |
| 1271 | // Does it fit in a signed long long? |
| 1272 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1273 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1274 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1275 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1276 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1279 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1280 | // If we still couldn't decide a type, we probably have something that |
| 1281 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1282 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1283 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1284 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1285 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1286 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1288 | if (ResultVal.getBitWidth() != Width) |
| 1289 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1290 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1291 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1292 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1294 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1295 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1296 | Res = new (Context) ImaginaryLiteral(Res, |
| 1297 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1298 | |
| 1299 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1302 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1303 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1304 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1305 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1306 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1310 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1311 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1312 | SourceLocation OpLoc, |
| 1313 | const SourceRange &ExprRange, |
| 1314 | bool isSizeof) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1315 | if (exprType->isDependentType()) |
| 1316 | return false; |
| 1317 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1318 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1319 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1320 | // alignof(function) is allowed as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1321 | if (isSizeof) |
| 1322 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1323 | return false; |
| 1324 | } |
| 1325 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1326 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1327 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1328 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1329 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1330 | return false; |
| 1331 | } |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1333 | if (RequireCompleteType(OpLoc, exprType, |
| 1334 | isSizeof ? diag::err_sizeof_incomplete_type : |
| 1335 | diag::err_alignof_incomplete_type, |
| 1336 | ExprRange)) |
| 1337 | return true; |
| 1338 | |
| 1339 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | bf2b095 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1340 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1341 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1342 | << exprType << isSizeof << ExprRange; |
| 1343 | return true; |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1346 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1349 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1350 | const SourceRange &ExprRange) { |
| 1351 | E = E->IgnoreParens(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1353 | // alignof decl is always ok. |
| 1354 | if (isa<DeclRefExpr>(E)) |
| 1355 | return false; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Cannot know anything else if the expression is dependent. |
| 1358 | if (E->isTypeDependent()) |
| 1359 | return false; |
| 1360 | |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1361 | if (E->getBitField()) { |
| 1362 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1363 | return true; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1364 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Alignment of a field access is always okay, so long as it isn't a |
| 1367 | // bit-field. |
| 1368 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 6eeaa78 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1369 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1370 | return false; |
| 1371 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1372 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1373 | } |
| 1374 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1375 | /// \brief Build a sizeof or alignof expression given a type operand. |
| 1376 | Action::OwningExprResult |
| 1377 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
| 1378 | bool isSizeOf, SourceRange R) { |
| 1379 | if (T.isNull()) |
| 1380 | return ExprError(); |
| 1381 | |
| 1382 | if (!T->isDependentType() && |
| 1383 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1384 | return ExprError(); |
| 1385 | |
| 1386 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1387 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1388 | Context.getSizeType(), OpLoc, |
| 1389 | R.getEnd())); |
| 1390 | } |
| 1391 | |
| 1392 | /// \brief Build a sizeof or alignof expression given an expression |
| 1393 | /// operand. |
| 1394 | Action::OwningExprResult |
| 1395 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1396 | bool isSizeOf, SourceRange R) { |
| 1397 | // Verify that the operand is valid. |
| 1398 | bool isInvalid = false; |
| 1399 | if (E->isTypeDependent()) { |
| 1400 | // Delay type-checking for type-dependent expressions. |
| 1401 | } else if (!isSizeOf) { |
| 1402 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1403 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1404 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1405 | isInvalid = true; |
| 1406 | } else { |
| 1407 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1408 | } |
| 1409 | |
| 1410 | if (isInvalid) |
| 1411 | return ExprError(); |
| 1412 | |
| 1413 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1414 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1415 | Context.getSizeType(), OpLoc, |
| 1416 | R.getEnd())); |
| 1417 | } |
| 1418 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1419 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1420 | /// the same for @c alignof and @c __alignof |
| 1421 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1422 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1423 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1424 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1425 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1426 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1427 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1428 | if (isType) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1429 | // FIXME: Preserve type source info. |
| 1430 | QualType ArgTy = GetTypeFromParser(TyOrEx); |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1431 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
| 1432 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1433 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1434 | // Get the end location. |
| 1435 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1436 | Action::OwningExprResult Result |
| 1437 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1438 | |
| 1439 | if (Result.isInvalid()) |
| 1440 | DeleteExpr(ArgEx); |
| 1441 | |
| 1442 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1445 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1446 | if (V->isTypeDependent()) |
| 1447 | return Context.DependentTy; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1449 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1450 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1451 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Otherwise they pass through real integer and floating point types here. |
| 1454 | if (V->getType()->isArithmeticType()) |
| 1455 | return V->getType(); |
| 1456 | |
| 1457 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1458 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1459 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1460 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1464 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1465 | Action::OwningExprResult |
| 1466 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1467 | tok::TokenKind Kind, ExprArg Input) { |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1468 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1469 | Input = MaybeConvertParenListExprToParenExpr(S, move(Input)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1470 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1472 | UnaryOperator::Opcode Opc; |
| 1473 | switch (Kind) { |
| 1474 | default: assert(0 && "Unknown unary op!"); |
| 1475 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1476 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1477 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1478 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1479 | if (getLangOptions().CPlusPlus && |
| 1480 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1481 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1482 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1483 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1484 | |
| 1485 | // C++ [over.inc]p1: |
| 1486 | // |
| 1487 | // [...] If the function is a member function with one |
| 1488 | // parameter (which shall be of type int) or a non-member |
| 1489 | // function with two parameters (the second of which shall be |
| 1490 | // of type int), it defines the postfix increment operator ++ |
| 1491 | // for objects of that type. When the postfix increment is |
| 1492 | // called as a result of using the ++ operator, the int |
| 1493 | // argument will have value zero. |
| 1494 | Expr *Args[2] = { |
| 1495 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1496 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1497 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1498 | }; |
| 1499 | |
| 1500 | // Build the candidate set for overloading |
| 1501 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1502 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1503 | |
| 1504 | // Perform overload resolution. |
| 1505 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1506 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1507 | case OR_Success: { |
| 1508 | // We found a built-in operator or an overloaded operator. |
| 1509 | FunctionDecl *FnDecl = Best->Function; |
| 1510 | |
| 1511 | if (FnDecl) { |
| 1512 | // We matched an overloaded operator. Build a call to that |
| 1513 | // operator. |
| 1514 | |
| 1515 | // Convert the arguments. |
| 1516 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1517 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1518 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1519 | } else { |
| 1520 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1521 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1522 | FnDecl->getParamDecl(0)->getType(), |
| 1523 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1524 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1528 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1529 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1530 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1531 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1532 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1533 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 6d8e573 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1534 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1535 | UsualUnaryConversions(FnExpr); |
| 1536 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1537 | Input.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1538 | Args[0] = Arg; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1539 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
| 1540 | Args, 2, ResultTy, |
| 1541 | OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | // We matched a built-in operator. Convert the arguments, then |
| 1544 | // break out so that we will build the appropriate built-in |
| 1545 | // operator node. |
| 1546 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1547 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1548 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1549 | |
| 1550 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1551 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | case OR_No_Viable_Function: |
| 1555 | // No viable function; fall through to handling this as a |
| 1556 | // built-in operator, which will produce an error message for us. |
| 1557 | break; |
| 1558 | |
| 1559 | case OR_Ambiguous: |
| 1560 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1561 | << UnaryOperator::getOpcodeStr(Opc) |
| 1562 | << Arg->getSourceRange(); |
| 1563 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1564 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1565 | |
| 1566 | case OR_Deleted: |
| 1567 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1568 | << Best->Function->isDeleted() |
| 1569 | << UnaryOperator::getOpcodeStr(Opc) |
| 1570 | << Arg->getSourceRange(); |
| 1571 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1572 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | // Either we found no viable overloaded operator or we matched a |
| 1576 | // built-in operator. In either case, fall through to trying to |
| 1577 | // build a built-in operation. |
| 1578 | } |
| 1579 | |
Eli Friedman | 94d3095 | 2009-07-22 23:24:42 +0000 | [diff] [blame] | 1580 | Input.release(); |
| 1581 | Input = Arg; |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 1582 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(Input)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1585 | Action::OwningExprResult |
| 1586 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1587 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1588 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1589 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1590 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1591 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1592 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1593 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1594 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | de72f3e | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1595 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1596 | Base.release(); |
| 1597 | Idx.release(); |
| 1598 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1599 | Context.DependentTy, RLoc)); |
| 1600 | } |
| 1601 | |
| 1602 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1603 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1604 | LHSExp->getType()->isEnumeralType() || |
| 1605 | RHSExp->getType()->isRecordType() || |
| 1606 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1607 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1608 | // to the candidate set. |
| 1609 | OverloadCandidateSet CandidateSet; |
| 1610 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1611 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1612 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1614 | // Perform overload resolution. |
| 1615 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1616 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1617 | case OR_Success: { |
| 1618 | // We found a built-in operator or an overloaded operator. |
| 1619 | FunctionDecl *FnDecl = Best->Function; |
| 1620 | |
| 1621 | if (FnDecl) { |
| 1622 | // We matched an overloaded operator. Build a call to that |
| 1623 | // operator. |
| 1624 | |
| 1625 | // Convert the arguments. |
| 1626 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1627 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1628 | PerformCopyInitialization(RHSExp, |
| 1629 | FnDecl->getParamDecl(0)->getType(), |
| 1630 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1631 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1632 | } else { |
| 1633 | // Convert the arguments. |
| 1634 | if (PerformCopyInitialization(LHSExp, |
| 1635 | FnDecl->getParamDecl(0)->getType(), |
| 1636 | "passing") || |
| 1637 | PerformCopyInitialization(RHSExp, |
| 1638 | FnDecl->getParamDecl(1)->getType(), |
| 1639 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1640 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1644 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1645 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1646 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1648 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1649 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1650 | SourceLocation()); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1651 | UsualUnaryConversions(FnExpr); |
| 1652 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1653 | Base.release(); |
| 1654 | Idx.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1655 | Args[0] = LHSExp; |
| 1656 | Args[1] = RHSExp; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1657 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 1658 | FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1659 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1660 | } else { |
| 1661 | // We matched a built-in operator. Convert the arguments, then |
| 1662 | // break out so that we will build the appropriate built-in |
| 1663 | // operator node. |
| 1664 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1665 | "passing") || |
| 1666 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1667 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1668 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1669 | |
| 1670 | break; |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | case OR_No_Viable_Function: |
| 1675 | // No viable function; fall through to handling this as a |
| 1676 | // built-in operator, which will produce an error message for us. |
| 1677 | break; |
| 1678 | |
| 1679 | case OR_Ambiguous: |
| 1680 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1681 | << "[]" |
| 1682 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1683 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1684 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1685 | |
| 1686 | case OR_Deleted: |
| 1687 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1688 | << Best->Function->isDeleted() |
| 1689 | << "[]" |
| 1690 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1691 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1692 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | // Either we found no viable overloaded operator or we matched a |
| 1696 | // built-in operator. In either case, fall through to trying to |
| 1697 | // build a built-in operation. |
| 1698 | } |
| 1699 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1700 | // Perform default conversions. |
| 1701 | DefaultFunctionArrayConversion(LHSExp); |
| 1702 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1704 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1705 | |
| 1706 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1707 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1708 | // in the subscript position. As a result, we need to derive the array base |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1709 | // and index from the expression types. |
| 1710 | Expr *BaseExpr, *IndexExpr; |
| 1711 | QualType ResultType; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1712 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1713 | BaseExpr = LHSExp; |
| 1714 | IndexExpr = RHSExp; |
| 1715 | ResultType = Context.DependentTy; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1716 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1717 | BaseExpr = LHSExp; |
| 1718 | IndexExpr = RHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1719 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1720 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1721 | // Handle the uncommon case of "123[Ptr]". |
| 1722 | BaseExpr = RHSExp; |
| 1723 | IndexExpr = LHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1724 | ResultType = PTy->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1725 | } else if (const ObjCObjectPointerType *PTy = |
| 1726 | LHSTy->getAsObjCObjectPointerType()) { |
| 1727 | BaseExpr = LHSExp; |
| 1728 | IndexExpr = RHSExp; |
| 1729 | ResultType = PTy->getPointeeType(); |
| 1730 | } else if (const ObjCObjectPointerType *PTy = |
| 1731 | RHSTy->getAsObjCObjectPointerType()) { |
| 1732 | // Handle the uncommon case of "123[Ptr]". |
| 1733 | BaseExpr = RHSExp; |
| 1734 | IndexExpr = LHSExp; |
| 1735 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1736 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1737 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1738 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1740 | // FIXME: need to deal with const... |
| 1741 | ResultType = VTy->getElementType(); |
Eli Friedman | d461407 | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1742 | } else if (LHSTy->isArrayType()) { |
| 1743 | // If we see an array that wasn't promoted by |
| 1744 | // DefaultFunctionArrayConversion, it must be an array that |
| 1745 | // wasn't promoted because of the C90 rule that doesn't |
| 1746 | // allow promoting non-lvalue arrays. Warn, then |
| 1747 | // force the promotion here. |
| 1748 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1749 | LHSExp->getSourceRange(); |
| 1750 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy)); |
| 1751 | LHSTy = LHSExp->getType(); |
| 1752 | |
| 1753 | BaseExpr = LHSExp; |
| 1754 | IndexExpr = RHSExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1755 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | d461407 | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1756 | } else if (RHSTy->isArrayType()) { |
| 1757 | // Same as previous, except for 123[f().a] case |
| 1758 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1759 | RHSExp->getSourceRange(); |
| 1760 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy)); |
| 1761 | RHSTy = RHSExp->getType(); |
| 1762 | |
| 1763 | BaseExpr = RHSExp; |
| 1764 | IndexExpr = LHSExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1765 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1766 | } else { |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1767 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1768 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1769 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1770 | // C99 6.5.2.1p1 |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1771 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1772 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1773 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1774 | << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1776 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| 1777 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1778 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| 1779 | // incomplete types are not object types. |
| 1780 | if (ResultType->isFunctionType()) { |
| 1781 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1782 | << ResultType << BaseExpr->getSourceRange(); |
| 1783 | return ExprError(); |
| 1784 | } |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1785 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1786 | if (!ResultType->isDependentType() && |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1787 | RequireCompleteType(LLoc, ResultType, diag::err_subscript_incomplete_type, |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1788 | BaseExpr->getSourceRange())) |
| 1789 | return ExprError(); |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Diagnose bad cases where we step over interface counts. |
| 1792 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1793 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1794 | << ResultType << BaseExpr->getSourceRange(); |
| 1795 | return ExprError(); |
| 1796 | } |
| 1797 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1798 | Base.release(); |
| 1799 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1800 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1801 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1804 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1805 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1806 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1807 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1808 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1809 | // The vector accessor can't exceed the number of elements. |
| 1810 | const char *compStr = CompName.getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1811 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1812 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1813 | // special names that indicate a subset of exactly half the elements are |
| 1814 | // to be selected. |
| 1815 | bool HalvingSwizzle = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1816 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1817 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1818 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | e2ed6f7 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 1819 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Check that we've found one of the special components, or that the component |
| 1822 | // names must come from the same set. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1823 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1824 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1825 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1826 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1827 | do |
| 1828 | compStr++; |
| 1829 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1830 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1831 | do |
| 1832 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1833 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1834 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1835 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1836 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1837 | // We didn't get to the end of the string. This means the component names |
| 1838 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1839 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1840 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1841 | return QualType(); |
| 1842 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1843 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1844 | // Ensure no component accessor exceeds the width of the vector type it |
| 1845 | // operates on. |
| 1846 | if (!HalvingSwizzle) { |
| 1847 | compStr = CompName.getName(); |
| 1848 | |
| 1849 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1850 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1851 | |
| 1852 | while (*compStr) { |
| 1853 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1854 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1855 | << baseType << SourceRange(CompLoc); |
| 1856 | return QualType(); |
| 1857 | } |
| 1858 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1859 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1860 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1861 | // If this is a halving swizzle, verify that the base type has an even |
| 1862 | // number of elements. |
| 1863 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1864 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1865 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1866 | return QualType(); |
| 1867 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1868 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1869 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1870 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1871 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1872 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1873 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1874 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
| 1875 | : CompName.getLength(); |
| 1876 | if (HexSwizzle) |
| 1877 | CompSize--; |
| 1878 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1879 | if (CompSize == 1) |
| 1880 | return vecType->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1881 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1882 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1883 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1884 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1885 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1886 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1887 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1888 | } |
| 1889 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1892 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 1893 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1894 | const Selector &Sel, |
| 1895 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1896 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1897 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(&Member)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1898 | return PD; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1899 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1900 | return OMD; |
| 1901 | |
| 1902 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1903 | E = PDecl->protocol_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1904 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
| 1905 | Context)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1906 | return D; |
| 1907 | } |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1911 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1912 | IdentifierInfo &Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1913 | const Selector &Sel, |
| 1914 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1915 | // Check protocols on qualified interfaces. |
| 1916 | Decl *GDecl = 0; |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1917 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1918 | E = QIdTy->qual_end(); I != E; ++I) { |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1919 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1920 | GDecl = PD; |
| 1921 | break; |
| 1922 | } |
| 1923 | // Also must look for a getter name which uses property syntax. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1924 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1925 | GDecl = OMD; |
| 1926 | break; |
| 1927 | } |
| 1928 | } |
| 1929 | if (!GDecl) { |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1930 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1931 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1932 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1933 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1934 | if (GDecl) |
| 1935 | return GDecl; |
| 1936 | } |
| 1937 | } |
| 1938 | return GDecl; |
| 1939 | } |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1940 | |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1941 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 1942 | /// all base class implementations. |
| 1943 | /// |
| 1944 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 1945 | const ObjCInterfaceDecl *IFace, |
| 1946 | const Selector &Sel) { |
| 1947 | ObjCMethodDecl *Method = 0; |
Argiris Kirtzidis | b1c4ee5 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1948 | if (ObjCImplementationDecl *ImpDecl = IFace->getImplementation()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1949 | Method = ImpDecl->getInstanceMethod(Sel); |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1950 | |
| 1951 | if (!Method && IFace->getSuperClass()) |
| 1952 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 1953 | return Method; |
| 1954 | } |
| 1955 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1956 | Action::OwningExprResult |
| 1957 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 1958 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Fariborz Jahanian | 0cc2ac1 | 2009-03-04 22:30:12 +0000 | [diff] [blame] | 1959 | IdentifierInfo &Member, |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1960 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS) { |
| 1961 | // FIXME: handle the CXXScopeSpec for proper lookup of qualified-ids |
| 1962 | if (SS && SS->isInvalid()) |
| 1963 | return ExprError(); |
| 1964 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1965 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1966 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1967 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1968 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1969 | assert(BaseExpr && "no record expression"); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1970 | |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1971 | // Perform default conversions. |
| 1972 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1973 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1974 | QualType BaseType = BaseExpr->getType(); |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1975 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 1976 | // use that. |
| 1977 | if (BaseType->isObjCIdType()) { |
| 1978 | // We have an 'id' type. Rather than fall through, we check if this |
| 1979 | // is a reference to 'isa'. |
| 1980 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 1981 | BaseType = Context.ObjCIdRedefinitionType; |
| 1982 | ImpCastExprToType(BaseExpr, BaseType); |
| 1983 | } |
| 1984 | } else if (BaseType->isObjCClassType() && |
| 1985 | BaseType != Context.ObjCClassRedefinitionType) { |
| 1986 | BaseType = Context.ObjCClassRedefinitionType; |
| 1987 | ImpCastExprToType(BaseExpr, BaseType); |
| 1988 | } |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1989 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1991 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1992 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1993 | if (OpKind == tok::arrow) { |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 1994 | if (BaseType->isDependentType()) |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 1995 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 1996 | BaseExpr, true, |
| 1997 | OpLoc, |
| 1998 | DeclarationName(&Member), |
| 1999 | MemberLoc)); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2000 | else if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2001 | BaseType = PT->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2002 | else if (BaseType->isObjCObjectPointerType()) |
| 2003 | ; |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2004 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2005 | return ExprError(Diag(MemberLoc, |
| 2006 | diag::err_typecheck_member_reference_arrow) |
| 2007 | << BaseType << BaseExpr->getSourceRange()); |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2008 | } else { |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2009 | if (BaseType->isDependentType()) { |
| 2010 | // Require that the base type isn't a pointer type |
| 2011 | // (so we'll report an error for) |
| 2012 | // T* t; |
| 2013 | // t.f; |
| 2014 | // |
| 2015 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2016 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2017 | // allows this, while still reporting an error if T is a struct pointer. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2018 | const PointerType *PT = BaseType->getAs<PointerType>(); |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (!PT || (getLangOptions().ObjC1 && |
| 2021 | !PT->getPointeeType()->isRecordType())) |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2022 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2023 | BaseExpr, false, |
| 2024 | OpLoc, |
| 2025 | DeclarationName(&Member), |
| 2026 | MemberLoc)); |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2027 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2028 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2029 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2030 | // Handle field access to simple records. This also handles access to fields |
| 2031 | // of the ObjC 'id' struct. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2032 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2033 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2034 | if (RequireCompleteType(OpLoc, BaseType, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2035 | diag::err_typecheck_incomplete_tag, |
| 2036 | BaseExpr->getSourceRange())) |
| 2037 | return ExprError(); |
| 2038 | |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2039 | DeclContext *DC = RDecl; |
| 2040 | if (SS && SS->isSet()) { |
| 2041 | // If the member name was a qualified-id, look into the |
| 2042 | // nested-name-specifier. |
| 2043 | DC = computeDeclContext(*SS, false); |
| 2044 | |
| 2045 | // FIXME: If DC is not computable, we should build a |
| 2046 | // CXXUnresolvedMemberExpr. |
| 2047 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2048 | } |
| 2049 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2050 | // The record definition is complete, now make sure the member is valid. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2051 | LookupResult Result |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2052 | = LookupQualifiedName(DC, DeclarationName(&Member), |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 2053 | LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2054 | |
Douglas Gregor | 12431cb | 2009-08-06 05:28:30 +0000 | [diff] [blame] | 2055 | if (SS && SS->isSet()) { |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2056 | QualType BaseTypeCanon |
| 2057 | = Context.getCanonicalType(BaseType).getUnqualifiedType(); |
| 2058 | QualType MemberTypeCanon |
| 2059 | = Context.getCanonicalType( |
| 2060 | Context.getTypeDeclType( |
| 2061 | dyn_cast<TypeDecl>(Result.getAsDecl()->getDeclContext()))); |
| 2062 | |
| 2063 | if (BaseTypeCanon != MemberTypeCanon && |
| 2064 | !IsDerivedFrom(BaseTypeCanon, MemberTypeCanon)) |
| 2065 | return ExprError(Diag(SS->getBeginLoc(), |
| 2066 | diag::err_not_direct_base_or_virtual) |
| 2067 | << MemberTypeCanon << BaseTypeCanon); |
| 2068 | } |
| 2069 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2070 | if (!Result) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2071 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member) |
| 2072 | << &Member << BaseExpr->getSourceRange()); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2073 | if (Result.isAmbiguous()) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2074 | DiagnoseAmbiguousLookup(Result, DeclarationName(&Member), |
| 2075 | MemberLoc, BaseExpr->getSourceRange()); |
| 2076 | return ExprError(); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2080 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2081 | // If the decl being referenced had an error, return an error for this |
| 2082 | // sub-expr without emitting another error, in order to avoid cascading |
| 2083 | // error cases. |
| 2084 | if (MemberDecl->isInvalidDecl()) |
| 2085 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2087 | // Check the use of this field |
| 2088 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 2089 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2091 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2092 | // We may have found a field within an anonymous union or struct |
| 2093 | // (C++ [class.union]). |
| 2094 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2095 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2096 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2097 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2098 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2099 | QualType MemberType = FD->getType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2100 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2101 | MemberType = Ref->getPointeeType(); |
| 2102 | else { |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2103 | unsigned BaseAddrSpace = BaseType.getAddressSpace(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2104 | unsigned combinedQualifiers = |
| 2105 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2106 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2107 | combinedQualifiers &= ~QualType::Const; |
| 2108 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2109 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 2110 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2111 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2113 | MarkDeclarationReferenced(MemberLoc, FD); |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2114 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2115 | return ExprError(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2116 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, FD, |
| 2117 | MemberLoc, MemberType)); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2120 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2121 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2122 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2123 | Var, MemberLoc, |
| 2124 | Var->getType().getNonReferenceType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2125 | } |
| 2126 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2127 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2128 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2129 | MemberFn, MemberLoc, |
| 2130 | MemberFn->getType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2131 | } |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2132 | if (FunctionTemplateDecl *FunTmpl |
| 2133 | = dyn_cast<FunctionTemplateDecl>(MemberDecl)) { |
| 2134 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2135 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 2136 | FunTmpl, MemberLoc, |
| 2137 | Context.OverloadTy)); |
| 2138 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2139 | if (OverloadedFunctionDecl *Ovl |
| 2140 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2141 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, Ovl, |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2142 | MemberLoc, Context.OverloadTy)); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2143 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2144 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2145 | return Owned(new (Context) MemberExpr(BaseExpr, OpKind == tok::arrow, |
| 2146 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2147 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2148 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2149 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2150 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2151 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2152 | // We found a declaration kind that we didn't expect. This is a |
| 2153 | // generic error message that tells the user that she can't refer |
| 2154 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2155 | return ExprError(Diag(MemberLoc, |
| 2156 | diag::err_typecheck_member_reference_unknown) |
| 2157 | << DeclarationName(&Member) << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2158 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2159 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2160 | // Handle properties on ObjC 'Class' types. |
Steve Naroff | 7982a64 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 2161 | if (OpKind == tok::period && BaseType->isObjCClassType()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2162 | // Also must look for a getter name which uses property syntax. |
| 2163 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2164 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2165 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2166 | ObjCMethodDecl *Getter; |
| 2167 | // FIXME: need to also look locally in the implementation. |
| 2168 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2169 | // Check the use of this method. |
| 2170 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2171 | return ExprError(); |
| 2172 | } |
| 2173 | // If we found a getter then this may be a valid dot-reference, we |
| 2174 | // will look for the matching setter, in case it is needed. |
| 2175 | Selector SetterSel = |
| 2176 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2177 | PP.getSelectorTable(), &Member); |
| 2178 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2179 | if (!Setter) { |
| 2180 | // If this reference is in an @implementation, also check for 'private' |
| 2181 | // methods. |
| 2182 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
| 2183 | } |
| 2184 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2185 | if (!Setter) |
| 2186 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2187 | |
| 2188 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2189 | return ExprError(); |
| 2190 | |
| 2191 | if (Getter || Setter) { |
| 2192 | QualType PType; |
| 2193 | |
| 2194 | if (Getter) |
| 2195 | PType = Getter->getResultType(); |
Fariborz Jahanian | 1c4da45 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2196 | else |
| 2197 | // Get the expression type from Setter's incoming parameter. |
| 2198 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2199 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 128cdc5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2200 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2201 | Setter, MemberLoc, BaseExpr)); |
| 2202 | } |
| 2203 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2204 | << &Member << BaseType); |
| 2205 | } |
| 2206 | } |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2207 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2208 | // (*Obj).ivar. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2209 | if ((OpKind == tok::arrow && BaseType->isObjCObjectPointerType()) || |
| 2210 | (OpKind == tok::period && BaseType->isObjCInterfaceType())) { |
| 2211 | const ObjCObjectPointerType *OPT = BaseType->getAsObjCObjectPointerType(); |
| 2212 | const ObjCInterfaceType *IFaceT = |
| 2213 | OPT ? OPT->getInterfaceType() : BaseType->getAsObjCInterfaceType(); |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2214 | if (IFaceT) { |
| 2215 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2216 | ObjCInterfaceDecl *ClassDeclared; |
| 2217 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(&Member, ClassDeclared); |
| 2218 | |
| 2219 | if (IV) { |
| 2220 | // If the decl being referenced had an error, return an error for this |
| 2221 | // sub-expr without emitting another error, in order to avoid cascading |
| 2222 | // error cases. |
| 2223 | if (IV->isInvalidDecl()) |
| 2224 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2225 | |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2226 | // Check whether we can reference this field. |
| 2227 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2228 | return ExprError(); |
| 2229 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2230 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2231 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2232 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2233 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2234 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2235 | // Case of a c-function declared inside an objc implementation. |
| 2236 | // FIXME: For a c-style function nested inside an objc implementation |
| 2237 | // class, there is no implementation context available, so we pass |
| 2238 | // down the context as argument to this routine. Ideally, this context |
| 2239 | // need be passed down in the AST node and somehow calculated from the |
| 2240 | // AST for a function decl. |
| 2241 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
| 2242 | if (ObjCImplementationDecl *IMPD = |
| 2243 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2244 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2245 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2246 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2247 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2248 | } |
| 2249 | |
| 2250 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2251 | if (ClassDeclared != IDecl || |
| 2252 | ClassOfMethodDecl != ClassDeclared) |
| 2253 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 2254 | << IV->getDeclName(); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2255 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2256 | // @protected |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2257 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 2258 | << IV->getDeclName(); |
Steve Naroff | f960657 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2259 | } |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2260 | |
| 2261 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2262 | MemberLoc, BaseExpr, |
| 2263 | OpKind == tok::arrow)); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2264 | } |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2265 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 2266 | << IDecl->getDeclName() << &Member |
| 2267 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2268 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2269 | } |
Steve Naroff | 7bffd37 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2270 | // Handle properties on 'id' and qualified "id". |
| 2271 | if (OpKind == tok::period && (BaseType->isObjCIdType() || |
| 2272 | BaseType->isObjCQualifiedIdType())) { |
| 2273 | const ObjCObjectPointerType *QIdTy = BaseType->getAsObjCObjectPointerType(); |
| 2274 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2275 | // Check protocols on qualified interfaces. |
| 2276 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
| 2277 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2278 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2279 | // Check the use of this declaration |
| 2280 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2281 | return ExprError(); |
| 2282 | |
| 2283 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2284 | MemberLoc, BaseExpr)); |
| 2285 | } |
| 2286 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2287 | // Check the use of this method. |
| 2288 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2289 | return ExprError(); |
| 2290 | |
| 2291 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
| 2292 | OMD->getResultType(), |
| 2293 | OMD, OpLoc, MemberLoc, |
| 2294 | NULL, 0)); |
| 2295 | } |
| 2296 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2297 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2298 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2299 | << &Member << BaseType); |
| 2300 | } |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2301 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2302 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2303 | const ObjCObjectPointerType *OPT; |
| 2304 | if (OpKind == tok::period && |
| 2305 | (OPT = BaseType->getAsObjCInterfacePointerType())) { |
| 2306 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2307 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
| 2308 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2309 | // Search for a declared property first. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2310 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2311 | // Check whether we can reference this property. |
| 2312 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2313 | return ExprError(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2314 | QualType ResTy = PD->getType(); |
| 2315 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2316 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | 80ccaa9 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2317 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2318 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2319 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2320 | MemberLoc, BaseExpr)); |
| 2321 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2322 | // Check protocols on qualified interfaces. |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2323 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2324 | E = OPT->qual_end(); I != E; ++I) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2325 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2326 | // Check whether we can reference this property. |
| 2327 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2328 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2329 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2330 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2331 | MemberLoc, BaseExpr)); |
| 2332 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2333 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2334 | E = OPT->qual_end(); I != E; ++I) |
| 2335 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(&Member)) { |
| 2336 | // Check whether we can reference this property. |
| 2337 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2338 | return ExprError(); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2339 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2340 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2341 | MemberLoc, BaseExpr)); |
| 2342 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2343 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2344 | // selector is implemented. |
| 2345 | |
| 2346 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2347 | // shared with the code in ActOnInstanceMessage. |
| 2348 | |
| 2349 | Selector Sel = PP.getSelectorTable().getNullarySelector(&Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2350 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2351 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2352 | // If this reference is in an @implementation, check for 'private' methods. |
| 2353 | if (!Getter) |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2354 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2355 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2356 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2357 | if (!Getter) |
| 2358 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2359 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2360 | // Check if we can reference this property. |
| 2361 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2362 | return ExprError(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2363 | } |
| 2364 | // If we found a getter then this may be a valid dot-reference, we |
| 2365 | // will look for the matching setter, in case it is needed. |
| 2366 | Selector SetterSel = |
| 2367 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2368 | PP.getSelectorTable(), &Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2369 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2370 | if (!Setter) { |
| 2371 | // If this reference is in an @implementation, also check for 'private' |
| 2372 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2373 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2374 | } |
| 2375 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2376 | if (!Setter) |
| 2377 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2378 | |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2379 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2380 | return ExprError(); |
| 2381 | |
| 2382 | if (Getter || Setter) { |
| 2383 | QualType PType; |
| 2384 | |
| 2385 | if (Getter) |
| 2386 | PType = Getter->getResultType(); |
Fariborz Jahanian | 1c4da45 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2387 | else |
| 2388 | // Get the expression type from Setter's incoming parameter. |
| 2389 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2390 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 128cdc5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2391 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2392 | Setter, MemberLoc, BaseExpr)); |
| 2393 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2394 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2395 | << &Member << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2396 | } |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2397 | |
Steve Naroff | 29d293b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2398 | // Handle the following exceptional case (*Obj).isa. |
| 2399 | if (OpKind == tok::period && |
| 2400 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Steve Naroff | bbe4f96 | 2009-07-29 14:06:03 +0000 | [diff] [blame] | 2401 | Member.isStr("isa")) |
Steve Naroff | 29d293b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2402 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
| 2403 | Context.getObjCIdType())); |
| 2404 | |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2405 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2406 | if (BaseType->isExtVectorType()) { |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2407 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2408 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2409 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2410 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, Member, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2411 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2412 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2414 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2415 | << BaseType << BaseExpr->getSourceRange(); |
| 2416 | |
| 2417 | // If the user is trying to apply -> or . to a function or function |
| 2418 | // pointer, it's probably because they forgot parentheses to call |
| 2419 | // the function. Suggest the addition of those parentheses. |
| 2420 | if (BaseType == Context.OverloadTy || |
| 2421 | BaseType->isFunctionType() || |
| 2422 | (BaseType->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2423 | BaseType->getAs<PointerType>()->isFunctionType())) { |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2424 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2425 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2426 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2427 | } |
| 2428 | |
| 2429 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2432 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 2433 | FunctionDecl *FD, |
| 2434 | ParmVarDecl *Param) { |
| 2435 | if (Param->hasUnparsedDefaultArg()) { |
| 2436 | Diag (CallLoc, |
| 2437 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2438 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
| 2439 | Diag(UnparsedDefaultArgLocs[Param], |
| 2440 | diag::note_default_argument_declared_here); |
| 2441 | } else { |
| 2442 | if (Param->hasUninstantiatedDefaultArg()) { |
| 2443 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 2444 | |
| 2445 | // Instantiate the expression. |
| 2446 | const TemplateArgumentList &ArgList = getTemplateInstantiationArgs(FD); |
| 2447 | |
| 2448 | // FIXME: We should really make a new InstantiatingTemplate ctor |
| 2449 | // that has a better message - right now we're just piggy-backing |
| 2450 | // off the "default template argument" error message. |
| 2451 | InstantiatingTemplate Inst(*this, CallLoc, FD->getPrimaryTemplate(), |
| 2452 | ArgList.getFlatArgumentList(), |
| 2453 | ArgList.flat_size()); |
| 2454 | |
| 2455 | OwningExprResult Result = InstantiateExpr(UninstExpr, ArgList); |
| 2456 | if (Result.isInvalid()) |
| 2457 | return ExprError(); |
| 2458 | |
| 2459 | if (SetParamDefaultArgument(Param, move(Result), |
| 2460 | /*FIXME:EqualLoc*/ |
| 2461 | UninstExpr->getSourceRange().getBegin())) |
| 2462 | return ExprError(); |
| 2463 | } |
| 2464 | |
| 2465 | Expr *DefaultExpr = Param->getDefaultArg(); |
| 2466 | |
| 2467 | // If the default expression creates temporaries, we need to |
| 2468 | // push them to the current stack of expression temporaries so they'll |
| 2469 | // be properly destroyed. |
| 2470 | if (CXXExprWithTemporaries *E |
| 2471 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
| 2472 | assert(!E->shouldDestroyTemporaries() && |
| 2473 | "Can't destroy temporaries in a default argument expr!"); |
| 2474 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2475 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | // We already type-checked the argument, so we know it works. |
| 2480 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 2481 | } |
| 2482 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2483 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2484 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2485 | /// function prototype Proto. Call is the call expression itself, and |
| 2486 | /// Fn is the function expression. For a C++ member function, this |
| 2487 | /// routine does not attempt to convert the object argument. Returns |
| 2488 | /// true if the call is ill-formed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2489 | bool |
| 2490 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2491 | FunctionDecl *FDecl, |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2492 | const FunctionProtoType *Proto, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2493 | Expr **Args, unsigned NumArgs, |
| 2494 | SourceLocation RParenLoc) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2495 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2496 | // assignment, to the types of the corresponding parameter, ... |
| 2497 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2498 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2499 | bool Invalid = false; |
| 2500 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2501 | // If too few arguments are available (and we don't have default |
| 2502 | // arguments for the remaining parameters), don't make the call. |
| 2503 | if (NumArgs < NumArgsInProto) { |
| 2504 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2505 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2506 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2507 | // Use default arguments for missing arguments |
| 2508 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2509 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | // If too many are passed and not variadic, error on the extras and drop |
| 2513 | // them. |
| 2514 | if (NumArgs > NumArgsInProto) { |
| 2515 | if (!Proto->isVariadic()) { |
| 2516 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2517 | diag::err_typecheck_call_too_many_args) |
| 2518 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2519 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2520 | Args[NumArgs-1]->getLocEnd()); |
| 2521 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2522 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2523 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2524 | } |
| 2525 | NumArgsToCheck = NumArgsInProto; |
| 2526 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2528 | // Continue to check argument types (even if we have too few/many args). |
| 2529 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2530 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2531 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2532 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2533 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2534 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2535 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2536 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2537 | ProtoArgType, |
| 2538 | diag::err_call_incomplete_argument, |
| 2539 | Arg->getSourceRange())) |
| 2540 | return true; |
| 2541 | |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2542 | // Pass the argument. |
| 2543 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2544 | return true; |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2545 | } else { |
Anders Carlsson | 60eb3be | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 2546 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2547 | |
| 2548 | OwningExprResult ArgExpr = |
| 2549 | BuildCXXDefaultArgExpr(Call->getSourceRange().getBegin(), |
| 2550 | FDecl, Param); |
| 2551 | if (ArgExpr.isInvalid()) |
| 2552 | return true; |
| 2553 | |
| 2554 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2557 | Call->setArg(i, Arg); |
| 2558 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2559 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2560 | // If this is a variadic call, handle args passed through "...". |
| 2561 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2562 | VariadicCallType CallType = VariadicFunction; |
| 2563 | if (Fn->getType()->isBlockPointerType()) |
| 2564 | CallType = VariadicBlock; // Block |
| 2565 | else if (isa<MemberExpr>(Fn)) |
| 2566 | CallType = VariadicMethod; |
| 2567 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2568 | // Promote the arguments (C99 6.5.2.2p7). |
| 2569 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2570 | Expr *Arg = Args[i]; |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2571 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2572 | Call->setArg(i, Arg); |
| 2573 | } |
| 2574 | } |
| 2575 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2576 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2579 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2580 | /// This provides the location of the left/right parens and a list of comma |
| 2581 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2582 | Action::OwningExprResult |
| 2583 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2584 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2585 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2586 | unsigned NumArgs = args.size(); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2589 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
| 2590 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2591 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2592 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2593 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2594 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2595 | NamedDecl *NDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2596 | DeclarationName UnqualifiedName; |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2597 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2598 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2599 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2600 | // in which case we won't do any semantic analysis now. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2601 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2602 | // Fn. |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2603 | bool Dependent = false; |
| 2604 | if (Fn->isTypeDependent()) |
| 2605 | Dependent = true; |
| 2606 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2607 | Dependent = true; |
| 2608 | |
| 2609 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2610 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2611 | Context.DependentTy, RParenLoc)); |
| 2612 | |
| 2613 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2614 | if (Fn->getType()->isRecordType()) |
| 2615 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2616 | CommaLocs, RParenLoc)); |
| 2617 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2618 | // Determine whether this is a call to a member function. |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2619 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) { |
| 2620 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
| 2621 | if (isa<OverloadedFunctionDecl>(MemDecl) || |
| 2622 | isa<CXXMethodDecl>(MemDecl) || |
| 2623 | (isa<FunctionTemplateDecl>(MemDecl) && |
| 2624 | isa<CXXMethodDecl>( |
| 2625 | cast<FunctionTemplateDecl>(MemDecl)->getTemplatedDecl()))) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2626 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2627 | CommaLocs, RParenLoc)); |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2628 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2631 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2632 | // Also, in C++, keep track of whether we should perform argument-dependent |
| 2633 | // lookup and whether there were any explicitly-specified template arguments. |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2634 | Expr *FnExpr = Fn; |
| 2635 | bool ADL = true; |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2636 | bool HasExplicitTemplateArgs = 0; |
| 2637 | const TemplateArgument *ExplicitTemplateArgs = 0; |
| 2638 | unsigned NumExplicitTemplateArgs = 0; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2639 | while (true) { |
| 2640 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2641 | FnExpr = IcExpr->getSubExpr(); |
| 2642 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2643 | // Parentheses around a function disable ADL |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2644 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2645 | ADL = false; |
| 2646 | FnExpr = PExpr->getSubExpr(); |
| 2647 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2648 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2649 | == UnaryOperator::AddrOf) { |
| 2650 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2651 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2652 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2653 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2654 | NDecl = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2655 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2656 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2657 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2658 | UnqualifiedName = DepName->getName(); |
| 2659 | break; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2660 | } else if (TemplateIdRefExpr *TemplateIdRef |
| 2661 | = dyn_cast<TemplateIdRefExpr>(FnExpr)) { |
| 2662 | NDecl = TemplateIdRef->getTemplateName().getAsTemplateDecl(); |
Douglas Gregor | 6631cb4 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2663 | if (!NDecl) |
| 2664 | NDecl = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl(); |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2665 | HasExplicitTemplateArgs = true; |
| 2666 | ExplicitTemplateArgs = TemplateIdRef->getTemplateArgs(); |
| 2667 | NumExplicitTemplateArgs = TemplateIdRef->getNumTemplateArgs(); |
| 2668 | |
| 2669 | // C++ [temp.arg.explicit]p6: |
| 2670 | // [Note: For simple function names, argument dependent lookup (3.4.2) |
| 2671 | // applies even when the function name is not visible within the |
| 2672 | // scope of the call. This is because the call still has the syntactic |
| 2673 | // form of a function call (3.4.1). But when a function template with |
| 2674 | // explicit template arguments is used, the call does not have the |
| 2675 | // correct syntactic form unless there is a function template with |
| 2676 | // that name visible at the point of the call. If no such name is |
| 2677 | // visible, the call is not syntactically well-formed and |
| 2678 | // argument-dependent lookup does not apply. If some such name is |
| 2679 | // visible, argument dependent lookup applies and additional function |
| 2680 | // templates may be found in other namespaces. |
| 2681 | // |
| 2682 | // The summary of this paragraph is that, if we get to this point and the |
| 2683 | // template-id was not a qualified name, then argument-dependent lookup |
| 2684 | // is still possible. |
| 2685 | if (TemplateIdRef->getQualifier()) |
| 2686 | ADL = false; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2687 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2688 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2689 | // Any kind of name that does not refer to a declaration (or |
| 2690 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2691 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2692 | break; |
| 2693 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2694 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2696 | OverloadedFunctionDecl *Ovl = 0; |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2697 | FunctionTemplateDecl *FunctionTemplate = 0; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2698 | if (NDecl) { |
| 2699 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2700 | if ((FunctionTemplate = dyn_cast<FunctionTemplateDecl>(NDecl))) |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2701 | FDecl = FunctionTemplate->getTemplatedDecl(); |
| 2702 | else |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2703 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2704 | Ovl = dyn_cast<OverloadedFunctionDecl>(NDecl); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2705 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2707 | if (Ovl || FunctionTemplate || |
| 2708 | (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2709 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2710 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2711 | ADL = false; |
| 2712 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2713 | // We don't perform ADL in C. |
| 2714 | if (!getLangOptions().CPlusPlus) |
| 2715 | ADL = false; |
| 2716 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2717 | if (Ovl || FunctionTemplate || ADL) { |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2718 | FDecl = ResolveOverloadedCallFn(Fn, NDecl, UnqualifiedName, |
| 2719 | HasExplicitTemplateArgs, |
| 2720 | ExplicitTemplateArgs, |
| 2721 | NumExplicitTemplateArgs, |
| 2722 | LParenLoc, Args, NumArgs, CommaLocs, |
| 2723 | RParenLoc, ADL); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2724 | if (!FDecl) |
| 2725 | return ExprError(); |
| 2726 | |
| 2727 | // Update Fn to refer to the actual function selected. |
| 2728 | Expr *NewFn = 0; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2729 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2730 | = dyn_cast<QualifiedDeclRefExpr>(FnExpr)) |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2731 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2732 | QDRExpr->getLocation(), |
| 2733 | false, false, |
| 2734 | QDRExpr->getQualifierRange(), |
| 2735 | QDRExpr->getQualifier()); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2736 | else |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2737 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2738 | Fn->getSourceRange().getBegin()); |
| 2739 | Fn->Destroy(Context); |
| 2740 | Fn = NewFn; |
| 2741 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2742 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2743 | |
| 2744 | // Promote the function operand. |
| 2745 | UsualUnaryConversions(Fn); |
| 2746 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2747 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2748 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2749 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2750 | Args, NumArgs, |
| 2751 | Context.BoolTy, |
| 2752 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2753 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2754 | const FunctionType *FuncT; |
| 2755 | if (!Fn->getType()->isBlockPointerType()) { |
| 2756 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2757 | // have type pointer to function". |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2758 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2759 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2760 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2761 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2762 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2763 | } else { // This is a block call. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2764 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2765 | getAsFunctionType(); |
| 2766 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2767 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2768 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2769 | << Fn->getType() << Fn->getSourceRange()); |
| 2770 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2771 | // Check for a valid return type |
| 2772 | if (!FuncT->getResultType()->isVoidType() && |
| 2773 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2774 | FuncT->getResultType(), |
| 2775 | diag::err_call_incomplete_return, |
| 2776 | TheCall->getSourceRange())) |
| 2777 | return ExprError(); |
| 2778 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2779 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2780 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2782 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2783 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2784 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2785 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2786 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2787 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2788 | |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2789 | if (FDecl) { |
| 2790 | // Check if we have too few/too many template arguments, based |
| 2791 | // on our knowledge of the function definition. |
| 2792 | const FunctionDecl *Def = 0; |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2793 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | f7ed781 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2794 | const FunctionProtoType *Proto = |
| 2795 | Def->getType()->getAsFunctionProtoType(); |
| 2796 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2797 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2798 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2799 | } |
| 2800 | } |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2803 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2804 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2805 | Expr *Arg = Args[i]; |
| 2806 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2807 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2808 | Arg->getType(), |
| 2809 | diag::err_call_incomplete_argument, |
| 2810 | Arg->getSourceRange())) |
| 2811 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2812 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2813 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2814 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2816 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2817 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2818 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2819 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2820 | |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2821 | // Check for sentinels |
| 2822 | if (NDecl) |
| 2823 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Anders Carlsson | 7fb1380 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2825 | // Do special checking on direct calls to functions. |
Anders Carlsson | 7fb1380 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2826 | if (FDecl) { |
| 2827 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 2828 | return ExprError(); |
| 2829 | |
| 2830 | if (unsigned BuiltinID = FDecl->getBuiltinID(Context)) |
| 2831 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 2832 | } else if (NDecl) { |
| 2833 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 2834 | return ExprError(); |
| 2835 | } |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2836 | |
Anders Carlsson | 54ad8a0 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 2837 | return MaybeBindToTemporary(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2840 | Action::OwningExprResult |
| 2841 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2842 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2843 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 2844 | //FIXME: Preserve type source info. |
| 2845 | QualType literalType = GetTypeFromParser(Ty); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2846 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2847 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2848 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2849 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2850 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2851 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2852 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2853 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | ed71c54 | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2854 | } else if (!literalType->isDependentType() && |
| 2855 | RequireCompleteType(LParenLoc, literalType, |
| 2856 | diag::err_typecheck_decl_incomplete_type, |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2857 | SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2858 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2859 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2860 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2861 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2862 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2863 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 2864 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2865 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2866 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2867 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 2868 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2869 | InitExpr.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2870 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2871 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2874 | Action::OwningExprResult |
| 2875 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2876 | SourceLocation RBraceLoc) { |
| 2877 | unsigned NumInit = initlist.size(); |
| 2878 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2879 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 2880 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2881 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2882 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2883 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2884 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2885 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2886 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2889 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | c358b62 | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 2890 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 2891 | CastExpr::CastKind& Kind, bool FunctionalStyle) { |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2892 | if (getLangOptions().CPlusPlus) |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 2893 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle); |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 2894 | |
Eli Friedman | 01e0f65 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 2895 | DefaultFunctionArrayConversion(castExpr); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2896 | |
| 2897 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 2898 | // type needs to be scalar. |
| 2899 | if (castType->isVoidType()) { |
| 2900 | // Cast to void allows any expr type. |
| 2901 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2902 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 2903 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 2904 | (castType->isStructureType() || castType->isUnionType())) { |
| 2905 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 2906 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2907 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 2908 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | b4671fa | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 2909 | Kind = CastExpr::CK_NoOp; |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2910 | } else if (castType->isUnionType()) { |
| 2911 | // GCC cast to union extension |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2912 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2913 | RecordDecl::field_iterator Field, FieldEnd; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2914 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2915 | Field != FieldEnd; ++Field) { |
| 2916 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 2917 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 2918 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 2919 | << castExpr->getSourceRange(); |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | if (Field == FieldEnd) |
| 2924 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 2925 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | b4671fa | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 2926 | Kind = CastExpr::CK_ToUnion; |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 2927 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2928 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2929 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2930 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2931 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2932 | } else if (!castExpr->getType()->isScalarType() && |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2933 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2934 | return Diag(castExpr->getLocStart(), |
| 2935 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2936 | << castExpr->getType() << castExpr->getSourceRange(); |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 2937 | } else if (castType->isExtVectorType()) { |
| 2938 | if (CheckExtVectorCast(TyR, castType, castExpr->getType())) |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2939 | return true; |
| 2940 | } else if (castType->isVectorType()) { |
| 2941 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 2942 | return true; |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 2943 | } else if (castExpr->getType()->isVectorType()) { |
| 2944 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 2945 | return true; |
Steve Naroff | ff6c802 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2946 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | 49fd7ad | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 2947 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Eli Friedman | 970e56c | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 2948 | } else if (!castType->isArithmeticType()) { |
| 2949 | QualType castExprType = castExpr->getType(); |
| 2950 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 2951 | return Diag(castExpr->getLocStart(), |
| 2952 | diag::err_cast_pointer_from_non_pointer_int) |
| 2953 | << castExprType << castExpr->getSourceRange(); |
| 2954 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 2955 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 2956 | return Diag(castExpr->getLocStart(), |
| 2957 | diag::err_cast_pointer_to_non_pointer_int) |
| 2958 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2959 | } |
Fariborz Jahanian | 4862e87 | 2009-05-22 21:42:52 +0000 | [diff] [blame] | 2960 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 2961 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 2962 | return false; |
| 2963 | } |
| 2964 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 2965 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2966 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2967 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2968 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2969 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2970 | return Diag(R.getBegin(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2971 | Ty->isVectorType() ? |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2972 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2973 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2974 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2975 | } else |
| 2976 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2977 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2978 | << VectorTy << Ty << R; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2979 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 2980 | return false; |
| 2981 | } |
| 2982 | |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 2983 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, QualType SrcTy) { |
| 2984 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
| 2985 | |
Nate Begeman | 9e06370 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 2986 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 2987 | // an ExtVectorType. |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 2988 | if (SrcTy->isVectorType()) { |
| 2989 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 2990 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 2991 | << DestTy << SrcTy << R; |
| 2992 | return false; |
| 2993 | } |
| 2994 | |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 2995 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 2996 | // conversion will take place first from scalar to elt type, and then |
| 2997 | // splat from elt type to vector. |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 2998 | if (SrcTy->isPointerType()) |
| 2999 | return Diag(R.getBegin(), |
| 3000 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3001 | << DestTy << SrcTy << R; |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3002 | return false; |
| 3003 | } |
| 3004 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3005 | Action::OwningExprResult |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3006 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3007 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3008 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
| 3009 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3010 | assert((Ty != 0) && (Op.get() != 0) && |
| 3011 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3012 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3013 | Expr *castExpr = (Expr *)Op.get(); |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3014 | //FIXME: Preserve type source info. |
| 3015 | QualType castType = GetTypeFromParser(Ty); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3016 | |
| 3017 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3018 | if (isa<ParenListExpr>(castExpr)) |
| 3019 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3020 | |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3021 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
| 3022 | Kind)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3023 | return ExprError(); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3024 | |
| 3025 | Op.release(); |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3026 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3027 | Kind, castExpr, castType, |
| 3028 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3031 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3032 | /// of comma binary operators. |
| 3033 | Action::OwningExprResult |
| 3034 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3035 | Expr *expr = EA.takeAs<Expr>(); |
| 3036 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3037 | if (!E) |
| 3038 | return Owned(expr); |
| 3039 | |
| 3040 | OwningExprResult Result(*this, E->getExpr(0)); |
| 3041 | |
| 3042 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3043 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3044 | Owned(E->getExpr(i))); |
| 3045 | |
| 3046 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3047 | } |
| 3048 | |
| 3049 | Action::OwningExprResult |
| 3050 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3051 | SourceLocation RParenLoc, ExprArg Op, |
| 3052 | QualType Ty) { |
| 3053 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
| 3054 | |
| 3055 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
| 3056 | // then handle it as such. |
| 3057 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3058 | if (PE->getNumExprs() == 0) { |
| 3059 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3060 | return ExprError(); |
| 3061 | } |
| 3062 | |
| 3063 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3064 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3065 | initExprs.push_back(PE->getExpr(i)); |
| 3066 | |
| 3067 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3068 | // braces instead of the original commas. |
| 3069 | Op.release(); |
| 3070 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
| 3071 | initExprs.size(), RParenLoc); |
| 3072 | E->setType(Ty); |
| 3073 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
| 3074 | Owned(E)); |
| 3075 | } else { |
| 3076 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 3077 | // sequence of BinOp comma operators. |
| 3078 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3079 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 3084 | SourceLocation R, |
| 3085 | MultiExprArg Val) { |
| 3086 | unsigned nexprs = Val.size(); |
| 3087 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| 3088 | assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); |
| 3089 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| 3090 | return Owned(expr); |
| 3091 | } |
| 3092 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3093 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3094 | /// In that case, lhs = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3095 | /// C99 6.5.15 |
| 3096 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3097 | SourceLocation QuestionLoc) { |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3098 | // C++ is sufficiently different to merit its own checker. |
| 3099 | if (getLangOptions().CPlusPlus) |
| 3100 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3101 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3102 | UsualUnaryConversions(Cond); |
| 3103 | UsualUnaryConversions(LHS); |
| 3104 | UsualUnaryConversions(RHS); |
| 3105 | QualType CondTy = Cond->getType(); |
| 3106 | QualType LHSTy = LHS->getType(); |
| 3107 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3108 | |
| 3109 | // first, check the condition. |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3110 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3111 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3112 | << CondTy; |
| 3113 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3114 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3116 | // Now check the two expressions. |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3117 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3118 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3120 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3121 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3122 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3123 | UsualArithmeticConversions(LHS, RHS); |
| 3124 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3125 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3126 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3127 | // If both operands are the same structure or union type, the result is that |
| 3128 | // type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3129 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3130 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3131 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3132 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3133 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3134 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3135 | // FIXME: Type of conditional expression must be complete in C mode. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3136 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3137 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3138 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3139 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3140 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3141 | if (!LHSTy->isVoidType()) |
| 3142 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3143 | << RHS->getSourceRange(); |
| 3144 | if (!RHSTy->isVoidType()) |
| 3145 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3146 | << LHS->getSourceRange(); |
| 3147 | ImpCastExprToType(LHS, Context.VoidTy); |
| 3148 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3149 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3150 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3151 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3152 | // the type of the other operand." |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3153 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3154 | RHS->isNullPointerConstant(Context)) { |
| 3155 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 3156 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3157 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3158 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3159 | LHS->isNullPointerConstant(Context)) { |
| 3160 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 3161 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3162 | } |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3163 | // Handle things like Class and struct objc_class*. Here we case the result |
| 3164 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 3165 | // redefinition type if an attempt is made to access its fields. |
| 3166 | if (LHSTy->isObjCClassType() && |
| 3167 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3168 | ImpCastExprToType(RHS, LHSTy); |
| 3169 | return LHSTy; |
| 3170 | } |
| 3171 | if (RHSTy->isObjCClassType() && |
| 3172 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3173 | ImpCastExprToType(LHS, RHSTy); |
| 3174 | return RHSTy; |
| 3175 | } |
| 3176 | // And the same for struct objc_object* / id |
| 3177 | if (LHSTy->isObjCIdType() && |
| 3178 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3179 | ImpCastExprToType(RHS, LHSTy); |
| 3180 | return LHSTy; |
| 3181 | } |
| 3182 | if (RHSTy->isObjCIdType() && |
| 3183 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3184 | ImpCastExprToType(LHS, RHSTy); |
| 3185 | return RHSTy; |
| 3186 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3187 | // Handle block pointer types. |
| 3188 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3189 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3190 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3191 | QualType destType = Context.getPointerType(Context.VoidTy); |
| 3192 | ImpCastExprToType(LHS, destType); |
| 3193 | ImpCastExprToType(RHS, destType); |
| 3194 | return destType; |
| 3195 | } |
| 3196 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3197 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3198 | return QualType(); |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3199 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3200 | // We have 2 block pointer types. |
| 3201 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3202 | // Two identical block pointer types are always compatible. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3203 | return LHSTy; |
| 3204 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3205 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3206 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3207 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3208 | |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3209 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3210 | rhptee.getUnqualifiedType())) { |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3211 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3212 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3213 | // In this situation, we assume void* type. No especially good |
| 3214 | // reason, but this is what gcc does, and we do have to pick |
| 3215 | // to get a consistent AST. |
| 3216 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3217 | ImpCastExprToType(LHS, incompatTy); |
| 3218 | ImpCastExprToType(RHS, incompatTy); |
| 3219 | return incompatTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3220 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3221 | // The block pointer types are compatible. |
| 3222 | ImpCastExprToType(LHS, LHSTy); |
| 3223 | ImpCastExprToType(RHS, LHSTy); |
Steve Naroff | 6ba2268 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3224 | return LHSTy; |
| 3225 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3226 | // Check constraints for Objective-C object pointers types. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3227 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3228 | |
| 3229 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3230 | // Two identical object pointer types are always compatible. |
| 3231 | return LHSTy; |
| 3232 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3233 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAsObjCObjectPointerType(); |
| 3234 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAsObjCObjectPointerType(); |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3235 | QualType compositeType = LHSTy; |
| 3236 | |
| 3237 | // If both operands are interfaces and either operand can be |
| 3238 | // assigned to the other, use that type as the composite |
| 3239 | // type. This allows |
| 3240 | // xxx ? (A*) a : (B*) b |
| 3241 | // where B is a subclass of A. |
| 3242 | // |
| 3243 | // Additionally, as for assignment, if either type is 'id' |
| 3244 | // allow silent coercion. Finally, if the types are |
| 3245 | // incompatible then make sure to use 'id' as the composite |
| 3246 | // type so the result is acceptable for sending messages to. |
| 3247 | |
| 3248 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3249 | // It could return the composite type. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3250 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
Fariborz Jahanian | 2e006d2 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3251 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3252 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
Fariborz Jahanian | 2e006d2 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3253 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3254 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 3255 | RHSTy->isObjCQualifiedIdType()) && |
Steve Naroff | 99eb86b | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3256 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3257 | // Need to handle "id<xx>" explicitly. |
| 3258 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3259 | // id. Currently localizing to here until clear this should be |
| 3260 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 3261 | compositeType = Context.getObjCIdType(); |
| 3262 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3263 | compositeType = Context.getObjCIdType(); |
| 3264 | } else { |
| 3265 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3266 | << LHSTy << RHSTy |
| 3267 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3268 | QualType incompatTy = Context.getObjCIdType(); |
| 3269 | ImpCastExprToType(LHS, incompatTy); |
| 3270 | ImpCastExprToType(RHS, incompatTy); |
| 3271 | return incompatTy; |
| 3272 | } |
| 3273 | // The object pointer types are compatible. |
| 3274 | ImpCastExprToType(LHS, compositeType); |
| 3275 | ImpCastExprToType(RHS, compositeType); |
| 3276 | return compositeType; |
| 3277 | } |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3278 | // Check Objective-C object pointer types and 'void *' |
| 3279 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3280 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3281 | QualType rhptee = RHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
| 3282 | QualType destPointee = lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3283 | QualType destType = Context.getPointerType(destPointee); |
| 3284 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3285 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3286 | return destType; |
| 3287 | } |
| 3288 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 3289 | QualType lhptee = LHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3290 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3291 | QualType destPointee = rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3292 | QualType destType = Context.getPointerType(destPointee); |
| 3293 | ImpCastExprToType(RHS, destType); // add qualifiers if necessary |
| 3294 | ImpCastExprToType(LHS, destType); // promote to void* |
| 3295 | return destType; |
| 3296 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3297 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3298 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3299 | // get the "pointed to" types |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3300 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3301 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3302 | |
| 3303 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3304 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3305 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 3306 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3307 | QualType destType = Context.getPointerType(destPointee); |
| 3308 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3309 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3310 | return destType; |
| 3311 | } |
| 3312 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 3313 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3314 | QualType destType = Context.getPointerType(destPointee); |
| 3315 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3316 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3317 | return destType; |
| 3318 | } |
| 3319 | |
| 3320 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3321 | // Two identical pointer types are always compatible. |
| 3322 | return LHSTy; |
| 3323 | } |
| 3324 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3325 | rhptee.getUnqualifiedType())) { |
| 3326 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3327 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3328 | // In this situation, we assume void* type. No especially good |
| 3329 | // reason, but this is what gcc does, and we do have to pick |
| 3330 | // to get a consistent AST. |
| 3331 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3332 | ImpCastExprToType(LHS, incompatTy); |
| 3333 | ImpCastExprToType(RHS, incompatTy); |
| 3334 | return incompatTy; |
| 3335 | } |
| 3336 | // The pointer types are compatible. |
| 3337 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3338 | // differently qualified versions of compatible types, the result type is |
| 3339 | // a pointer to an appropriately qualified version of the *composite* |
| 3340 | // type. |
| 3341 | // FIXME: Need to calculate the composite type. |
| 3342 | // FIXME: Need to add qualifiers |
| 3343 | ImpCastExprToType(LHS, LHSTy); |
| 3344 | ImpCastExprToType(RHS, LHSTy); |
| 3345 | return LHSTy; |
| 3346 | } |
| 3347 | |
| 3348 | // GCC compatibility: soften pointer/integer mismatch. |
| 3349 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3350 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3351 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3352 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 3353 | return RHSTy; |
| 3354 | } |
| 3355 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3356 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3357 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3358 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 3359 | return LHSTy; |
| 3360 | } |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3361 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3362 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3363 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3364 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3365 | return QualType(); |
| 3366 | } |
| 3367 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3368 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3369 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3370 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3371 | SourceLocation ColonLoc, |
| 3372 | ExprArg Cond, ExprArg LHS, |
| 3373 | ExprArg RHS) { |
| 3374 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3375 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3376 | |
| 3377 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3378 | // was the condition. |
| 3379 | bool isLHSNull = LHSExpr == 0; |
| 3380 | if (isLHSNull) |
| 3381 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3382 | |
| 3383 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3384 | RHSExpr, QuestionLoc); |
| 3385 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3386 | return ExprError(); |
| 3387 | |
| 3388 | Cond.release(); |
| 3389 | LHS.release(); |
| 3390 | RHS.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3391 | return Owned(new (Context) ConditionalOperator(CondExpr, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3392 | isLHSNull ? 0 : LHSExpr, |
| 3393 | RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3396 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3397 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3398 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3399 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3400 | // FIXME: add a couple examples in this comment. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3401 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3402 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 3403 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3404 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3405 | if ((lhsType->isObjCClassType() && |
| 3406 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3407 | (rhsType->isObjCClassType() && |
| 3408 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3409 | return Compatible; |
| 3410 | } |
| 3411 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3412 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3413 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 3414 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3415 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3416 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3417 | lhptee = Context.getCanonicalType(lhptee); |
| 3418 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3419 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3420 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3421 | |
| 3422 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3423 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3424 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3425 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3426 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3427 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3428 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3429 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3430 | // incomplete type and the other is a pointer to a qualified or unqualified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3431 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3432 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3433 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3434 | return ConvTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3435 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3436 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3437 | assert(rhptee->isFunctionType()); |
| 3438 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3439 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3440 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3441 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3442 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3443 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3444 | |
| 3445 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3446 | assert(lhptee->isFunctionType()); |
| 3447 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3448 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3449 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3450 | // unqualified versions of compatible types, ... |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3451 | lhptee = lhptee.getUnqualifiedType(); |
| 3452 | rhptee = rhptee.getUnqualifiedType(); |
| 3453 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3454 | // Check if the pointee types are compatible ignoring the sign. |
| 3455 | // We explicitly check for char so that we catch "char" vs |
| 3456 | // "unsigned char" on systems where "char" is unsigned. |
| 3457 | if (lhptee->isCharType()) { |
| 3458 | lhptee = Context.UnsignedCharTy; |
| 3459 | } else if (lhptee->isSignedIntegerType()) { |
| 3460 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 3461 | } |
| 3462 | if (rhptee->isCharType()) { |
| 3463 | rhptee = Context.UnsignedCharTy; |
| 3464 | } else if (rhptee->isSignedIntegerType()) { |
| 3465 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 3466 | } |
| 3467 | if (lhptee == rhptee) { |
| 3468 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3469 | // takes priority over sign incompatibility because the sign |
| 3470 | // warning can be disabled. |
| 3471 | if (ConvTy != Compatible) |
| 3472 | return ConvTy; |
| 3473 | return IncompatiblePointerSign; |
| 3474 | } |
| 3475 | // General pointer incompatibility takes priority over qualifiers. |
| 3476 | return IncompatiblePointer; |
| 3477 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3478 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3481 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3482 | /// block pointer types are compatible or whether a block and normal pointer |
| 3483 | /// are compatible. It is more restrict than comparing two function pointer |
| 3484 | // types. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3485 | Sema::AssignConvertType |
| 3486 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3487 | QualType rhsType) { |
| 3488 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3489 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3490 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3491 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 3492 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3493 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3494 | // make sure we operate on the canonical type |
| 3495 | lhptee = Context.getCanonicalType(lhptee); |
| 3496 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3497 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3498 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3499 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3500 | // For blocks we enforce that qualifiers are identical. |
| 3501 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 3502 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3503 | |
Eli Friedman | b6eed6e | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3504 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3505 | return IncompatibleBlockPointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3506 | return ConvTy; |
| 3507 | } |
| 3508 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3509 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3510 | /// has code to accommodate several GCC extensions when type checking |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3511 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3512 | /// |
| 3513 | /// int a, *pint; |
| 3514 | /// short *pshort; |
| 3515 | /// struct foo *pfoo; |
| 3516 | /// |
| 3517 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3518 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3519 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3520 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3521 | /// |
| 3522 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3523 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3524 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3525 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3526 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3527 | // Get canonical types. We're not formatting these types, just comparing |
| 3528 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3529 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3530 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3531 | |
| 3532 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3533 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3534 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3535 | if ((lhsType->isObjCClassType() && |
| 3536 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3537 | (rhsType->isObjCClassType() && |
| 3538 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3539 | return Compatible; |
| 3540 | } |
| 3541 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3542 | // If the left-hand side is a reference type, then we are in a |
| 3543 | // (rare!) case where we've allowed the use of references in C, |
| 3544 | // e.g., as a parameter type in a built-in function. In this case, |
| 3545 | // just make sure that the type referenced is compatible with the |
| 3546 | // right-hand side type. The caller is responsible for adjusting |
| 3547 | // lhsType so that the resulting expression does not have reference |
| 3548 | // type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3549 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3550 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3551 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3552 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3553 | } |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3554 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 3555 | // to the same ExtVector type. |
| 3556 | if (lhsType->isExtVectorType()) { |
| 3557 | if (rhsType->isExtVectorType()) |
| 3558 | return lhsType == rhsType ? Compatible : Incompatible; |
| 3559 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 3560 | return Compatible; |
| 3561 | } |
| 3562 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3563 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3564 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3565 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3566 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3567 | if (getLangOptions().LaxVectorConversions && |
| 3568 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3569 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3570 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3571 | } |
| 3572 | return Incompatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3573 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3574 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3575 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3576 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3577 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3578 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3579 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3580 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3581 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3582 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3583 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3584 | |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3585 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3586 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3587 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3588 | return Compatible; |
| 3589 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3590 | } |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3591 | if (rhsType->getAs<BlockPointerType>()) { |
| 3592 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3593 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3594 | |
| 3595 | // Treat block pointers as objects. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3596 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3597 | return Compatible; |
| 3598 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3599 | return Incompatible; |
| 3600 | } |
| 3601 | |
| 3602 | if (isa<BlockPointerType>(lhsType)) { |
| 3603 | if (rhsType->isIntegerType()) |
Eli Friedman | c589830 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3604 | return IntToBlockPointer; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3605 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3606 | // Treat block pointers as objects. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3607 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3608 | return Compatible; |
| 3609 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3610 | if (rhsType->isBlockPointerType()) |
| 3611 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3612 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3613 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3614 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3615 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3616 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3617 | return Incompatible; |
| 3618 | } |
| 3619 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3620 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 3621 | if (rhsType->isIntegerType()) |
| 3622 | return IntToPointer; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3623 | |
| 3624 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3625 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3626 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 3627 | return Compatible; |
| 3628 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3629 | } |
| 3630 | if (rhsType->isObjCObjectPointerType()) { |
Steve Naroff | 7bffd37 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3631 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 3632 | return Compatible; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3633 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 3634 | return Compatible; |
Steve Naroff | 99eb86b | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3635 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 3636 | return IncompatibleObjCQualifiedId; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3637 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3638 | } |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3639 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3640 | if (RHSPT->getPointeeType()->isVoidType()) |
| 3641 | return Compatible; |
| 3642 | } |
| 3643 | // Treat block pointers as objects. |
| 3644 | if (rhsType->isBlockPointerType()) |
| 3645 | return Compatible; |
| 3646 | return Incompatible; |
| 3647 | } |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3648 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3649 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3650 | if (lhsType == Context.BoolTy) |
| 3651 | return Compatible; |
| 3652 | |
| 3653 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3654 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3655 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3656 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3657 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3658 | |
| 3659 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3660 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3661 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3662 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3663 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3664 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 3665 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 3666 | if (lhsType == Context.BoolTy) |
| 3667 | return Compatible; |
| 3668 | |
| 3669 | if (lhsType->isIntegerType()) |
| 3670 | return PointerToInt; |
| 3671 | |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3672 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3673 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3674 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3675 | return Compatible; |
| 3676 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3677 | } |
| 3678 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3679 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3680 | return Compatible; |
| 3681 | return Incompatible; |
| 3682 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3683 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3684 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3685 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3686 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3687 | } |
| 3688 | return Incompatible; |
| 3689 | } |
| 3690 | |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3691 | /// \brief Constructs a transparent union from an expression that is |
| 3692 | /// used to initialize the transparent union. |
| 3693 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
| 3694 | QualType UnionType, FieldDecl *Field) { |
| 3695 | // Build an initializer list that designates the appropriate member |
| 3696 | // of the transparent union. |
| 3697 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3698 | &E, 1, |
| 3699 | SourceLocation()); |
| 3700 | Initializer->setType(UnionType); |
| 3701 | Initializer->setInitializedFieldInUnion(Field); |
| 3702 | |
| 3703 | // Build a compound literal constructing a value of the transparent |
| 3704 | // union type from this initializer list. |
| 3705 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3706 | false); |
| 3707 | } |
| 3708 | |
| 3709 | Sema::AssignConvertType |
| 3710 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3711 | QualType FromType = rExpr->getType(); |
| 3712 | |
| 3713 | // If the ArgType is a Union type, we want to handle a potential |
| 3714 | // transparent_union GCC extension. |
| 3715 | const RecordType *UT = ArgType->getAsUnionType(); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3716 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3717 | return Incompatible; |
| 3718 | |
| 3719 | // The field to initialize within the transparent union. |
| 3720 | RecordDecl *UD = UT->getDecl(); |
| 3721 | FieldDecl *InitField = 0; |
| 3722 | // It's compatible if the expression matches any of the fields. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3723 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 3724 | itend = UD->field_end(); |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3725 | it != itend; ++it) { |
| 3726 | if (it->getType()->isPointerType()) { |
| 3727 | // If the transparent union contains a pointer type, we allow: |
| 3728 | // 1) void pointer |
| 3729 | // 2) null pointer constant |
| 3730 | if (FromType->isPointerType()) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3731 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3732 | ImpCastExprToType(rExpr, it->getType()); |
| 3733 | InitField = *it; |
| 3734 | break; |
| 3735 | } |
| 3736 | |
| 3737 | if (rExpr->isNullPointerConstant(Context)) { |
| 3738 | ImpCastExprToType(rExpr, it->getType()); |
| 3739 | InitField = *it; |
| 3740 | break; |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 3745 | == Compatible) { |
| 3746 | InitField = *it; |
| 3747 | break; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | if (!InitField) |
| 3752 | return Incompatible; |
| 3753 | |
| 3754 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 3755 | return Compatible; |
| 3756 | } |
| 3757 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3758 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3759 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3760 | if (getLangOptions().CPlusPlus) { |
| 3761 | if (!lhsType->isRecordType()) { |
| 3762 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3763 | // expression is implicitly converted (C++ 4) to the |
| 3764 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3765 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3766 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3767 | return Incompatible; |
Chris Lattner | 79e9a42 | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 3768 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3772 | // structures. |
| 3773 | } |
| 3774 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3775 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3776 | // a null pointer constant. |
Steve Naroff | d305a86 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 3777 | if ((lhsType->isPointerType() || |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3778 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3779 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3780 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3781 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3782 | return Compatible; |
| 3783 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3784 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3785 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3786 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3787 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3788 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3789 | // |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3790 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3791 | if (!lhsType->isReferenceType()) |
| 3792 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3793 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3794 | Sema::AssignConvertType result = |
| 3795 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3796 | |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3797 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3798 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3799 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3800 | // so that we can use references in built-in functions even in C. |
| 3801 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3802 | // does not have reference type. |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3803 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3804 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3805 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3808 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3809 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3810 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3811 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3812 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3815 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3816 | Expr *&rex) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3817 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3818 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3819 | QualType lhsType = |
| 3820 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3821 | QualType rhsType = |
| 3822 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3823 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3824 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3825 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3826 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3827 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3828 | // Handle the case of a vector & extvector type of the same size and element |
| 3829 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3830 | if (getLangOptions().LaxVectorConversions) { |
| 3831 | // FIXME: Should we warn here? |
| 3832 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3833 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3834 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3835 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3836 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3837 | } |
| 3838 | } |
| 3839 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3840 | |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3841 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 3842 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 3843 | bool swapped = false; |
| 3844 | if (rhsType->isExtVectorType()) { |
| 3845 | swapped = true; |
| 3846 | std::swap(rex, lex); |
| 3847 | std::swap(rhsType, lhsType); |
| 3848 | } |
| 3849 | |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 3850 | // Handle the case of an ext vector and scalar. |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3851 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) { |
| 3852 | QualType EltTy = LV->getElementType(); |
| 3853 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 3854 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 3855 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3856 | if (swapped) std::swap(rex, lex); |
| 3857 | return lhsType; |
| 3858 | } |
| 3859 | } |
| 3860 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 3861 | rhsType->isRealFloatingType()) { |
| 3862 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 3863 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3864 | if (swapped) std::swap(rex, lex); |
| 3865 | return lhsType; |
| 3866 | } |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3869 | |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 3870 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3871 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3872 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3873 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3874 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3877 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3878 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3879 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 3880 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3881 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3882 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3883 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3884 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3885 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3886 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3887 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3891 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3892 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 3893 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3894 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 3895 | return CheckVectorOperands(Loc, lex, rex); |
| 3896 | return InvalidOperands(Loc, lex, rex); |
| 3897 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3898 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3899 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3901 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3902 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3903 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3907 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3908 | { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3909 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3910 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3911 | if (CompLHSTy) *CompLHSTy = compType; |
| 3912 | return compType; |
| 3913 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3914 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3915 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3916 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3917 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3918 | if (lex->getType()->isArithmeticType() && |
| 3919 | rex->getType()->isArithmeticType()) { |
| 3920 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 3921 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3922 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3923 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3924 | // Put any potential pointer into PExp |
| 3925 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3926 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3927 | std::swap(PExp, IExp); |
| 3928 | |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3929 | if (PExp->getType()->isAnyPointerType()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3930 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3931 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 3932 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3933 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3934 | // Check for arithmetic on pointers to incomplete types. |
| 3935 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3936 | if (getLangOptions().CPlusPlus) { |
| 3937 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3938 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3939 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3940 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3941 | |
| 3942 | // GNU extension: arithmetic on pointer to void |
| 3943 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 3944 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3945 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3946 | if (getLangOptions().CPlusPlus) { |
| 3947 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 3948 | << lex->getType() << lex->getSourceRange(); |
| 3949 | return QualType(); |
| 3950 | } |
| 3951 | |
| 3952 | // GNU extension: arithmetic on pointer to function |
| 3953 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 3954 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 3fc227b | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 3955 | } else { |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 3956 | // Check if we require a complete type. |
| 3957 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 3fc227b | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 3958 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 3959 | PExp->getType()->isObjCObjectPointerType()) && |
| 3960 | RequireCompleteType(Loc, PointeeTy, |
| 3961 | diag::err_typecheck_arithmetic_incomplete_type, |
| 3962 | PExp->getSourceRange(), SourceRange(), |
| 3963 | PExp->getType())) |
| 3964 | return QualType(); |
| 3965 | } |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 3966 | // Diagnose bad cases where we step over interface counts. |
| 3967 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 3968 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 3969 | << PointeeTy << PExp->getSourceRange(); |
| 3970 | return QualType(); |
| 3971 | } |
| 3972 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3973 | if (CompLHSTy) { |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3974 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 3975 | if (LHSTy.isNull()) { |
| 3976 | LHSTy = lex->getType(); |
| 3977 | if (LHSTy->isPromotableIntegerType()) |
| 3978 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 3979 | } |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3980 | *CompLHSTy = LHSTy; |
| 3981 | } |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 3982 | return PExp->getType(); |
| 3983 | } |
| 3984 | } |
| 3985 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3986 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3989 | // C99 6.5.6 |
| 3990 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3991 | SourceLocation Loc, QualType* CompLHSTy) { |
| 3992 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 3993 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 3994 | if (CompLHSTy) *CompLHSTy = compType; |
| 3995 | return compType; |
| 3996 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3997 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 3998 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3999 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4000 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4001 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4002 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4003 | if (lex->getType()->isArithmeticType() |
| 4004 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4005 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4006 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4007 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4008 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4009 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4010 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 7982a64 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4011 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4013 | // The LHS must be an completely-defined object type. |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4015 | bool ComplainAboutVoid = false; |
| 4016 | Expr *ComplainAboutFunc = 0; |
| 4017 | if (lpointee->isVoidType()) { |
| 4018 | if (getLangOptions().CPlusPlus) { |
| 4019 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4020 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4021 | return QualType(); |
| 4022 | } |
| 4023 | |
| 4024 | // GNU C extension: arithmetic on pointer to void |
| 4025 | ComplainAboutVoid = true; |
| 4026 | } else if (lpointee->isFunctionType()) { |
| 4027 | if (getLangOptions().CPlusPlus) { |
| 4028 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4029 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4030 | return QualType(); |
| 4031 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4032 | |
| 4033 | // GNU C extension: arithmetic on pointer to function |
| 4034 | ComplainAboutFunc = lex; |
| 4035 | } else if (!lpointee->isDependentType() && |
| 4036 | RequireCompleteType(Loc, lpointee, |
| 4037 | diag::err_typecheck_sub_ptr_object, |
| 4038 | lex->getSourceRange(), |
| 4039 | SourceRange(), |
| 4040 | lex->getType())) |
| 4041 | return QualType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4043 | // Diagnose bad cases where we step over interface counts. |
| 4044 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4045 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4046 | << lpointee << lex->getSourceRange(); |
| 4047 | return QualType(); |
| 4048 | } |
| 4049 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4050 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4051 | if (rex->getType()->isIntegerType()) { |
| 4052 | if (ComplainAboutVoid) |
| 4053 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4054 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4055 | if (ComplainAboutFunc) |
| 4056 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4057 | << ComplainAboutFunc->getType() |
| 4058 | << ComplainAboutFunc->getSourceRange(); |
| 4059 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4060 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4061 | return lex->getType(); |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4062 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4063 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4064 | // Handle pointer-pointer subtractions. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4065 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4066 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4068 | // RHS must be a completely-type object type. |
| 4069 | // Handle the GNU void* extension. |
| 4070 | if (rpointee->isVoidType()) { |
| 4071 | if (getLangOptions().CPlusPlus) { |
| 4072 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4073 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4074 | return QualType(); |
| 4075 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4077 | ComplainAboutVoid = true; |
| 4078 | } else if (rpointee->isFunctionType()) { |
| 4079 | if (getLangOptions().CPlusPlus) { |
| 4080 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4081 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4082 | return QualType(); |
| 4083 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4084 | |
| 4085 | // GNU extension: arithmetic on pointer to function |
| 4086 | if (!ComplainAboutFunc) |
| 4087 | ComplainAboutFunc = rex; |
| 4088 | } else if (!rpointee->isDependentType() && |
| 4089 | RequireCompleteType(Loc, rpointee, |
| 4090 | diag::err_typecheck_sub_ptr_object, |
| 4091 | rex->getSourceRange(), |
| 4092 | SourceRange(), |
| 4093 | rex->getType())) |
| 4094 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4095 | |
Eli Friedman | 143ddc9 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4096 | if (getLangOptions().CPlusPlus) { |
| 4097 | // Pointee types must be the same: C++ [expr.add] |
| 4098 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4099 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4100 | << lex->getType() << rex->getType() |
| 4101 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4102 | return QualType(); |
| 4103 | } |
| 4104 | } else { |
| 4105 | // Pointee types must be compatible C99 6.5.6p3 |
| 4106 | if (!Context.typesAreCompatible( |
| 4107 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4108 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4109 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4110 | << lex->getType() << rex->getType() |
| 4111 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4112 | return QualType(); |
| 4113 | } |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4114 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4115 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4116 | if (ComplainAboutVoid) |
| 4117 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4118 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4119 | if (ComplainAboutFunc) |
| 4120 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4121 | << ComplainAboutFunc->getType() |
| 4122 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4123 | |
| 4124 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4125 | return Context.getPointerDiffType(); |
| 4126 | } |
| 4127 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4128 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4129 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4132 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4133 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4134 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4135 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4136 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4137 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4138 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4139 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4140 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4141 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4142 | if (LHSTy.isNull()) { |
| 4143 | LHSTy = lex->getType(); |
| 4144 | if (LHSTy->isPromotableIntegerType()) |
| 4145 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4146 | } |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4147 | if (!isCompAssign) |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4148 | ImpCastExprToType(lex, LHSTy); |
| 4149 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4150 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4151 | |
Ryan Flynn | f109fff | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4152 | // Sanity-check shift operands |
| 4153 | llvm::APSInt Right; |
| 4154 | // Check right/shifter operand |
| 4155 | if (rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | a5e7693 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4156 | if (Right.isNegative()) |
Ryan Flynn | f109fff | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4157 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4158 | else { |
| 4159 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4160 | Context.getTypeSize(lex->getType())); |
| 4161 | if (Right.uge(LeftBits)) |
| 4162 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4163 | } |
| 4164 | } |
| 4165 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4166 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4167 | return LHSTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4170 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4171 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4172 | unsigned OpaqueOpc, bool isRelational) { |
| 4173 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 4174 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4175 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4176 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4177 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4178 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 4179 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 4180 | UsualArithmeticConversions(lex, rex); |
| 4181 | else { |
| 4182 | UsualUnaryConversions(lex); |
| 4183 | UsualUnaryConversions(rex); |
| 4184 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4185 | QualType lType = lex->getType(); |
| 4186 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4187 | |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4188 | if (!lType->isFloatingType() |
| 4189 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4190 | // For non-floating point types, check for self-comparisons of the form |
| 4191 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4192 | // often indicate logic errors in the program. |
Ted Kremenek | 264b5cb | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 4193 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
| 4194 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4195 | Expr *LHSStripped = lex->IgnoreParens(); |
| 4196 | Expr *RHSStripped = rex->IgnoreParens(); |
| 4197 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 4198 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | f042dc6 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 4199 | if (DRL->getDecl() == DRR->getDecl() && |
| 4200 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4201 | Diag(Loc, diag::warn_selfcomparison); |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4202 | |
| 4203 | if (isa<CastExpr>(LHSStripped)) |
| 4204 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 4205 | if (isa<CastExpr>(RHSStripped)) |
| 4206 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| 4207 | |
| 4208 | // Warn about comparisons against a string constant (unless the other |
| 4209 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4210 | Expr *literalString = 0; |
| 4211 | Expr *literalStringStripped = 0; |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4212 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4213 | !RHSStripped->isNullPointerConstant(Context)) { |
| 4214 | literalString = lex; |
| 4215 | literalStringStripped = LHSStripped; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4216 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 4217 | isa<ObjCEncodeExpr>(RHSStripped)) && |
| 4218 | !LHSStripped->isNullPointerConstant(Context)) { |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4219 | literalString = rex; |
| 4220 | literalStringStripped = RHSStripped; |
| 4221 | } |
| 4222 | |
| 4223 | if (literalString) { |
| 4224 | std::string resultComparison; |
| 4225 | switch (Opc) { |
| 4226 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 4227 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 4228 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 4229 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 4230 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 4231 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 4232 | default: assert(false && "Invalid comparison operator"); |
| 4233 | } |
| 4234 | Diag(Loc, diag::warn_stringcompare) |
| 4235 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 4236 | << literalString->getSourceRange() |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4237 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 4238 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 4239 | "strcmp(") |
| 4240 | << CodeModificationHint::CreateInsertion( |
| 4241 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4242 | resultComparison); |
| 4243 | } |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 4244 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4246 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4247 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4248 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4249 | if (isRelational) { |
| 4250 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4251 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4252 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4253 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4254 | if (lType->isFloatingType()) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4255 | assert(rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4256 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 4257 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4258 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4259 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4260 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4261 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4262 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4263 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 4264 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4265 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4266 | // All of the following pointer related warnings are GCC extensions, except |
| 4267 | // when handling null pointer constants. One day, we can consider making them |
| 4268 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 4269 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4270 | QualType LCanPointeeTy = |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4271 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4272 | QualType RCanPointeeTy = |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4273 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4275 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4276 | if (LCanPointeeTy == RCanPointeeTy) |
| 4277 | return ResultTy; |
| 4278 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4279 | // C++ [expr.rel]p2: |
| 4280 | // [...] Pointer conversions (4.10) and qualification |
| 4281 | // conversions (4.4) are performed on pointer operands (or on |
| 4282 | // a pointer operand and a null pointer constant) to bring |
| 4283 | // them to their composite pointer type. [...] |
| 4284 | // |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4285 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4286 | // comparisons of pointers. |
Douglas Gregor | cf651d2 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 4287 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4288 | if (T.isNull()) { |
| 4289 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4290 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4291 | return QualType(); |
| 4292 | } |
| 4293 | |
| 4294 | ImpCastExprToType(lex, T); |
| 4295 | ImpCastExprToType(rex, T); |
| 4296 | return ResultTy; |
| 4297 | } |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4298 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 4299 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 4300 | RCanPointeeTy.getUnqualifiedType())) { |
| 4301 | // Valid unless a relational comparison of function pointers |
| 4302 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 4303 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 4304 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4305 | } |
| 4306 | } else if (!isRelational && |
| 4307 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 4308 | // Valid unless comparison between non-null pointer and function pointer |
| 4309 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 4310 | && !LHSIsNull && !RHSIsNull) { |
| 4311 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 4312 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4313 | } |
| 4314 | } else { |
| 4315 | // Invalid |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4316 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4317 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4318 | } |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4319 | if (LCanPointeeTy != RCanPointeeTy) |
| 4320 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4321 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4322 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4323 | |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4324 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4325 | // Comparison of pointers with null pointer constants and equality |
| 4326 | // comparisons of member pointers to null pointer constants. |
| 4327 | if (RHSIsNull && |
| 4328 | (lType->isPointerType() || |
| 4329 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 9a38552 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4330 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4331 | return ResultTy; |
| 4332 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4333 | if (LHSIsNull && |
| 4334 | (rType->isPointerType() || |
| 4335 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 9a38552 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4336 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4337 | return ResultTy; |
| 4338 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4339 | |
| 4340 | // Comparison of member pointers. |
| 4341 | if (!isRelational && |
| 4342 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 4343 | // C++ [expr.eq]p2: |
| 4344 | // In addition, pointers to members can be compared, or a pointer to |
| 4345 | // member and a null pointer constant. Pointer to member conversions |
| 4346 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 4347 | // them to a common type. If one operand is a null pointer constant, |
| 4348 | // the common type is the type of the other operand. Otherwise, the |
| 4349 | // common type is a pointer to member type similar (4.4) to the type |
| 4350 | // of one of the operands, with a cv-qualification signature (4.4) |
| 4351 | // that is the union of the cv-qualification signatures of the operand |
| 4352 | // types. |
| 4353 | QualType T = FindCompositePointerType(lex, rex); |
| 4354 | if (T.isNull()) { |
| 4355 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4356 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4357 | return QualType(); |
| 4358 | } |
| 4359 | |
| 4360 | ImpCastExprToType(lex, T); |
| 4361 | ImpCastExprToType(rex, T); |
| 4362 | return ResultTy; |
| 4363 | } |
| 4364 | |
| 4365 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4366 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4367 | return ResultTy; |
| 4368 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4369 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4370 | // Handle block pointer types. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4371 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4372 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 4373 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4374 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4375 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | b6eed6e | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4376 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4377 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4378 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4379 | } |
| 4380 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4381 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4382 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4383 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4384 | if (!isRelational |
| 4385 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4386 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4387 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4388 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4389 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4390 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4391 | ->getPointeeType()->isVoidType()))) |
| 4392 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4393 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4394 | } |
| 4395 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4396 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4397 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4398 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4399 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4400 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4401 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 4402 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4403 | bool LPtrToVoid = LPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4404 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4405 | bool RPtrToVoid = RPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4406 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4407 | |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4408 | if (!LPtrToVoid && !RPtrToVoid && |
| 4409 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4410 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4411 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4412 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4413 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4414 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4415 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4416 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4417 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4418 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 4419 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4420 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4421 | return ResultTy; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4422 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4423 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4424 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4425 | unsigned DiagID = 0; |
| 4426 | if (RHSIsNull) { |
| 4427 | if (isRelational) |
| 4428 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4429 | } else if (isRelational) |
| 4430 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4431 | else |
| 4432 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
| 4433 | |
| 4434 | if (DiagID) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4435 | Diag(Loc, DiagID) |
Chris Lattner | f350c6e | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4436 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4437 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4438 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4439 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4440 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4441 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4442 | unsigned DiagID = 0; |
| 4443 | if (LHSIsNull) { |
| 4444 | if (isRelational) |
| 4445 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4446 | } else if (isRelational) |
| 4447 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4448 | else |
| 4449 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4451 | if (DiagID) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4452 | Diag(Loc, DiagID) |
Chris Lattner | f350c6e | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4453 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4454 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4455 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4456 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4457 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4458 | // Handle block pointers. |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4459 | if (!isRelational && RHSIsNull |
| 4460 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4461 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4462 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4463 | } |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4464 | if (!isRelational && LHSIsNull |
| 4465 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4466 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4467 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4468 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4469 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4472 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4473 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4474 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4475 | /// types. |
| 4476 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4477 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4478 | bool isRelational) { |
| 4479 | // Check to make sure we're operating on vectors of the same type and width, |
| 4480 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4481 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4482 | if (vType.isNull()) |
| 4483 | return vType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4484 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4485 | QualType lType = lex->getType(); |
| 4486 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4487 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4488 | // For non-floating point types, check for self-comparisons of the form |
| 4489 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4490 | // often indicate logic errors in the program. |
| 4491 | if (!lType->isFloatingType()) { |
| 4492 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4493 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4494 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4495 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4496 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4497 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4498 | // Check for comparisons of floating point operands using != and ==. |
| 4499 | if (!isRelational && lType->isFloatingType()) { |
| 4500 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4501 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4502 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4503 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4504 | // Return the type for the comparison, which is the same as vector type for |
| 4505 | // integer vectors, or an integer type of identical size and number of |
| 4506 | // elements for floating point vectors. |
| 4507 | if (lType->isIntegerType()) |
| 4508 | return lType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4509 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4510 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4511 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4512 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4513 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4514 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4515 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4516 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4517 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4518 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4519 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4520 | } |
| 4521 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4522 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4523 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4524 | { |
| 4525 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4526 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4527 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4528 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4529 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4530 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4531 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4532 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4536 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4537 | { |
| 4538 | UsualUnaryConversions(lex); |
| 4539 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4540 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 4541 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4542 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4543 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4546 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4547 | /// is a read-only property; return true if so. A readonly property expression |
| 4548 | /// depends on various declarations and thus must be treated specially. |
| 4549 | /// |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4550 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4551 | { |
| 4552 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4553 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4554 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4555 | QualType BaseType = PropExpr->getBase()->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4556 | if (const ObjCObjectPointerType *OPT = |
| 4557 | BaseType->getAsObjCInterfacePointerType()) |
| 4558 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 4559 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4560 | return true; |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | return false; |
| 4564 | } |
| 4565 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4566 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4567 | /// emit an error and return true. If so, return false. |
| 4568 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4569 | SourceLocation OrigLoc = Loc; |
| 4570 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
| 4571 | &Loc); |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4572 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4573 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4574 | if (IsLV == Expr::MLV_Valid) |
| 4575 | return false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4576 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4577 | unsigned Diag = 0; |
| 4578 | bool NeedType = false; |
| 4579 | switch (IsLV) { // C99 6.5.16p2 |
| 4580 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4581 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4582 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4583 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4584 | NeedType = true; |
| 4585 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4586 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4587 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4588 | NeedType = true; |
| 4589 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4590 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4591 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4592 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4593 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4594 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4595 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4596 | case Expr::MLV_IncompleteType: |
| 4597 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4598 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4599 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 4600 | E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4601 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4602 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4603 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4604 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4605 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4606 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4607 | case Expr::MLV_ReadonlyProperty: |
| 4608 | Diag = diag::error_readonly_property_assignment; |
| 4609 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4610 | case Expr::MLV_NoSetterProperty: |
| 4611 | Diag = diag::error_nosetter_property_assignment; |
| 4612 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4613 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4614 | |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4615 | SourceRange Assign; |
| 4616 | if (Loc != OrigLoc) |
| 4617 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4618 | if (NeedType) |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4619 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4620 | else |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4621 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4622 | return true; |
| 4623 | } |
| 4624 | |
| 4625 | |
| 4626 | |
| 4627 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4628 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4629 | SourceLocation Loc, |
| 4630 | QualType CompoundType) { |
| 4631 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4632 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4633 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4634 | |
| 4635 | QualType LHSType = LHS->getType(); |
| 4636 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4637 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4638 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4639 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4640 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4641 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4642 | // Special case of NSObject attributes on c-style pointer types. |
| 4643 | if (ConvTy == IncompatiblePointer && |
| 4644 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | ad75bd2 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4645 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4646 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | ad75bd2 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4647 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4648 | ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4649 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4650 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 4651 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 4652 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4653 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4654 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 4655 | RHSCheck = ICE->getSubExpr(); |
| 4656 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 4657 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 4658 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4659 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4660 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4661 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 4662 | // And there is a space or other character before the subexpr of the |
| 4663 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | f1e5d4a | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 4664 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 4665 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4666 | Diag(Loc, diag::warn_not_compound_assign) |
| 4667 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 4668 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4669 | } |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4670 | } |
| 4671 | } else { |
| 4672 | // Compound assignment "x += y" |
Eli Friedman | b653af4 | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 4673 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4674 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4675 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4676 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 4677 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4678 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4680 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 4681 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4682 | // it is the unqualified version of the type of the left operand. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4683 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 4684 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4685 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4686 | // operand. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4687 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4690 | // C99 6.5.17 |
| 4691 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 4692 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4693 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4694 | |
| 4695 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 4696 | // incomplete in C++). |
| 4697 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4698 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 4702 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4703 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 4704 | bool isInc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4705 | if (Op->isTypeDependent()) |
| 4706 | return Context.DependentTy; |
| 4707 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4708 | QualType ResType = Op->getType(); |
| 4709 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4710 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4711 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 4712 | // Decrement of bool is not allowed. |
| 4713 | if (!isInc) { |
| 4714 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 4715 | return QualType(); |
| 4716 | } |
| 4717 | // Increment of bool sets it to true, but is deprecated. |
| 4718 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 4719 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4720 | // OK! |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4721 | } else if (ResType->isAnyPointerType()) { |
| 4722 | QualType PointeeTy = ResType->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4723 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4724 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4725 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4726 | if (getLangOptions().CPlusPlus) { |
| 4727 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 4728 | << Op->getSourceRange(); |
| 4729 | return QualType(); |
| 4730 | } |
| 4731 | |
| 4732 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4733 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4734 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4735 | if (getLangOptions().CPlusPlus) { |
| 4736 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 4737 | << Op->getType() << Op->getSourceRange(); |
| 4738 | return QualType(); |
| 4739 | } |
| 4740 | |
| 4741 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4742 | << ResType << Op->getSourceRange(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4743 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Douglas Gregor | cde3a2d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 4744 | diag::err_typecheck_arithmetic_incomplete_type, |
| 4745 | Op->getSourceRange(), SourceRange(), |
| 4746 | ResType)) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4747 | return QualType(); |
Fariborz Jahanian | 4738ac5 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 4748 | // Diagnose bad cases where we step over interface counts. |
| 4749 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4750 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 4751 | << PointeeTy << Op->getSourceRange(); |
| 4752 | return QualType(); |
| 4753 | } |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4754 | } else if (ResType->isComplexType()) { |
| 4755 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 4756 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4757 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4758 | } else { |
| 4759 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4760 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4761 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4762 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4763 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 4764 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4765 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4766 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4767 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4770 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4771 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4772 | /// where the declaration is needed for type checking. We only need to |
| 4773 | /// handle cases when the expression references a function designator |
| 4774 | /// or is an lvalue. Here are some examples: |
| 4775 | /// - &(x) => x |
| 4776 | /// - &*****f => f for f a function designator. |
| 4777 | /// - &s.xx => s |
| 4778 | /// - &s.zz[1].yy -> s, if zz is an array |
| 4779 | /// - *(x + 1) -> x, if x is an array |
| 4780 | /// - &"123"[2] -> 0 |
| 4781 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4782 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4783 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4784 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 4785 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4786 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4787 | case Stmt::MemberExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4788 | // If this is an arrow operator, the address is an offset from |
| 4789 | // the base's value, so the object the base refers to is |
| 4790 | // irrelevant. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4791 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4792 | return 0; |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4793 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4794 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4795 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4796 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 4797 | // promotion of register arrays earlier. |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4798 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 4799 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 4800 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 4801 | return getPrimaryDecl(ICE->getSubExpr()); |
| 4802 | } |
| 4803 | return 0; |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4804 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4805 | case Stmt::UnaryOperatorClass: { |
| 4806 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4807 | |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4808 | switch(UO->getOpcode()) { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4809 | case UnaryOperator::Real: |
| 4810 | case UnaryOperator::Imag: |
| 4811 | case UnaryOperator::Extension: |
| 4812 | return getPrimaryDecl(UO->getSubExpr()); |
| 4813 | default: |
| 4814 | return 0; |
| 4815 | } |
| 4816 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4817 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4818 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4819 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4820 | // If the result of an implicit cast is an l-value, we care about |
| 4821 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4822 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4823 | default: |
| 4824 | return 0; |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4829 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4830 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4831 | /// Note: The usual conversions are *not* applied to the operand of the & |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4832 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4833 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4834 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4835 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4836 | // Make sure to ignore parentheses in subsequent checks |
| 4837 | op = op->IgnoreParens(); |
| 4838 | |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 4839 | if (op->isTypeDependent()) |
| 4840 | return Context.DependentTy; |
| 4841 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4842 | if (getLangOptions().C99) { |
| 4843 | // Implement C99-only parts of addressof rules. |
| 4844 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 4845 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 4846 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 4847 | // (assuming the deref expression is valid). |
| 4848 | return uOp->getSubExpr()->getType(); |
| 4849 | } |
| 4850 | // Technically, there should be a check for array subscript |
| 4851 | // expressions here, but the result of one is always an lvalue anyway. |
| 4852 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4853 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 4854 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 4855 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4856 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 4857 | // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4858 | // The operand must be either an l-value or a function designator |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4859 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4860 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 4861 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 4862 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4863 | return QualType(); |
| 4864 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 4865 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4866 | // The operand cannot be a bit-field |
| 4867 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4868 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 4869 | return QualType(); |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4870 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 4871 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4872 | // The operand cannot be an element of a vector |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4873 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 4874 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4875 | return QualType(); |
Fariborz Jahanian | b35984a | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 4876 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 4877 | // cannot take address of a property expression. |
| 4878 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4879 | << "property expression" << op->getSourceRange(); |
| 4880 | return QualType(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 4881 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4882 | // We have an lvalue with a decl. Make sure the decl is not declared |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4883 | // with the register storage-class specifier. |
| 4884 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 4885 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4886 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 4887 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4888 | return QualType(); |
| 4889 | } |
Douglas Gregor | 62f7876 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4890 | } else if (isa<OverloadedFunctionDecl>(dcl) || |
| 4891 | isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4892 | return Context.OverloadTy; |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 4893 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 4894 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4895 | // Could be a pointer to member, though, if there is an explicit |
| 4896 | // scope qualifier for the class. |
| 4897 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 4898 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 4899 | if (Ctx && Ctx->isRecord()) { |
| 4900 | if (FD->getType()->isReferenceType()) { |
| 4901 | Diag(OpLoc, |
| 4902 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 4903 | << FD->getDeclName() << FD->getType(); |
| 4904 | return QualType(); |
| 4905 | } |
| 4906 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4907 | return Context.getMemberPointerType(op->getType(), |
| 4908 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 4909 | } |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 4910 | } |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4911 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 4912 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4913 | // As above. |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 4914 | if (isa<QualifiedDeclRefExpr>(op) && MD->isInstance()) |
| 4915 | return Context.getMemberPointerType(op->getType(), |
| 4916 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 4917 | } else if (!isa<FunctionDecl>(dcl)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4918 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4919 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 4920 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 4921 | if (lval == Expr::LV_IncompleteVoidType) { |
| 4922 | // Taking the address of a void variable is technically illegal, but we |
| 4923 | // allow it in cases which are otherwise valid. |
| 4924 | // Example: "extern void x; void* y = &x;". |
| 4925 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 4926 | } |
| 4927 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4928 | // If the operand has type "type", the result has type "pointer to type". |
| 4929 | return Context.getPointerType(op->getType()); |
| 4930 | } |
| 4931 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4932 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4933 | if (Op->isTypeDependent()) |
| 4934 | return Context.DependentTy; |
| 4935 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4936 | UsualUnaryConversions(Op); |
| 4937 | QualType Ty = Op->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4938 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4939 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 4940 | // incomplete type or void. It would be possible to warn about dereferencing |
| 4941 | // a void pointer, but it's completely well-defined, and such a warning is |
| 4942 | // unlikely to catch any mistakes. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4943 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4944 | return PT->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4945 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4946 | if (const ObjCObjectPointerType *OPT = Ty->getAsObjCObjectPointerType()) |
| 4947 | return OPT->getPointeeType(); |
| 4948 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4949 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4950 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4951 | return QualType(); |
| 4952 | } |
| 4953 | |
| 4954 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 4955 | tok::TokenKind Kind) { |
| 4956 | BinaryOperator::Opcode Opc; |
| 4957 | switch (Kind) { |
| 4958 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4959 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 4960 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4961 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 4962 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 4963 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 4964 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 4965 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 4966 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 4967 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 4968 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 4969 | case tok::less: Opc = BinaryOperator::LT; break; |
| 4970 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 4971 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 4972 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 4973 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 4974 | case tok::amp: Opc = BinaryOperator::And; break; |
| 4975 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 4976 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 4977 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 4978 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 4979 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 4980 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 4981 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 4982 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 4983 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 4984 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 4985 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 4986 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 4987 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 4988 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 4989 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 4990 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 4991 | } |
| 4992 | return Opc; |
| 4993 | } |
| 4994 | |
| 4995 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 4996 | tok::TokenKind Kind) { |
| 4997 | UnaryOperator::Opcode Opc; |
| 4998 | switch (Kind) { |
| 4999 | default: assert(0 && "Unknown unary op!"); |
| 5000 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5001 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5002 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5003 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5004 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5005 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5006 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5007 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5008 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5009 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 5010 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 5011 | } |
| 5012 | return Opc; |
| 5013 | } |
| 5014 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5015 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5016 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5017 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5018 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5019 | unsigned Op, |
| 5020 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5021 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5022 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5023 | // The following two variables are used for compound assignment operators |
| 5024 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5025 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5026 | |
| 5027 | switch (Opc) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5028 | case BinaryOperator::Assign: |
| 5029 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5030 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5031 | case BinaryOperator::PtrMemD: |
| 5032 | case BinaryOperator::PtrMemI: |
| 5033 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5034 | Opc == BinaryOperator::PtrMemI); |
| 5035 | break; |
| 5036 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5037 | case BinaryOperator::Div: |
| 5038 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5039 | break; |
| 5040 | case BinaryOperator::Rem: |
| 5041 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5042 | break; |
| 5043 | case BinaryOperator::Add: |
| 5044 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5045 | break; |
| 5046 | case BinaryOperator::Sub: |
| 5047 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5048 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5049 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5050 | case BinaryOperator::Shr: |
| 5051 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5052 | break; |
| 5053 | case BinaryOperator::LE: |
| 5054 | case BinaryOperator::LT: |
| 5055 | case BinaryOperator::GE: |
| 5056 | case BinaryOperator::GT: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5057 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5058 | break; |
| 5059 | case BinaryOperator::EQ: |
| 5060 | case BinaryOperator::NE: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5061 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5062 | break; |
| 5063 | case BinaryOperator::And: |
| 5064 | case BinaryOperator::Xor: |
| 5065 | case BinaryOperator::Or: |
| 5066 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5067 | break; |
| 5068 | case BinaryOperator::LAnd: |
| 5069 | case BinaryOperator::LOr: |
| 5070 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5071 | break; |
| 5072 | case BinaryOperator::MulAssign: |
| 5073 | case BinaryOperator::DivAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5074 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 5075 | CompLHSTy = CompResultTy; |
| 5076 | if (!CompResultTy.isNull()) |
| 5077 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5078 | break; |
| 5079 | case BinaryOperator::RemAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5080 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 5081 | CompLHSTy = CompResultTy; |
| 5082 | if (!CompResultTy.isNull()) |
| 5083 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5084 | break; |
| 5085 | case BinaryOperator::AddAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5086 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5087 | if (!CompResultTy.isNull()) |
| 5088 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5089 | break; |
| 5090 | case BinaryOperator::SubAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5091 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5092 | if (!CompResultTy.isNull()) |
| 5093 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5094 | break; |
| 5095 | case BinaryOperator::ShlAssign: |
| 5096 | case BinaryOperator::ShrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5097 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 5098 | CompLHSTy = CompResultTy; |
| 5099 | if (!CompResultTy.isNull()) |
| 5100 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5101 | break; |
| 5102 | case BinaryOperator::AndAssign: |
| 5103 | case BinaryOperator::XorAssign: |
| 5104 | case BinaryOperator::OrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5105 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 5106 | CompLHSTy = CompResultTy; |
| 5107 | if (!CompResultTy.isNull()) |
| 5108 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5109 | break; |
| 5110 | case BinaryOperator::Comma: |
| 5111 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 5112 | break; |
| 5113 | } |
| 5114 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5115 | return ExprError(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5116 | if (CompResultTy.isNull()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5117 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 5118 | else |
| 5119 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5120 | CompLHSTy, CompResultTy, |
| 5121 | OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5124 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5125 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 5126 | tok::TokenKind Kind, |
| 5127 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5128 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5129 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5130 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5131 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 5132 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5134 | if (getLangOptions().CPlusPlus && |
| 5135 | (lhs->getType()->isOverloadableType() || |
| 5136 | rhs->getType()->isOverloadableType())) { |
| 5137 | // Find all of the overloaded operators visible from this |
| 5138 | // point. We perform both an operator-name lookup from the local |
| 5139 | // scope and an argument-dependent lookup based on the types of |
| 5140 | // the arguments. |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5141 | FunctionSet Functions; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5142 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 5143 | if (OverOp != OO_None) { |
| 5144 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 5145 | Functions); |
| 5146 | Expr *Args[2] = { lhs, rhs }; |
| 5147 | DeclarationName OpName |
| 5148 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5149 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5150 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5151 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5152 | // Build the (potentially-overloaded, potentially-dependent) |
| 5153 | // binary operation. |
| 5154 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5157 | // Build a built-in binary operation. |
| 5158 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5161 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| 5162 | unsigned OpcIn, |
| 5163 | ExprArg InputArg) { |
| 5164 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5165 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5166 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5167 | Expr *Input = (Expr *)InputArg.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5168 | QualType resultType; |
| 5169 | switch (Opc) { |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5170 | case UnaryOperator::OffsetOf: |
| 5171 | assert(false && "Invalid unary operator"); |
| 5172 | break; |
| 5173 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5174 | case UnaryOperator::PreInc: |
| 5175 | case UnaryOperator::PreDec: |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5176 | case UnaryOperator::PostInc: |
| 5177 | case UnaryOperator::PostDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5178 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5179 | Opc == UnaryOperator::PreInc || |
| 5180 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5181 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5182 | case UnaryOperator::AddrOf: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5183 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 5184 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5185 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 5186 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5187 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 5188 | break; |
| 5189 | case UnaryOperator::Plus: |
| 5190 | case UnaryOperator::Minus: |
| 5191 | UsualUnaryConversions(Input); |
| 5192 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5193 | if (resultType->isDependentType()) |
| 5194 | break; |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5195 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 5196 | break; |
| 5197 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 5198 | resultType->isEnumeralType()) |
| 5199 | break; |
| 5200 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 5201 | Opc == UnaryOperator::Plus && |
| 5202 | resultType->isPointerType()) |
| 5203 | break; |
| 5204 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5205 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5206 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5207 | case UnaryOperator::Not: // bitwise complement |
| 5208 | UsualUnaryConversions(Input); |
| 5209 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5210 | if (resultType->isDependentType()) |
| 5211 | break; |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5212 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 5213 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 5214 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5215 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5216 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5217 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5218 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5219 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5220 | break; |
| 5221 | case UnaryOperator::LNot: // logical negation |
| 5222 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 5223 | DefaultFunctionArrayConversion(Input); |
| 5224 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5225 | if (resultType->isDependentType()) |
| 5226 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5227 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5228 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5229 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5230 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5231 | // In C++, it's bool. C++ 5.3.1p8 |
| 5232 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5233 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5234 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5235 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 5236 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5237 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5238 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5239 | resultType = Input->getType(); |
| 5240 | break; |
| 5241 | } |
| 5242 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5243 | return ExprError(); |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5244 | |
| 5245 | InputArg.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5246 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5249 | // Unary Operators. 'Tok' is the token for the operator. |
| 5250 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5251 | tok::TokenKind Op, ExprArg input) { |
| 5252 | Expr *Input = (Expr*)input.get(); |
| 5253 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 5254 | |
| 5255 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 5256 | // Find all of the overloaded operators visible from this |
| 5257 | // point. We perform both an operator-name lookup from the local |
| 5258 | // scope and an argument-dependent lookup based on the types of |
| 5259 | // the arguments. |
| 5260 | FunctionSet Functions; |
| 5261 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 5262 | if (OverOp != OO_None) { |
| 5263 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 5264 | Functions); |
| 5265 | DeclarationName OpName |
| 5266 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5267 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 5268 | } |
| 5269 | |
| 5270 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 5271 | } |
| 5272 | |
| 5273 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 5274 | } |
| 5275 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 5276 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5277 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 5278 | SourceLocation LabLoc, |
| 5279 | IdentifierInfo *LabelII) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5280 | // Look up the record for this label identifier. |
Chris Lattner | 2616d8c | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 5281 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5282 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 5283 | // If we haven't seen this label yet, create a forward reference. It |
| 5284 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | b88d81c | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 5285 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5286 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5287 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5288 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5289 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 5290 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5293 | Sema::OwningExprResult |
| 5294 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 5295 | SourceLocation RPLoc) { // "({..})" |
| 5296 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5297 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 5298 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 5299 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5300 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | aa25759 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 5301 | if (isFileScope) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5302 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5303 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5304 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 5305 | // example, it is not possible to goto into a stmt expression apparently. |
| 5306 | // More semantic analysis is needed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5307 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5308 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 5309 | // as the type of the stmtexpr. |
| 5310 | QualType Ty = Context.VoidTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5311 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5312 | if (!Compound->body_empty()) { |
| 5313 | Stmt *LastStmt = Compound->body_back(); |
| 5314 | // If LastStmt is a label, skip down through into the body. |
| 5315 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 5316 | LastStmt = Label->getSubStmt(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5317 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5318 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5319 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5320 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5321 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5322 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 5323 | // expressions are not lvalues. |
| 5324 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5325 | substmt.release(); |
| 5326 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5327 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5328 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5329 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 5330 | SourceLocation BuiltinLoc, |
| 5331 | SourceLocation TypeLoc, |
| 5332 | TypeTy *argty, |
| 5333 | OffsetOfComponent *CompPtr, |
| 5334 | unsigned NumComponents, |
| 5335 | SourceLocation RPLoc) { |
| 5336 | // FIXME: This function leaks all expressions in the offset components on |
| 5337 | // error. |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5338 | // FIXME: Preserve type source info. |
| 5339 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5340 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5341 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5342 | bool Dependent = ArgTy->isDependentType(); |
| 5343 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5344 | // We must have at least one component that refers to the type, and the first |
| 5345 | // one is known to be a field designator. Verify that the ArgTy represents |
| 5346 | // a struct/union/class. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5347 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5348 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5349 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5350 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 5351 | // with an incomplete type would be illegal. |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 5352 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5353 | // Otherwise, create a null pointer as the base, and iteratively process |
| 5354 | // the offsetof designators. |
| 5355 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 5356 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5357 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5358 | ArgTy, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 5359 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5360 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 5361 | // GCC extension, diagnose them. |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5362 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 5363 | // a system header! |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5364 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5365 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 5366 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5367 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5368 | if (!Dependent) { |
Eli Friedman | c24ae00 | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 5369 | bool DidWarnAboutNonPOD = false; |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5370 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5371 | // FIXME: Dependent case loses a lot of information here. And probably |
| 5372 | // leaks like a sieve. |
| 5373 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 5374 | const OffsetOfComponent &OC = CompPtr[i]; |
| 5375 | if (OC.isBrackets) { |
| 5376 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 5377 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 5378 | if (!AT) { |
| 5379 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5380 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 5381 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 5385 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5386 | // Promote the array so it looks more like a normal array subscript |
| 5387 | // expression. |
| 5388 | DefaultFunctionArrayConversion(Res); |
| 5389 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5390 | // C99 6.5.2.1p1 |
| 5391 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5392 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5393 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5394 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5395 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5396 | << Idx->getSourceRange()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5397 | |
| 5398 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5399 | OC.LocEnd); |
| 5400 | continue; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5401 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5402 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5403 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5404 | if (!RC) { |
| 5405 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5406 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5407 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5408 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5409 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5410 | // Get the decl corresponding to this. |
| 5411 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5412 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5413 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | bbceaea | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5414 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5415 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5416 | << Res->getType()); |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5417 | DidWarnAboutNonPOD = true; |
| 5418 | } |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5421 | FieldDecl *MemberDecl |
| 5422 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 5423 | LookupMemberName) |
| 5424 | .getAsDecl()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5425 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5426 | if (!MemberDecl) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5427 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member) |
| 5428 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5429 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5430 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5431 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5432 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5433 | Res = BuildAnonymousStructUnionMemberReference( |
| 5434 | SourceLocation(), MemberDecl, Res, SourceLocation()).takeAs<Expr>(); |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5435 | } else { |
| 5436 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5437 | // doesn't matter here. |
| 5438 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5439 | MemberDecl->getType().getNonReferenceType()); |
| 5440 | } |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5441 | } |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5442 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5443 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5444 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5445 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5449 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5450 | TypeTy *arg1,TypeTy *arg2, |
| 5451 | SourceLocation RPLoc) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5452 | // FIXME: Preserve type source info. |
| 5453 | QualType argT1 = GetTypeFromParser(arg1); |
| 5454 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5455 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5456 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | e621150 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5458 | if (getLangOptions().CPlusPlus) { |
| 5459 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5460 | << SourceRange(BuiltinLoc, RPLoc); |
| 5461 | return ExprError(); |
| 5462 | } |
| 5463 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5464 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5465 | argT1, argT2, RPLoc)); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5468 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5469 | ExprArg cond, |
| 5470 | ExprArg expr1, ExprArg expr2, |
| 5471 | SourceLocation RPLoc) { |
| 5472 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5473 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5474 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5475 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5476 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5477 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5478 | QualType resType; |
Douglas Gregor | dd4ae3f | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5479 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5480 | resType = Context.DependentTy; |
| 5481 | } else { |
| 5482 | // The conditional expression is required to be a constant expression. |
| 5483 | llvm::APSInt condEval(32); |
| 5484 | SourceLocation ExpLoc; |
| 5485 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5486 | return ExprError(Diag(ExpLoc, |
| 5487 | diag::err_typecheck_choose_expr_requires_constant) |
| 5488 | << CondExpr->getSourceRange()); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5489 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5490 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5491 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 5492 | } |
| 5493 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5494 | cond.release(); expr1.release(); expr2.release(); |
| 5495 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 5496 | resType, RPLoc)); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5499 | //===----------------------------------------------------------------------===// |
| 5500 | // Clang Extensions. |
| 5501 | //===----------------------------------------------------------------------===// |
| 5502 | |
| 5503 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5504 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5505 | // Analyze block parameters. |
| 5506 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5507 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5508 | // Add BSI to CurBlock. |
| 5509 | BSI->PrevBlockInfo = CurBlock; |
| 5510 | CurBlock = BSI; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5511 | |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5512 | BSI->ReturnType = QualType(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5513 | BSI->TheScope = BlockScope; |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5514 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | c7ef2b9 | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 5515 | BSI->hasPrototype = false; |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5516 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5517 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5518 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5519 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5520 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5523 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5524 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5525 | |
| 5526 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5527 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5528 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5529 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5530 | |
Mike Stump | 458287d | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5531 | if (T->isArrayType()) { |
| 5532 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5533 | diag::err_block_returns_array); |
| 5534 | return; |
| 5535 | } |
| 5536 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5537 | // The parameter list is optional, if there was none, assume (). |
| 5538 | if (!T->isFunctionType()) |
| 5539 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 5540 | |
| 5541 | CurBlock->hasPrototype = true; |
| 5542 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5543 | // Check for a valid sentinel attribute on this block. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5544 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5545 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5546 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5547 | // FIXME: remove the attribute. |
| 5548 | } |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5549 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
| 5550 | |
| 5551 | // Do not allow returning a objc interface by-value. |
| 5552 | if (RetTy->isObjCInterfaceType()) { |
| 5553 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5554 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5555 | return; |
| 5556 | } |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5557 | return; |
| 5558 | } |
| 5559 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5560 | // Analyze arguments to block. |
| 5561 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 5562 | "Not a function declarator!"); |
| 5563 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5564 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5565 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 5566 | CurBlock->isVariadic = true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5567 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5568 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 5569 | // no arguments, not a function that takes a single void argument. |
| 5570 | if (FTI.hasPrototype && |
| 5571 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5572 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 5573 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5574 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5575 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5576 | } else if (FTI.hasPrototype) { |
| 5577 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5578 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5579 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5580 | } |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5581 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5582 | CurBlock->Params.size()); |
Fariborz Jahanian | 536f73d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 5583 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5584 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5585 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 5586 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 5587 | // If this has an identifier, add it to the scope stack. |
| 5588 | if ((*AI)->getIdentifier()) |
| 5589 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5590 | |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5591 | // Check for a valid sentinel attribute on this block. |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5592 | if (!CurBlock->isVariadic && |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5593 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5594 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5595 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5596 | // FIXME: remove the attribute. |
| 5597 | } |
| 5598 | |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5599 | // Analyze the return type. |
| 5600 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5601 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
| 5602 | |
| 5603 | // Do not allow returning a objc interface by-value. |
| 5604 | if (RetTy->isObjCInterfaceType()) { |
| 5605 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5606 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5607 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5608 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 5612 | /// is invoked to pop the information about the block from the action impl. |
| 5613 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 5614 | // Ensure that CurBlock is deleted. |
| 5615 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5616 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5617 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 5618 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5619 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | eb4d4a5 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 5620 | PopDeclContext(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5621 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5622 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 5626 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5627 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 5628 | StmtArg body, Scope *CurScope) { |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5629 | // If blocks are disabled, emit an error. |
| 5630 | if (!LangOpts.Blocks) |
| 5631 | Diag(CaretLoc, diag::err_blocks_disable); |
| 5632 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5633 | // Ensure that CurBlock is deleted. |
| 5634 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5635 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5636 | PopDeclContext(); |
| 5637 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5638 | // Pop off CurBlock, handle nested blocks. |
| 5639 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5640 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5641 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5642 | if (!BSI->ReturnType.isNull()) |
| 5643 | RetTy = BSI->ReturnType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5644 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5645 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 5646 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 5647 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5648 | |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5649 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5650 | QualType BlockTy; |
| 5651 | if (!BSI->hasPrototype) |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5652 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 5653 | NoReturn); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5654 | else |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5655 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5656 | BSI->isVariadic, 0, false, false, 0, 0, |
| 5657 | NoReturn); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5658 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5659 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5660 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5661 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5662 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5663 | // If needed, diagnose invalid gotos and switches in the block. |
| 5664 | if (CurFunctionNeedsScopeChecking) |
| 5665 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 5666 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
| 5667 | |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5668 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5669 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5670 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 5671 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5672 | } |
| 5673 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5674 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 5675 | ExprArg expr, TypeTy *type, |
| 5676 | SourceLocation RPLoc) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5677 | QualType T = GetTypeFromParser(type); |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5678 | Expr *E = static_cast<Expr*>(expr.get()); |
| 5679 | Expr *OrigExpr = E; |
| 5680 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5681 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5682 | |
| 5683 | // Get the va_list type |
| 5684 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5685 | if (VaListType->isArrayType()) { |
| 5686 | // Deal with implicit array decay; for example, on x86-64, |
| 5687 | // va_list is an array, but it's supposed to decay to |
| 5688 | // a pointer for va_arg. |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5689 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5690 | // Make sure the input expression also decays appropriately. |
| 5691 | UsualUnaryConversions(E); |
| 5692 | } else { |
| 5693 | // Otherwise, the va_list argument must be an l-value because |
| 5694 | // it is modified by va_arg. |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5695 | if (!E->isTypeDependent() && |
| 5696 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5697 | return ExprError(); |
| 5698 | } |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5700 | if (!E->isTypeDependent() && |
| 5701 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5702 | return ExprError(Diag(E->getLocStart(), |
| 5703 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5704 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 89a72c5 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 5705 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5706 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5707 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5708 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5709 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5710 | expr.release(); |
| 5711 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 5712 | RPLoc)); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5713 | } |
| 5714 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5715 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5716 | // The type of __null will be int or long, depending on the size of |
| 5717 | // pointers on the target. |
| 5718 | QualType Ty; |
| 5719 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 5720 | Ty = Context.IntTy; |
| 5721 | else |
| 5722 | Ty = Context.LongTy; |
| 5723 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5724 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5725 | } |
| 5726 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5727 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 5728 | SourceLocation Loc, |
| 5729 | QualType DstType, QualType SrcType, |
| 5730 | Expr *SrcExpr, const char *Flavor) { |
| 5731 | // Decode the result (notice that AST's are still created for extensions). |
| 5732 | bool isInvalid = false; |
| 5733 | unsigned DiagKind; |
| 5734 | switch (ConvTy) { |
| 5735 | default: assert(0 && "Unknown conversion type"); |
| 5736 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5737 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5738 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 5739 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5740 | case IntToPointer: |
| 5741 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 5742 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5743 | case IncompatiblePointer: |
| 5744 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 5745 | break; |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5746 | case IncompatiblePointerSign: |
| 5747 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 5748 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5749 | case FunctionVoidPointer: |
| 5750 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 5751 | break; |
| 5752 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 5753 | // If the qualifiers lost were because we were applying the |
| 5754 | // (deprecated) C++ conversion from a string literal to a char* |
| 5755 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 5756 | // Ideally, this check would be performed in |
| 5757 | // CheckPointerTypesForAssignment. However, that would require a |
| 5758 | // bit of refactoring (so that the second argument is an |
| 5759 | // expression, rather than a type), which should be done as part |
| 5760 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 5761 | // C++ semantics. |
| 5762 | if (getLangOptions().CPlusPlus && |
| 5763 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 5764 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5765 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 5766 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5767 | case IntToBlockPointer: |
| 5768 | DiagKind = diag::err_int_to_block_pointer; |
| 5769 | break; |
| 5770 | case IncompatibleBlockPointer: |
Mike Stump | d331e75 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 5771 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5772 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5773 | case IncompatibleObjCQualifiedId: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5774 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5775 | // it can give a more specific diagnostic. |
| 5776 | DiagKind = diag::warn_incompatible_qualified_id; |
| 5777 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5778 | case IncompatibleVectors: |
| 5779 | DiagKind = diag::warn_incompatible_vectors; |
| 5780 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5781 | case Incompatible: |
| 5782 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 5783 | isInvalid = true; |
| 5784 | break; |
| 5785 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5786 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5787 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 5788 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5789 | return isInvalid; |
| 5790 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5791 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 5792 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5793 | llvm::APSInt ICEResult; |
| 5794 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 5795 | if (Result) |
| 5796 | *Result = ICEResult; |
| 5797 | return false; |
| 5798 | } |
| 5799 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5800 | Expr::EvalResult EvalResult; |
| 5801 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5802 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5803 | EvalResult.HasSideEffects) { |
| 5804 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 5805 | |
| 5806 | if (EvalResult.Diag) { |
| 5807 | // We only show the note if it's not the usual "invalid subexpression" |
| 5808 | // or if it's actually in a subexpression. |
| 5809 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 5810 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 5811 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 5812 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5813 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5814 | return true; |
| 5815 | } |
| 5816 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5817 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 5818 | E->getSourceRange(); |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5819 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5820 | if (EvalResult.Diag && |
| 5821 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 5822 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5823 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5824 | if (Result) |
| 5825 | *Result = EvalResult.Val.getInt(); |
| 5826 | return false; |
| 5827 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5828 | |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5829 | Sema::ExpressionEvaluationContext |
| 5830 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
| 5831 | // Introduce a new set of potentially referenced declarations to the stack. |
| 5832 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 5833 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
| 5834 | |
| 5835 | std::swap(ExprEvalContext, NewContext); |
| 5836 | return NewContext; |
| 5837 | } |
| 5838 | |
| 5839 | void |
| 5840 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 5841 | ExpressionEvaluationContext NewContext) { |
| 5842 | ExprEvalContext = NewContext; |
| 5843 | |
| 5844 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 5845 | // Mark any remaining declarations in the current position of the stack |
| 5846 | // as "referenced". If they were not meant to be referenced, semantic |
| 5847 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 5848 | PotentiallyReferencedDecls RemainingDecls; |
| 5849 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 5850 | PotentiallyReferencedDeclStack.pop_back(); |
| 5851 | |
| 5852 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 5853 | IEnd = RemainingDecls.end(); |
| 5854 | I != IEnd; ++I) |
| 5855 | MarkDeclarationReferenced(I->first, I->second); |
| 5856 | } |
| 5857 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5858 | |
| 5859 | /// \brief Note that the given declaration was referenced in the source code. |
| 5860 | /// |
| 5861 | /// This routine should be invoke whenever a given declaration is referenced |
| 5862 | /// in the source code, and where that reference occurred. If this declaration |
| 5863 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 5864 | /// C99 6.9p3), then the declaration will be marked as used. |
| 5865 | /// |
| 5866 | /// \param Loc the location where the declaration was referenced. |
| 5867 | /// |
| 5868 | /// \param D the declaration that has been referenced by the source code. |
| 5869 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 5870 | assert(D && "No declaration?"); |
| 5871 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5872 | if (D->isUsed()) |
| 5873 | return; |
| 5874 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5875 | // Mark a parameter declaration "used", regardless of whether we're in a |
| 5876 | // template or not. |
| 5877 | if (isa<ParmVarDecl>(D)) |
| 5878 | D->setUsed(true); |
| 5879 | |
| 5880 | // Do not mark anything as "used" within a dependent context; wait for |
| 5881 | // an instantiation. |
| 5882 | if (CurContext->isDependentContext()) |
| 5883 | return; |
| 5884 | |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5885 | switch (ExprEvalContext) { |
| 5886 | case Unevaluated: |
| 5887 | // We are in an expression that is not potentially evaluated; do nothing. |
| 5888 | return; |
| 5889 | |
| 5890 | case PotentiallyEvaluated: |
| 5891 | // We are in a potentially-evaluated expression, so this declaration is |
| 5892 | // "used"; handle this below. |
| 5893 | break; |
| 5894 | |
| 5895 | case PotentiallyPotentiallyEvaluated: |
| 5896 | // We are in an expression that may be potentially evaluated; queue this |
| 5897 | // declaration reference until we know whether the expression is |
| 5898 | // potentially evaluated. |
| 5899 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 5900 | return; |
| 5901 | } |
| 5902 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5903 | // Note that this declaration has been used. |
Fariborz Jahanian | 8915a3d | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 5904 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5905 | unsigned TypeQuals; |
Fariborz Jahanian | 2f5a0a3 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 5906 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 5907 | if (!Constructor->isUsed()) |
| 5908 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5909 | } else if (Constructor->isImplicit() && |
| 5910 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5911 | if (!Constructor->isUsed()) |
| 5912 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 5913 | } |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5914 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 5915 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 5916 | DefineImplicitDestructor(Loc, Destructor); |
| 5917 | |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5918 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 5919 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 5920 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 5921 | if (!MethodDecl->isUsed()) |
| 5922 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 5923 | } |
| 5924 | } |
Fariborz Jahanian | b12bd43 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 5925 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 5926 | // Implicit instantiation of function templates and member functions of |
| 5927 | // class templates. |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 5928 | if (!Function->getBody()) { |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 5929 | // FIXME: distinguish between implicit instantiations of function |
| 5930 | // templates and explicit specializations (the latter don't get |
| 5931 | // instantiated, naturally). |
| 5932 | if (Function->getInstantiatedFromMemberFunction() || |
| 5933 | Function->getPrimaryTemplate()) |
Douglas Gregor | dcdb384 | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 5934 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5938 | // FIXME: keep track of references to static functions |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5939 | Function->setUsed(true); |
| 5940 | return; |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5941 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5942 | |
| 5943 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5944 | // Implicit instantiation of static data members of class templates. |
| 5945 | // FIXME: distinguish between implicit instantiations (which we need to |
| 5946 | // actually instantiate) and explicit specializations. |
| 5947 | if (Var->isStaticDataMember() && |
| 5948 | Var->getInstantiatedFromStaticDataMember()) |
| 5949 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 5950 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5951 | // FIXME: keep track of references to static data? |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5953 | D->setUsed(true); |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5954 | return; |
| 5955 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5956 | } |
| 5957 | |