Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/Lex/LiteralSupport.h" |
| 26 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 27 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 28 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 29 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 30 | #include "clang/Parse/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 34 | /// \brief Determine whether the use of this declaration is valid, and |
| 35 | /// emit any corresponding diagnostics. |
| 36 | /// |
| 37 | /// This routine diagnoses various problems with referencing |
| 38 | /// declarations that can occur when using a declaration. For example, |
| 39 | /// it might warn if a deprecated or unavailable declaration is being |
| 40 | /// used, or produce an error (and return true) if a C++0x deleted |
| 41 | /// function is being used. |
| 42 | /// |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 43 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 44 | /// decls. |
| 45 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 46 | /// \returns true if there was an error (this declaration cannot be |
| 47 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 48 | /// |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 49 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 50 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 51 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 52 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 55 | // See if the decl is unavailable |
| 56 | if (D->getAttr<UnavailableAttr>()) { |
| 57 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 58 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 59 | } |
| 60 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 61 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 62 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 63 | if (FD->isDeleted()) { |
| 64 | Diag(Loc, diag::err_deleted_function_use); |
| 65 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 66 | return true; |
| 67 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 68 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 70 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 73 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 75 | /// attribute. It warns if call does not have the sentinel argument. |
| 76 | /// |
| 77 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 79 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 81 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 82 | int sentinelPos = attr->getSentinel(); |
| 83 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 85 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 86 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 87 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 88 | bool warnNotEnoughArgs = false; |
| 89 | int isMethod = 0; |
| 90 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 91 | // skip over named parameters. |
| 92 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 93 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 94 | if (nullPos) |
| 95 | --nullPos; |
| 96 | else |
| 97 | ++i; |
| 98 | } |
| 99 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 100 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 101 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 102 | // skip over named parameters. |
| 103 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 104 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 105 | if (nullPos) |
| 106 | --nullPos; |
| 107 | else |
| 108 | ++i; |
| 109 | } |
| 110 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 111 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 112 | // block or function pointer call. |
| 113 | QualType Ty = V->getType(); |
| 114 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 116 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 117 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 118 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 119 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 120 | unsigned k; |
| 121 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 122 | if (nullPos) |
| 123 | --nullPos; |
| 124 | else |
| 125 | ++i; |
| 126 | } |
| 127 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 128 | } |
| 129 | if (Ty->isBlockPointerType()) |
| 130 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 131 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 132 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 133 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
| 136 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 137 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 138 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | int sentinel = i; |
| 142 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 143 | --sentinelPos; |
| 144 | ++i; |
| 145 | } |
| 146 | if (sentinelPos > 0) { |
| 147 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 148 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | while (i < NumArgs-1) { |
| 152 | ++i; |
| 153 | ++sentinel; |
| 154 | } |
| 155 | Expr *sentinelExpr = Args[sentinel]; |
Anders Carlsson | e4d2bdd | 2009-11-24 17:24:21 +0000 | [diff] [blame] | 156 | if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) && |
| 157 | (!sentinelExpr->getType()->isPointerType() || |
| 158 | !sentinelExpr->isNullPointerConstant(Context, |
| 159 | Expr::NPC_ValueDependentIsNull)))) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 160 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 161 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | } |
| 163 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 166 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 167 | Expr *Ex = (Expr *)E; |
| 168 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 169 | } |
| 170 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 171 | //===----------------------------------------------------------------------===// |
| 172 | // Standard Promotions and Conversions |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 175 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 176 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 177 | QualType Ty = E->getType(); |
| 178 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 179 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 180 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 182 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 183 | else if (Ty->isArrayType()) { |
| 184 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 185 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 186 | // type 'array of type' is converted to an expression that has type 'pointer |
| 187 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 188 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 189 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 190 | // |
| 191 | // C++ 4.2p1: |
| 192 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 193 | // T" can be converted to an rvalue of type "pointer to T". |
| 194 | // |
| 195 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 196 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 197 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 198 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 204 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 205 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 206 | /// In these instances, this routine should *not* be called. |
| 207 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 208 | QualType Ty = Expr->getType(); |
| 209 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 211 | // C99 6.3.1.1p2: |
| 212 | // |
| 213 | // The following may be used in an expression wherever an int or |
| 214 | // unsigned int may be used: |
| 215 | // - an object or expression with an integer type whose integer |
| 216 | // conversion rank is less than or equal to the rank of int |
| 217 | // and unsigned int. |
| 218 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 219 | // |
| 220 | // If an int can represent all values of the original type, the |
| 221 | // value is converted to an int; otherwise, it is converted to an |
| 222 | // unsigned int. These are called the integer promotions. All |
| 223 | // other types are unchanged by the integer promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 224 | QualType PTy = Context.isPromotableBitField(Expr); |
| 225 | if (!PTy.isNull()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 226 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 227 | return Expr; |
| 228 | } |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 229 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 230 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 231 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 232 | return Expr; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 235 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 236 | return Expr; |
| 237 | } |
| 238 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 239 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 241 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 242 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 243 | QualType Ty = Expr->getType(); |
| 244 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 246 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 247 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 248 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 249 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 250 | CastExpr::CK_FloatingCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 252 | UsualUnaryConversions(Expr); |
| 253 | } |
| 254 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 255 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 256 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 257 | /// interfaces passed by value. This returns true if the argument type is |
| 258 | /// completely illegal. |
| 259 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 260 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 262 | if (Expr->getType()->isObjCInterfaceType()) { |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 263 | switch (ExprEvalContexts.back().Context ) { |
| 264 | case Unevaluated: |
| 265 | // The argument will never be evaluated, so don't complain. |
| 266 | break; |
| 267 | |
| 268 | case PotentiallyEvaluated: |
| 269 | Diag(Expr->getLocStart(), |
| 270 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 271 | << Expr->getType() << CT; |
| 272 | return true; |
| 273 | |
| 274 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 275 | ExprEvalContexts.back().addDiagnostic(Expr->getLocStart(), |
| 276 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 277 | << Expr->getType() << CT); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 282 | if (!Expr->getType()->isPODType()) { |
| 283 | switch (ExprEvalContexts.back().Context ) { |
| 284 | case Unevaluated: |
| 285 | // The argument will never be evaluated, so don't complain. |
| 286 | break; |
| 287 | |
| 288 | case PotentiallyEvaluated: |
| 289 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 290 | << Expr->getType() << CT; |
| 291 | break; |
| 292 | |
| 293 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 294 | ExprEvalContexts.back().addDiagnostic(Expr->getLocStart(), |
| 295 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 296 | << Expr->getType() << CT); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | } |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 300 | |
| 301 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 305 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 306 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 308 | /// responsible for emitting appropriate error diagnostics. |
| 309 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 310 | /// GCC. |
| 311 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 312 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 313 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 314 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 315 | |
| 316 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 319 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 320 | QualType lhs = |
| 321 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 323 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 324 | |
| 325 | // If both types are identical, no conversion is needed. |
| 326 | if (lhs == rhs) |
| 327 | return lhs; |
| 328 | |
| 329 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 330 | // The caller can deal with this (e.g. pointer + int). |
| 331 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 332 | return lhs; |
| 333 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 334 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 335 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 336 | if (!LHSBitfieldPromoteTy.isNull()) |
| 337 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 338 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 339 | if (!RHSBitfieldPromoteTy.isNull()) |
| 340 | rhs = RHSBitfieldPromoteTy; |
| 341 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 342 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 343 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 344 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 345 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 346 | return destType; |
| 347 | } |
| 348 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 349 | //===----------------------------------------------------------------------===// |
| 350 | // Semantic Analysis for various Expression Types |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | |
| 353 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 354 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 356 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 357 | /// multiple tokens. However, the common case is that StringToks points to one |
| 358 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 359 | /// |
| 360 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 361 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | assert(NumStringToks && "Must have at least one string!"); |
| 363 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 364 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 366 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | |
| 368 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 369 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 370 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 371 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 372 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 373 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 374 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 375 | |
| 376 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 377 | if (getLangOptions().CPlusPlus) |
| 378 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 379 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 380 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 381 | // the nul terminator character as well as the string length for pascal |
| 382 | // strings. |
| 383 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 384 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 385 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 387 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 389 | Literal.GetStringLength(), |
| 390 | Literal.AnyWide, StrTy, |
| 391 | &StringTokLocs[0], |
| 392 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 395 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 396 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 397 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 398 | /// for values inside the block or for globals). |
| 399 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 400 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 401 | /// up-to-date. |
| 402 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 403 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 404 | ValueDecl *VD) { |
| 405 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 406 | // we wanted to. |
| 407 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 408 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 410 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 411 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 412 | return false; |
| 413 | |
| 414 | // If this is a reference to an extern, static, or global variable, no need to |
| 415 | // snapshot it. |
| 416 | // FIXME: What about 'const' variables in C++? |
| 417 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 418 | if (!Var->hasLocalStorage()) |
| 419 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 421 | // Blocks that have these can't be constant. |
| 422 | CurBlock->hasBlockDeclRefExprs = true; |
| 423 | |
| 424 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 425 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 426 | // be defined outside all of the current blocks (in which case the blocks do |
| 427 | // all get the bit). Walk the nesting chain. |
| 428 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 429 | NextBlock = NextBlock->PrevBlockInfo) { |
| 430 | // If we found the defining block for the variable, don't mark the block as |
| 431 | // having a reference outside it. |
| 432 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 433 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 435 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 436 | // a snapshot as well. |
| 437 | NextBlock->hasBlockDeclRefExprs = true; |
| 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 440 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 443 | |
| 444 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 445 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 446 | Sema::OwningExprResult |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 447 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 448 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 449 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 450 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 452 | << D->getDeclName(); |
| 453 | return ExprError(); |
| 454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 456 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 457 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 458 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 459 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 461 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 463 | << D->getIdentifier(); |
| 464 | return ExprError(); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 470 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 472 | return Owned(DeclRefExpr::Create(Context, |
| 473 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 474 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 475 | D, Loc, Ty)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 478 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 479 | /// variable corresponding to the anonymous union or struct whose type |
| 480 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 481 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 482 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 484 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 486 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 487 | // operation rather than a slow walk through DeclContext's vector (which |
| 488 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 489 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 491 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 492 | D != DEnd; ++D) { |
| 493 | if (*D == Record) { |
| 494 | // The object for the anonymous struct/union directly |
| 495 | // follows its type in the list of declarations. |
| 496 | ++D; |
| 497 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 498 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 499 | return *D; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | assert(false && "Missing object for anonymous record"); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 507 | /// \brief Given a field that represents a member of an anonymous |
| 508 | /// struct/union, build the path from that field's context to the |
| 509 | /// actual member. |
| 510 | /// |
| 511 | /// Construct the sequence of field member references we'll have to |
| 512 | /// perform to get to the field in the anonymous union/struct. The |
| 513 | /// list of members is built from the field outward, so traverse it |
| 514 | /// backwards to go from an object in the current context to the field |
| 515 | /// we found. |
| 516 | /// |
| 517 | /// \returns The variable from which the field access should begin, |
| 518 | /// for an anonymous struct/union that is not a member of another |
| 519 | /// class. Otherwise, returns NULL. |
| 520 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 521 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 522 | assert(Field->getDeclContext()->isRecord() && |
| 523 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 524 | && "Field must be stored inside an anonymous struct or union"); |
| 525 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 526 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 527 | VarDecl *BaseObject = 0; |
| 528 | DeclContext *Ctx = Field->getDeclContext(); |
| 529 | do { |
| 530 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 531 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 532 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 533 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 534 | else { |
| 535 | BaseObject = cast<VarDecl>(AnonObject); |
| 536 | break; |
| 537 | } |
| 538 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 540 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 541 | |
| 542 | return BaseObject; |
| 543 | } |
| 544 | |
| 545 | Sema::OwningExprResult |
| 546 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 547 | FieldDecl *Field, |
| 548 | Expr *BaseObjectExpr, |
| 549 | SourceLocation OpLoc) { |
| 550 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 552 | AnonFields); |
| 553 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 554 | // Build the expression that refers to the base object, from |
| 555 | // which we will build a sequence of member references to each |
| 556 | // of the anonymous union objects and, eventually, the field we |
| 557 | // found via name lookup. |
| 558 | bool BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 559 | Qualifiers BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 560 | if (BaseObject) { |
| 561 | // BaseObject is an anonymous struct/union variable (and is, |
| 562 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 563 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 564 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 565 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 566 | SourceLocation()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 567 | BaseQuals |
| 568 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 569 | } else if (BaseObjectExpr) { |
| 570 | // The caller provided the base object expression. Determine |
| 571 | // whether its a pointer and whether it adds any qualifiers to the |
| 572 | // anonymous struct/union fields we're looking into. |
| 573 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 574 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 575 | BaseObjectIsPointer = true; |
| 576 | ObjectType = ObjectPtr->getPointeeType(); |
| 577 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 578 | BaseQuals |
| 579 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 580 | } else { |
| 581 | // We've found a member of an anonymous struct/union that is |
| 582 | // inside a non-anonymous struct/union, so in a well-formed |
| 583 | // program our base object expression is "this". |
| 584 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 585 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 587 | = Context.getTagDeclType( |
| 588 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 589 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 591 | == Context.getCanonicalType(ThisType)) || |
| 592 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 593 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 594 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 595 | MD->getThisType(Context)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 596 | BaseObjectIsPointer = true; |
| 597 | } |
| 598 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 599 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 600 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 601 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 602 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 606 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 607 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // Build the implicit member references to the field of the |
| 611 | // anonymous struct/union. |
| 612 | Expr *Result = BaseObjectExpr; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 613 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 614 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 615 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 616 | FI != FIEnd; ++FI) { |
| 617 | QualType MemberType = (*FI)->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 618 | Qualifiers MemberTypeQuals = |
| 619 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 620 | |
| 621 | // CVR attributes from the base are picked up by members, |
| 622 | // except that 'mutable' members don't pick up 'const'. |
| 623 | if ((*FI)->isMutable()) |
| 624 | ResultQuals.removeConst(); |
| 625 | |
| 626 | // GC attributes are never picked up by members. |
| 627 | ResultQuals.removeObjCGCAttr(); |
| 628 | |
| 629 | // TR 18037 does not allow fields to be declared with address spaces. |
| 630 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 631 | |
| 632 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 633 | if (NewQuals != MemberTypeQuals) |
| 634 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 635 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 636 | MarkDeclarationReferenced(Loc, *FI); |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 637 | PerformObjectMemberConversion(Result, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 638 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 639 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 640 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 641 | BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 642 | ResultQuals = NewQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 645 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 646 | } |
| 647 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 648 | /// Decomposes the given name into a DeclarationName, its location, and |
| 649 | /// possibly a list of template arguments. |
| 650 | /// |
| 651 | /// If this produces template arguments, it is permitted to call |
| 652 | /// DecomposeTemplateName. |
| 653 | /// |
| 654 | /// This actually loses a lot of source location information for |
| 655 | /// non-standard name kinds; we should consider preserving that in |
| 656 | /// some way. |
| 657 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 658 | const UnqualifiedId &Id, |
| 659 | TemplateArgumentListInfo &Buffer, |
| 660 | DeclarationName &Name, |
| 661 | SourceLocation &NameLoc, |
| 662 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 663 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 664 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 665 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 666 | |
| 667 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 668 | Id.TemplateId->getTemplateArgs(), |
| 669 | Id.TemplateId->NumArgs); |
| 670 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 671 | TemplateArgsPtr.release(); |
| 672 | |
| 673 | TemplateName TName = |
| 674 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 675 | |
| 676 | Name = SemaRef.Context.getNameForTemplate(TName); |
| 677 | NameLoc = Id.TemplateId->TemplateNameLoc; |
| 678 | TemplateArgs = &Buffer; |
| 679 | } else { |
| 680 | Name = SemaRef.GetNameFromUnqualifiedId(Id); |
| 681 | NameLoc = Id.StartLocation; |
| 682 | TemplateArgs = 0; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// Decompose the given template name into a list of lookup results. |
| 687 | /// |
| 688 | /// The unqualified ID must name a non-dependent template, which can |
| 689 | /// be more easily tested by checking whether DecomposeUnqualifiedId |
| 690 | /// found template arguments. |
| 691 | static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) { |
| 692 | assert(Id.getKind() == UnqualifiedId::IK_TemplateId); |
| 693 | TemplateName TName = |
| 694 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 695 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 696 | if (TemplateDecl *TD = TName.getAsTemplateDecl()) |
| 697 | R.addDecl(TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 698 | else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate()) |
| 699 | for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end(); |
| 700 | I != E; ++I) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 701 | R.addDecl(*I); |
John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 702 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 703 | R.resolveKind(); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 704 | } |
| 705 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 706 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
| 707 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 708 | E = Record->bases_end(); I != E; ++I) { |
| 709 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 710 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 711 | if (!BaseRT) return false; |
| 712 | |
| 713 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 714 | if (!BaseRecord->isDefinition() || |
| 715 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | return true; |
| 720 | } |
| 721 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 722 | /// Determines whether we can lookup this id-expression now or whether |
| 723 | /// we have to wait until template instantiation is complete. |
| 724 | static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 725 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 726 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 727 | // If the qualifier scope isn't computable, it's definitely dependent. |
| 728 | if (!DC) return true; |
| 729 | |
| 730 | // If the qualifier scope doesn't name a record, we can always look into it. |
| 731 | if (!isa<CXXRecordDecl>(DC)) return false; |
| 732 | |
| 733 | // We can't look into record types unless they're fully-formed. |
| 734 | if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true; |
| 735 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 738 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 739 | /// Determines if the given class is provably not derived from all of |
| 740 | /// the prospective base classes. |
| 741 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 742 | CXXRecordDecl *Record, |
| 743 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 744 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 745 | return false; |
| 746 | |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 747 | RecordDecl *RD = Record->getDefinition(SemaRef.Context); |
| 748 | if (!RD) return false; |
| 749 | Record = cast<CXXRecordDecl>(RD); |
| 750 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 751 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 752 | E = Record->bases_end(); I != E; ++I) { |
| 753 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 754 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 755 | if (!BaseRT) return false; |
| 756 | |
| 757 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 758 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | return true; |
| 763 | } |
| 764 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 765 | /// Determines if this a C++ class member. |
| 766 | static bool IsClassMember(NamedDecl *D) { |
| 767 | DeclContext *DC = D->getDeclContext(); |
John McCall | 336e774 | 2009-12-02 19:59:55 +0000 | [diff] [blame] | 768 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 769 | // C++0x [class.mem]p1: |
| 770 | // The enumerators of an unscoped enumeration defined in |
| 771 | // the class are members of the class. |
| 772 | // FIXME: support C++0x scoped enumerations. |
| 773 | if (isa<EnumDecl>(DC)) |
| 774 | DC = DC->getParent(); |
| 775 | |
| 776 | return DC->isRecord(); |
| 777 | } |
| 778 | |
| 779 | /// Determines if this is an instance member of a class. |
| 780 | static bool IsInstanceMember(NamedDecl *D) { |
| 781 | assert(IsClassMember(D) && |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 782 | "checking whether non-member is instance member"); |
| 783 | |
| 784 | if (isa<FieldDecl>(D)) return true; |
| 785 | |
| 786 | if (isa<CXXMethodDecl>(D)) |
| 787 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 788 | |
| 789 | if (isa<FunctionTemplateDecl>(D)) { |
| 790 | D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 791 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 792 | } |
| 793 | |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | enum IMAKind { |
| 798 | /// The reference is definitely not an instance member access. |
| 799 | IMA_Static, |
| 800 | |
| 801 | /// The reference may be an implicit instance member access. |
| 802 | IMA_Mixed, |
| 803 | |
| 804 | /// The reference may be to an instance member, but it is invalid if |
| 805 | /// so, because the context is not an instance method. |
| 806 | IMA_Mixed_StaticContext, |
| 807 | |
| 808 | /// The reference may be to an instance member, but it is invalid if |
| 809 | /// so, because the context is from an unrelated class. |
| 810 | IMA_Mixed_Unrelated, |
| 811 | |
| 812 | /// The reference is definitely an implicit instance member access. |
| 813 | IMA_Instance, |
| 814 | |
| 815 | /// The reference may be to an unresolved using declaration. |
| 816 | IMA_Unresolved, |
| 817 | |
| 818 | /// The reference may be to an unresolved using declaration and the |
| 819 | /// context is not an instance method. |
| 820 | IMA_Unresolved_StaticContext, |
| 821 | |
| 822 | /// The reference is to a member of an anonymous structure in a |
| 823 | /// non-class context. |
| 824 | IMA_AnonymousMember, |
| 825 | |
| 826 | /// All possible referrents are instance members and the current |
| 827 | /// context is not an instance method. |
| 828 | IMA_Error_StaticContext, |
| 829 | |
| 830 | /// All possible referrents are instance members of an unrelated |
| 831 | /// class. |
| 832 | IMA_Error_Unrelated |
| 833 | }; |
| 834 | |
| 835 | /// The given lookup names class member(s) and is not being used for |
| 836 | /// an address-of-member expression. Classify the type of access |
| 837 | /// according to whether it's possible that this reference names an |
| 838 | /// instance member. This is best-effort; it is okay to |
| 839 | /// conservatively answer "yes", in which case some errors will simply |
| 840 | /// not be caught until template-instantiation. |
| 841 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 842 | const LookupResult &R) { |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 843 | assert(!R.empty() && IsClassMember(*R.begin())); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 844 | |
| 845 | bool isStaticContext = |
| 846 | (!isa<CXXMethodDecl>(SemaRef.CurContext) || |
| 847 | cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic()); |
| 848 | |
| 849 | if (R.isUnresolvableResult()) |
| 850 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 851 | |
| 852 | // Collect all the declaring classes of instance members we find. |
| 853 | bool hasNonInstance = false; |
| 854 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 855 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 856 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 857 | if (IsInstanceMember(D)) { |
| 858 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 859 | |
| 860 | // If this is a member of an anonymous record, move out to the |
| 861 | // innermost non-anonymous struct or union. If there isn't one, |
| 862 | // that's a special case. |
| 863 | while (R->isAnonymousStructOrUnion()) { |
| 864 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 865 | if (!R) return IMA_AnonymousMember; |
| 866 | } |
| 867 | Classes.insert(R->getCanonicalDecl()); |
| 868 | } |
| 869 | else |
| 870 | hasNonInstance = true; |
| 871 | } |
| 872 | |
| 873 | // If we didn't find any instance members, it can't be an implicit |
| 874 | // member reference. |
| 875 | if (Classes.empty()) |
| 876 | return IMA_Static; |
| 877 | |
| 878 | // If the current context is not an instance method, it can't be |
| 879 | // an implicit member reference. |
| 880 | if (isStaticContext) |
| 881 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 882 | |
| 883 | // If we can prove that the current context is unrelated to all the |
| 884 | // declaring classes, it can't be an implicit member reference (in |
| 885 | // which case it's an error if any of those members are selected). |
| 886 | if (IsProvablyNotDerivedFrom(SemaRef, |
| 887 | cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(), |
| 888 | Classes)) |
| 889 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 890 | |
| 891 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 892 | } |
| 893 | |
| 894 | /// Diagnose a reference to a field with no object available. |
| 895 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 896 | const CXXScopeSpec &SS, |
| 897 | const LookupResult &R) { |
| 898 | SourceLocation Loc = R.getNameLoc(); |
| 899 | SourceRange Range(Loc); |
| 900 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 901 | |
| 902 | if (R.getAsSingle<FieldDecl>()) { |
| 903 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 904 | if (MD->isStatic()) { |
| 905 | // "invalid use of member 'x' in static member function" |
| 906 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 907 | << Range << R.getLookupName(); |
| 908 | return; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 913 | << R.getLookupName() << Range; |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 918 | } |
| 919 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 920 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 921 | const CXXScopeSpec &SS, |
| 922 | UnqualifiedId &Id, |
| 923 | bool HasTrailingLParen, |
| 924 | bool isAddressOfOperand) { |
| 925 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 926 | "cannot be direct & operand and have a trailing lparen"); |
| 927 | |
| 928 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 929 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 930 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 931 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 932 | |
| 933 | // Decompose the UnqualifiedId into the following data. |
| 934 | DeclarationName Name; |
| 935 | SourceLocation NameLoc; |
| 936 | const TemplateArgumentListInfo *TemplateArgs; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 937 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 938 | Name, NameLoc, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 940 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 941 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 942 | // C++ [temp.dep.expr]p3: |
| 943 | // An id-expression is type-dependent if it contains: |
| 944 | // -- a nested-name-specifier that contains a class-name that |
| 945 | // names a dependent type. |
| 946 | // Determine whether this is a member of an unknown specialization; |
| 947 | // we need to handle these differently. |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 948 | if (SS.isSet() && IsDependentIdExpression(*this, SS)) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 949 | return ActOnDependentIdExpression(SS, Name, NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 950 | isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 951 | TemplateArgs); |
| 952 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 953 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 954 | // Perform the required lookup. |
| 955 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 956 | if (TemplateArgs) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 957 | // Just re-use the lookup done by isTemplateName. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 958 | DecomposeTemplateName(R, Id); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 959 | } else { |
| 960 | LookupParsedName(R, S, &SS, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 962 | // If this reference is in an Objective-C method, then we need to do |
| 963 | // some special Objective-C lookup, too. |
| 964 | if (!SS.isSet() && II && getCurMethodDecl()) { |
| 965 | OwningExprResult E(LookupInObjCMethod(R, S, II)); |
| 966 | if (E.isInvalid()) |
| 967 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 969 | Expr *Ex = E.takeAs<Expr>(); |
| 970 | if (Ex) return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 973 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 974 | if (R.isAmbiguous()) |
| 975 | return ExprError(); |
| 976 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 977 | // Determine whether this name might be a candidate for |
| 978 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 979 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 980 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 981 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 983 | // in C90, extension in C99, forbidden in C++). |
| 984 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 985 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 986 | if (D) R.addDecl(D); |
| 987 | } |
| 988 | |
| 989 | // If this name wasn't predeclared and if this is not a function |
| 990 | // call, diagnose the problem. |
| 991 | if (R.empty()) { |
| 992 | if (!SS.isEmpty()) |
| 993 | return ExprError(Diag(NameLoc, diag::err_no_member) |
| 994 | << Name << computeDeclContext(SS, false) |
| 995 | << SS.getRange()); |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 996 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 997 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 998 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 999 | return ExprError(Diag(NameLoc, diag::err_undeclared_use) |
| 1000 | << Name); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1001 | else |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1002 | return ExprError(Diag(NameLoc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1006 | // This is guaranteed from this point on. |
| 1007 | assert(!R.empty() || ADL); |
| 1008 | |
| 1009 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1010 | // Warn about constructs like: |
| 1011 | // if (void *X = foo()) { ... } else { X }. |
| 1012 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1014 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1015 | Scope *CheckS = S; |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1016 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1018 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1019 | ExprError(Diag(NameLoc, diag::warn_value_always_zero) |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1020 | << Var->getDeclName() |
| 1021 | << (Var->getType()->isPointerType()? 2 : |
| 1022 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1026 | // Move to the parent of this scope. |
| 1027 | CheckS = CheckS->getParent(); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1030 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1031 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1032 | // C99 DR 316 says that, if a function type comes from a |
| 1033 | // function definition (without a prototype), that type is only |
| 1034 | // used for checking compatibility. Therefore, when referencing |
| 1035 | // the function, we pretend that we don't have the full function |
| 1036 | // type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1037 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1038 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1040 | QualType T = Func->getType(); |
| 1041 | QualType NoProtoType = T; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1042 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1043 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1044 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1048 | // Check whether this might be a C++ implicit instance member access. |
| 1049 | // C++ [expr.prim.general]p6: |
| 1050 | // Within the definition of a non-static member function, an |
| 1051 | // identifier that names a non-static member is transformed to a |
| 1052 | // class member access expression. |
| 1053 | // But note that &SomeClass::foo is grammatically distinct, even |
| 1054 | // though we don't parse it that way. |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 1055 | if (!R.empty() && IsClassMember(*R.begin())) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1056 | bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty()); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1057 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1058 | if (!isAbstractMemberPointer) { |
| 1059 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1060 | case IMA_Instance: |
| 1061 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1062 | |
| 1063 | case IMA_AnonymousMember: |
| 1064 | assert(R.isSingleResult()); |
| 1065 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1066 | R.getAsSingle<FieldDecl>()); |
| 1067 | |
| 1068 | case IMA_Mixed: |
| 1069 | case IMA_Mixed_Unrelated: |
| 1070 | case IMA_Unresolved: |
| 1071 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1072 | |
| 1073 | case IMA_Static: |
| 1074 | case IMA_Mixed_StaticContext: |
| 1075 | case IMA_Unresolved_StaticContext: |
| 1076 | break; |
| 1077 | |
| 1078 | case IMA_Error_StaticContext: |
| 1079 | case IMA_Error_Unrelated: |
| 1080 | DiagnoseInstanceReference(*this, SS, R); |
| 1081 | return ExprError(); |
| 1082 | } |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1086 | if (TemplateArgs) |
| 1087 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1088 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1089 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1090 | } |
| 1091 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1092 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1093 | /// declaration name, generally during template instantiation. |
| 1094 | /// There's a large number of things which don't need to be done along |
| 1095 | /// this path. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1096 | Sema::OwningExprResult |
| 1097 | Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS, |
| 1098 | DeclarationName Name, |
| 1099 | SourceLocation NameLoc) { |
| 1100 | DeclContext *DC; |
| 1101 | if (!(DC = computeDeclContext(SS, false)) || |
| 1102 | DC->isDependentContext() || |
| 1103 | RequireCompleteDeclContext(SS)) |
| 1104 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0); |
| 1105 | |
| 1106 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1107 | LookupQualifiedName(R, DC); |
| 1108 | |
| 1109 | if (R.isAmbiguous()) |
| 1110 | return ExprError(); |
| 1111 | |
| 1112 | if (R.empty()) { |
| 1113 | Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange(); |
| 1114 | return ExprError(); |
| 1115 | } |
| 1116 | |
| 1117 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1118 | } |
| 1119 | |
| 1120 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1121 | /// detected that we're currently inside an ObjC method. Perform some |
| 1122 | /// additional lookup. |
| 1123 | /// |
| 1124 | /// Ideally, most of this would be done by lookup, but there's |
| 1125 | /// actually quite a lot of extra work involved. |
| 1126 | /// |
| 1127 | /// Returns a null sentinel to indicate trivial success. |
| 1128 | Sema::OwningExprResult |
| 1129 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
| 1130 | IdentifierInfo *II) { |
| 1131 | SourceLocation Loc = Lookup.getNameLoc(); |
| 1132 | |
| 1133 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1134 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1135 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1136 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1137 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1138 | |
| 1139 | // If we're in a class method, we don't normally want to look for |
| 1140 | // ivars. But if we don't find anything else, and there's an |
| 1141 | // ivar, that's an error. |
| 1142 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 1143 | |
| 1144 | bool LookForIvars; |
| 1145 | if (Lookup.empty()) |
| 1146 | LookForIvars = true; |
| 1147 | else if (IsClassMethod) |
| 1148 | LookForIvars = false; |
| 1149 | else |
| 1150 | LookForIvars = (Lookup.isSingleResult() && |
| 1151 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1152 | |
| 1153 | if (LookForIvars) { |
| 1154 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1155 | ObjCInterfaceDecl *ClassDeclared; |
| 1156 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1157 | // Diagnose using an ivar in a class method. |
| 1158 | if (IsClassMethod) |
| 1159 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1160 | << IV->getDeclName()); |
| 1161 | |
| 1162 | // If we're referencing an invalid decl, just return this as a silent |
| 1163 | // error node. The error diagnostic was already emitted on the decl. |
| 1164 | if (IV->isInvalidDecl()) |
| 1165 | return ExprError(); |
| 1166 | |
| 1167 | // Check if referencing a field with __attribute__((deprecated)). |
| 1168 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1169 | return ExprError(); |
| 1170 | |
| 1171 | // Diagnose the use of an ivar outside of the declaring class. |
| 1172 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1173 | ClassDeclared != IFace) |
| 1174 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1175 | |
| 1176 | // FIXME: This should use a new expr for a direct reference, don't |
| 1177 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1178 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1179 | UnqualifiedId SelfName; |
| 1180 | SelfName.setIdentifier(&II, SourceLocation()); |
| 1181 | CXXScopeSpec SelfScopeSpec; |
| 1182 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 1183 | SelfName, false, false); |
| 1184 | MarkDeclarationReferenced(Loc, IV); |
| 1185 | return Owned(new (Context) |
| 1186 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1187 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1188 | } |
| 1189 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
| 1190 | // We should warn if a local variable hides an ivar. |
| 1191 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1192 | ObjCInterfaceDecl *ClassDeclared; |
| 1193 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1194 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1195 | IFace == ClassDeclared) |
| 1196 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | // Needed to implement property "super.method" notation. |
| 1201 | if (Lookup.empty() && II->isStr("super")) { |
| 1202 | QualType T; |
| 1203 | |
| 1204 | if (getCurMethodDecl()->isInstanceMethod()) |
| 1205 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 1206 | getCurMethodDecl()->getClassInterface())); |
| 1207 | else |
| 1208 | T = Context.getObjCClassType(); |
| 1209 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
| 1210 | } |
| 1211 | |
| 1212 | // Sentinel value saying that we didn't do anything special. |
| 1213 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1214 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1215 | |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1216 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1217 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1218 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 1219 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1221 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1223 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1224 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 1225 | return false; |
| 1226 | QualType FromRecordType = From->getType(); |
| 1227 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1228 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1229 | DestType = Context.getPointerType(DestType); |
| 1230 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1231 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1232 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 1233 | CheckDerivedToBaseConversion(FromRecordType, |
| 1234 | DestRecordType, |
| 1235 | From->getSourceRange().getBegin(), |
| 1236 | From->getSourceRange())) |
| 1237 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 1238 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 1239 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1240 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1241 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1242 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1244 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1246 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1247 | SourceLocation Loc, QualType Ty, |
| 1248 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1249 | NestedNameSpecifier *Qualifier = 0; |
| 1250 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1251 | if (SS.isSet()) { |
| 1252 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1253 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1256 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
| 1257 | Member, Loc, TemplateArgs, Ty); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1260 | /// Builds an implicit member access expression. The current context |
| 1261 | /// is known to be an instance method, and the given unqualified lookup |
| 1262 | /// set is known to contain only instance members, at least one of which |
| 1263 | /// is from an appropriate type. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1264 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1265 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1266 | LookupResult &R, |
| 1267 | const TemplateArgumentListInfo *TemplateArgs, |
| 1268 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1269 | assert(!R.empty() && !R.isAmbiguous()); |
| 1270 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1271 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1272 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1273 | // We may have found a field within an anonymous union or struct |
| 1274 | // (C++ [class.union]). |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1275 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1276 | // FIXME: template-ids inside anonymous structs? |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1277 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1278 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1279 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1280 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1281 | // If this is known to be an instance access, go ahead and build a |
| 1282 | // 'this' expression now. |
| 1283 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 1284 | Expr *This = 0; // null signifies implicit access |
| 1285 | if (IsKnownInstance) { |
| 1286 | This = new (Context) CXXThisExpr(SourceLocation(), ThisType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1289 | return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType, |
| 1290 | /*OpLoc*/ SourceLocation(), |
| 1291 | /*IsArrow*/ true, |
| 1292 | SS, R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1295 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1296 | const LookupResult &R, |
| 1297 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1298 | // Only when used directly as the postfix-expression of a call. |
| 1299 | if (!HasTrailingLParen) |
| 1300 | return false; |
| 1301 | |
| 1302 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1303 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1304 | return false; |
| 1305 | |
| 1306 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1307 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1308 | return false; |
| 1309 | |
| 1310 | // Turn off ADL when we find certain kinds of declarations during |
| 1311 | // normal lookup: |
| 1312 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1313 | NamedDecl *D = *I; |
| 1314 | |
| 1315 | // C++0x [basic.lookup.argdep]p3: |
| 1316 | // -- a declaration of a class member |
| 1317 | // Since using decls preserve this property, we check this on the |
| 1318 | // original decl. |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 1319 | if (IsClassMember(D)) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1320 | return false; |
| 1321 | |
| 1322 | // C++0x [basic.lookup.argdep]p3: |
| 1323 | // -- a block-scope function declaration that is not a |
| 1324 | // using-declaration |
| 1325 | // NOTE: we also trigger this for function templates (in fact, we |
| 1326 | // don't check the decl type at all, since all other decl types |
| 1327 | // turn off ADL anyway). |
| 1328 | if (isa<UsingShadowDecl>(D)) |
| 1329 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1330 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1331 | return false; |
| 1332 | |
| 1333 | // C++0x [basic.lookup.argdep]p3: |
| 1334 | // -- a declaration that is neither a function or a function |
| 1335 | // template |
| 1336 | // And also for builtin functions. |
| 1337 | if (isa<FunctionDecl>(D)) { |
| 1338 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1339 | |
| 1340 | // But also builtin functions. |
| 1341 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1342 | return false; |
| 1343 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1351 | /// Diagnoses obvious problems with the use of the given declaration |
| 1352 | /// as an expression. This is only actually called for lookups that |
| 1353 | /// were not overloaded, and it doesn't promise that the declaration |
| 1354 | /// will in fact be used. |
| 1355 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1356 | if (isa<TypedefDecl>(D)) { |
| 1357 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1358 | return true; |
| 1359 | } |
| 1360 | |
| 1361 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1362 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | if (isa<NamespaceDecl>(D)) { |
| 1367 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1375 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1376 | LookupResult &R, |
| 1377 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1378 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1379 | // just build an ordinary singleton decl ref. |
| 1380 | if (!NeedsADL && R.isSingleResult()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1381 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1382 | |
| 1383 | // We only need to check the declaration if there's exactly one |
| 1384 | // result, because in the overloaded case the results can only be |
| 1385 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1386 | if (R.isSingleResult() && |
| 1387 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1388 | return ExprError(); |
| 1389 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1390 | bool Dependent |
| 1391 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1392 | UnresolvedLookupExpr *ULE |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1393 | = UnresolvedLookupExpr::Create(Context, Dependent, |
| 1394 | (NestedNameSpecifier*) SS.getScopeRep(), |
| 1395 | SS.getRange(), |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1396 | R.getLookupName(), R.getNameLoc(), |
| 1397 | NeedsADL, R.isOverloadedResult()); |
| 1398 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1399 | ULE->addDecl(*I); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1400 | |
| 1401 | return Owned(ULE); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1406 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1407 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1408 | SourceLocation Loc, NamedDecl *D) { |
| 1409 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1410 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1411 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1412 | DeclarationName Name = D->getDeclName(); |
| 1413 | |
| 1414 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1415 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1417 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1418 | // Specifically diagnose references to class templates that are missing |
| 1419 | // a template argument list. |
| 1420 | Diag(Loc, diag::err_template_decl_ref) |
| 1421 | << Template << SS.getRange(); |
| 1422 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1423 | return ExprError(); |
| 1424 | } |
| 1425 | |
| 1426 | // Make sure that we're referring to a value. |
| 1427 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1428 | if (!VD) { |
| 1429 | Diag(Loc, diag::err_ref_non_value) |
| 1430 | << D << SS.getRange(); |
Daniel Dunbar | ec3455f | 2009-12-15 04:24:24 +0000 | [diff] [blame] | 1431 | Diag(D->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1432 | return ExprError(); |
| 1433 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1435 | // Check whether this declaration can be used. Note that we suppress |
| 1436 | // this check when we're going to perform argument-dependent lookup |
| 1437 | // on this function name, because this might not be the function |
| 1438 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1439 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1440 | return ExprError(); |
| 1441 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1442 | // Only create DeclRefExpr's for valid Decl's. |
| 1443 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1444 | return ExprError(); |
| 1445 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1446 | // If the identifier reference is inside a block, and it refers to a value |
| 1447 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1448 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1449 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1450 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1451 | // We do not do this for things like enum constants, global variables, etc, |
| 1452 | // as they do not get snapshotted. |
| 1453 | // |
| 1454 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1455 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1456 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1457 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1458 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1459 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1460 | // This is to record that a 'const' was actually synthesize and added. |
| 1461 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1462 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1464 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1466 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1467 | } |
| 1468 | // If this reference is not in a block or if the referenced variable is |
| 1469 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1470 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1471 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1474 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1475 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1476 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1477 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1478 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1479 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1480 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1481 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1482 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1485 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1486 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1488 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1489 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1490 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1491 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1492 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1493 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1494 | QualType ResTy; |
| 1495 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1496 | ResTy = Context.DependentTy; |
| 1497 | } else { |
| 1498 | unsigned Length = |
| 1499 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1500 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1501 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1502 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1503 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1504 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1505 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1508 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1509 | llvm::SmallString<16> CharBuffer; |
| 1510 | CharBuffer.resize(Tok.getLength()); |
| 1511 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1512 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1514 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1515 | Tok.getLocation(), PP); |
| 1516 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1517 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1518 | |
| 1519 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1520 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1521 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1522 | Literal.isWide(), |
| 1523 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1526 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1527 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1529 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1530 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1531 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1532 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1533 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1534 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1535 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1537 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1538 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1539 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1541 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1542 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1543 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1545 | Tok.getLocation(), PP); |
| 1546 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1547 | return ExprError(); |
| 1548 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1549 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1551 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1552 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1553 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1554 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1555 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1556 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1557 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1558 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1559 | |
| 1560 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1561 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1562 | // isExact will be set by GetFloatValue(). |
| 1563 | bool isExact = false; |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1564 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1565 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1567 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1568 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1569 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1570 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1572 | // long long is a C99 feature. |
| 1573 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1574 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1575 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1577 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1578 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1580 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1581 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1582 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1583 | Ty = Context.UnsignedLongLongTy; |
| 1584 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1585 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1586 | } else { |
| 1587 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1588 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1590 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1591 | // be an unsigned int. |
| 1592 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1593 | |
| 1594 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1595 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1596 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1597 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1598 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1599 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1600 | // Does it fit in a unsigned int? |
| 1601 | if (ResultVal.isIntN(IntSize)) { |
| 1602 | // Does it fit in a signed int? |
| 1603 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1604 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1605 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1606 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1607 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1608 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1609 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1610 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1611 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1612 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1613 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1614 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | // Does it fit in a unsigned long? |
| 1616 | if (ResultVal.isIntN(LongSize)) { |
| 1617 | // Does it fit in a signed long? |
| 1618 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1619 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1621 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1622 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1623 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1626 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1627 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1628 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1629 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | // Does it fit in a unsigned long long? |
| 1631 | if (ResultVal.isIntN(LongLongSize)) { |
| 1632 | // Does it fit in a signed long long? |
| 1633 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1634 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1636 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1637 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1641 | // If we still couldn't decide a type, we probably have something that |
| 1642 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1643 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1645 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1646 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1649 | if (ResultVal.getBitWidth() != Width) |
| 1650 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1651 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1652 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1655 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1656 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1658 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1659 | |
| 1660 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1663 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1664 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1665 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1666 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1667 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1671 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1672 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1673 | SourceLocation OpLoc, |
| 1674 | const SourceRange &ExprRange, |
| 1675 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1676 | if (exprType->isDependentType()) |
| 1677 | return false; |
| 1678 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1679 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1680 | // the result is the size of the referenced type." |
| 1681 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1682 | // result shall be the alignment of the referenced type." |
| 1683 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1684 | exprType = Ref->getPointeeType(); |
| 1685 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1686 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1687 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1688 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1689 | if (isSizeof) |
| 1690 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1691 | return false; |
| 1692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1694 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1695 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1696 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1697 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1698 | return false; |
| 1699 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1701 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 1702 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 1703 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1704 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1706 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1707 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1708 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1709 | << exprType << isSizeof << ExprRange; |
| 1710 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1713 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1716 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1717 | const SourceRange &ExprRange) { |
| 1718 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1719 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1721 | if (isa<DeclRefExpr>(E)) |
| 1722 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Cannot know anything else if the expression is dependent. |
| 1725 | if (E->isTypeDependent()) |
| 1726 | return false; |
| 1727 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1728 | if (E->getBitField()) { |
| 1729 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1730 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1731 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1732 | |
| 1733 | // Alignment of a field access is always okay, so long as it isn't a |
| 1734 | // bit-field. |
| 1735 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1736 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1737 | return false; |
| 1738 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1739 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1740 | } |
| 1741 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1742 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | Action::OwningExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1744 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1745 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1746 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1747 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1748 | return ExprError(); |
| 1749 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1750 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1751 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1752 | if (!T->isDependentType() && |
| 1753 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1754 | return ExprError(); |
| 1755 | |
| 1756 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1757 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1758 | Context.getSizeType(), OpLoc, |
| 1759 | R.getEnd())); |
| 1760 | } |
| 1761 | |
| 1762 | /// \brief Build a sizeof or alignof expression given an expression |
| 1763 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | Action::OwningExprResult |
| 1765 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1766 | bool isSizeOf, SourceRange R) { |
| 1767 | // Verify that the operand is valid. |
| 1768 | bool isInvalid = false; |
| 1769 | if (E->isTypeDependent()) { |
| 1770 | // Delay type-checking for type-dependent expressions. |
| 1771 | } else if (!isSizeOf) { |
| 1772 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1773 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1774 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1775 | isInvalid = true; |
| 1776 | } else { |
| 1777 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1778 | } |
| 1779 | |
| 1780 | if (isInvalid) |
| 1781 | return ExprError(); |
| 1782 | |
| 1783 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1784 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1785 | Context.getSizeType(), OpLoc, |
| 1786 | R.getEnd())); |
| 1787 | } |
| 1788 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1789 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1790 | /// the same for @c alignof and @c __alignof |
| 1791 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1792 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1793 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1794 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1795 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1796 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1798 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1799 | TypeSourceInfo *TInfo; |
| 1800 | (void) GetTypeFromParser(TyOrEx, &TInfo); |
| 1801 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1803 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1804 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1805 | Action::OwningExprResult Result |
| 1806 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1807 | |
| 1808 | if (Result.isInvalid()) |
| 1809 | DeleteExpr(ArgEx); |
| 1810 | |
| 1811 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1814 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1815 | if (V->isTypeDependent()) |
| 1816 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1818 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1819 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1820 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1822 | // Otherwise they pass through real integer and floating point types here. |
| 1823 | if (V->getType()->isArithmeticType()) |
| 1824 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1826 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1827 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1828 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1829 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1833 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1834 | Action::OwningExprResult |
| 1835 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1836 | tok::TokenKind Kind, ExprArg Input) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1837 | UnaryOperator::Opcode Opc; |
| 1838 | switch (Kind) { |
| 1839 | default: assert(0 && "Unknown unary op!"); |
| 1840 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1841 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1842 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1843 | |
Eli Friedman | e4216e9 | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 1844 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1847 | Action::OwningExprResult |
| 1848 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1849 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1850 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1851 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1852 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1853 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1854 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1856 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1857 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1858 | Base.release(); |
| 1859 | Idx.release(); |
| 1860 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1861 | Context.DependentTy, RLoc)); |
| 1862 | } |
| 1863 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1865 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1866 | LHSExp->getType()->isEnumeralType() || |
| 1867 | RHSExp->getType()->isRecordType() || |
| 1868 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1869 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1872 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 1873 | } |
| 1874 | |
| 1875 | |
| 1876 | Action::OwningExprResult |
| 1877 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 1878 | ExprArg Idx, SourceLocation RLoc) { |
| 1879 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 1880 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 1881 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1882 | // Perform default conversions. |
| 1883 | DefaultFunctionArrayConversion(LHSExp); |
| 1884 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1886 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1888 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1889 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1890 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1891 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1892 | Expr *BaseExpr, *IndexExpr; |
| 1893 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1894 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1895 | BaseExpr = LHSExp; |
| 1896 | IndexExpr = RHSExp; |
| 1897 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1898 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1899 | BaseExpr = LHSExp; |
| 1900 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1901 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1902 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1903 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1904 | BaseExpr = RHSExp; |
| 1905 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1906 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1908 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1909 | BaseExpr = LHSExp; |
| 1910 | IndexExpr = RHSExp; |
| 1911 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1913 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1914 | // Handle the uncommon case of "123[Ptr]". |
| 1915 | BaseExpr = RHSExp; |
| 1916 | IndexExpr = LHSExp; |
| 1917 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1918 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1919 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1920 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1921 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1922 | // FIXME: need to deal with const... |
| 1923 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1924 | } else if (LHSTy->isArrayType()) { |
| 1925 | // If we see an array that wasn't promoted by |
| 1926 | // DefaultFunctionArrayConversion, it must be an array that |
| 1927 | // wasn't promoted because of the C90 rule that doesn't |
| 1928 | // allow promoting non-lvalue arrays. Warn, then |
| 1929 | // force the promotion here. |
| 1930 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1931 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1932 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 1933 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1934 | LHSTy = LHSExp->getType(); |
| 1935 | |
| 1936 | BaseExpr = LHSExp; |
| 1937 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1938 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1939 | } else if (RHSTy->isArrayType()) { |
| 1940 | // Same as previous, except for 123[f().a] case |
| 1941 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1942 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1943 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 1944 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1945 | RHSTy = RHSExp->getType(); |
| 1946 | |
| 1947 | BaseExpr = RHSExp; |
| 1948 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1949 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1950 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1951 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1952 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1953 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1954 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1955 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1956 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1957 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1958 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1960 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 1961 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 1962 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 1963 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 1964 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1965 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1967 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1968 | // incomplete types are not object types. |
| 1969 | if (ResultType->isFunctionType()) { |
| 1970 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1971 | << ResultType << BaseExpr->getSourceRange(); |
| 1972 | return ExprError(); |
| 1973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1975 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1977 | PDiag(diag::err_subscript_incomplete_type) |
| 1978 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1979 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1981 | // Diagnose bad cases where we step over interface counts. |
| 1982 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1983 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1984 | << ResultType << BaseExpr->getSourceRange(); |
| 1985 | return ExprError(); |
| 1986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1988 | Base.release(); |
| 1989 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1990 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1991 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1994 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1995 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1997 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 1998 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 1999 | // see FIXME there. |
| 2000 | // |
| 2001 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2002 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2003 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2004 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2005 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2006 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2007 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2008 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2009 | // special names that indicate a subset of exactly half the elements are |
| 2010 | // to be selected. |
| 2011 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2012 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2013 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2014 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 131f465 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 2015 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2016 | |
| 2017 | // Check that we've found one of the special components, or that the component |
| 2018 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2019 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2020 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2021 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2022 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2023 | do |
| 2024 | compStr++; |
| 2025 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2026 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2027 | do |
| 2028 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2029 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2030 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2031 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2032 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2033 | // We didn't get to the end of the string. This means the component names |
| 2034 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2035 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 2036 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2037 | return QualType(); |
| 2038 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2039 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2040 | // Ensure no component accessor exceeds the width of the vector type it |
| 2041 | // operates on. |
| 2042 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2043 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2044 | |
| 2045 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2046 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2047 | |
| 2048 | while (*compStr) { |
| 2049 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2050 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2051 | << baseType << SourceRange(CompLoc); |
| 2052 | return QualType(); |
| 2053 | } |
| 2054 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2055 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2056 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2057 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2058 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2059 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2060 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2061 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2062 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2063 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2064 | if (HexSwizzle) |
| 2065 | CompSize--; |
| 2066 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2067 | if (CompSize == 1) |
| 2068 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2069 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2070 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2071 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2072 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2073 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2074 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2075 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2076 | } |
| 2077 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2080 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2081 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2082 | const Selector &Sel, |
| 2083 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2085 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2086 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2087 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2088 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2090 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2091 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2093 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2094 | return D; |
| 2095 | } |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2099 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2100 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2101 | const Selector &Sel, |
| 2102 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2103 | // Check protocols on qualified interfaces. |
| 2104 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2105 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2106 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2107 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2108 | GDecl = PD; |
| 2109 | break; |
| 2110 | } |
| 2111 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2112 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2113 | GDecl = OMD; |
| 2114 | break; |
| 2115 | } |
| 2116 | } |
| 2117 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2118 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2119 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2120 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2121 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2122 | if (GDecl) |
| 2123 | return GDecl; |
| 2124 | } |
| 2125 | } |
| 2126 | return GDecl; |
| 2127 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2128 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2129 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2130 | Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType, |
| 2131 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2132 | const CXXScopeSpec &SS, |
| 2133 | NamedDecl *FirstQualifierInScope, |
| 2134 | DeclarationName Name, SourceLocation NameLoc, |
| 2135 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2136 | Expr *BaseExpr = Base.takeAs<Expr>(); |
| 2137 | |
| 2138 | // Even in dependent contexts, try to diagnose base expressions with |
| 2139 | // obviously wrong types, e.g.: |
| 2140 | // |
| 2141 | // T* t; |
| 2142 | // t.f; |
| 2143 | // |
| 2144 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2145 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2146 | // allows this, while still reporting an error if T is a struct pointer. |
| 2147 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2148 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2149 | if (PT && (!getLangOptions().ObjC1 || |
| 2150 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2151 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2152 | Diag(NameLoc, diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2153 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2154 | return ExprError(); |
| 2155 | } |
| 2156 | } |
| 2157 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2158 | assert(BaseType->isDependentType()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2161 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2162 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2163 | IsArrow, OpLoc, |
| 2164 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 2165 | SS.getRange(), |
| 2166 | FirstQualifierInScope, |
| 2167 | Name, NameLoc, |
| 2168 | TemplateArgs)); |
| 2169 | } |
| 2170 | |
| 2171 | /// We know that the given qualified member reference points only to |
| 2172 | /// declarations which do not belong to the static type of the base |
| 2173 | /// expression. Diagnose the problem. |
| 2174 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2175 | Expr *BaseExpr, |
| 2176 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2177 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2178 | const LookupResult &R) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2179 | // If this is an implicit member access, use a different set of |
| 2180 | // diagnostics. |
| 2181 | if (!BaseExpr) |
| 2182 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2183 | |
| 2184 | // FIXME: this is an exceedingly lame diagnostic for some of the more |
| 2185 | // complicated cases here. |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2186 | DeclContext *DC = R.getRepresentativeDecl()->getDeclContext(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2187 | SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual) |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2188 | << SS.getRange() << DC << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | // Check whether the declarations we found through a nested-name |
| 2192 | // specifier in a member expression are actually members of the base |
| 2193 | // type. The restriction here is: |
| 2194 | // |
| 2195 | // C++ [expr.ref]p2: |
| 2196 | // ... In these cases, the id-expression shall name a |
| 2197 | // member of the class or of one of its base classes. |
| 2198 | // |
| 2199 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2200 | // an unrelated class, and for us to find an overload set including |
| 2201 | // decls from classes which are not superclasses, as long as the decl |
| 2202 | // we actually pick through overload resolution is from a superclass. |
| 2203 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2204 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2205 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2206 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2207 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2208 | if (!BaseRT) { |
| 2209 | // We can't check this yet because the base type is still |
| 2210 | // dependent. |
| 2211 | assert(BaseType->isDependentType()); |
| 2212 | return false; |
| 2213 | } |
| 2214 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2215 | |
| 2216 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2217 | // If this is an implicit member reference and we find a |
| 2218 | // non-instance member, it's not an error. |
| 2219 | if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl())) |
| 2220 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2221 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2222 | // Note that we use the DC of the decl, not the underlying decl. |
| 2223 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext()); |
| 2224 | while (RecordD->isAnonymousStructOrUnion()) |
| 2225 | RecordD = cast<CXXRecordDecl>(RecordD->getParent()); |
| 2226 | |
| 2227 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| 2228 | MemberRecord.insert(RecordD->getCanonicalDecl()); |
| 2229 | |
| 2230 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2231 | return false; |
| 2232 | } |
| 2233 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2234 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2235 | return true; |
| 2236 | } |
| 2237 | |
| 2238 | static bool |
| 2239 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2240 | SourceRange BaseRange, const RecordType *RTy, |
| 2241 | SourceLocation OpLoc, const CXXScopeSpec &SS) { |
| 2242 | RecordDecl *RDecl = RTy->getDecl(); |
| 2243 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 2244 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2245 | << BaseRange)) |
| 2246 | return true; |
| 2247 | |
| 2248 | DeclContext *DC = RDecl; |
| 2249 | if (SS.isSet()) { |
| 2250 | // If the member name was a qualified-id, look into the |
| 2251 | // nested-name-specifier. |
| 2252 | DC = SemaRef.computeDeclContext(SS, false); |
| 2253 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2254 | if (SemaRef.RequireCompleteDeclContext(SS)) { |
| 2255 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2256 | << SS.getRange() << DC; |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2260 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2261 | |
| 2262 | if (!isa<TypeDecl>(DC)) { |
| 2263 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2264 | << DC << SS.getRange(); |
| 2265 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2269 | // The record definition is complete, now look up the member. |
| 2270 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2271 | |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2276 | Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2277 | SourceLocation OpLoc, bool IsArrow, |
| 2278 | const CXXScopeSpec &SS, |
| 2279 | NamedDecl *FirstQualifierInScope, |
| 2280 | DeclarationName Name, SourceLocation NameLoc, |
| 2281 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2282 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2283 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2284 | if (BaseType->isDependentType() || |
| 2285 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2286 | return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2287 | IsArrow, OpLoc, |
| 2288 | SS, FirstQualifierInScope, |
| 2289 | Name, NameLoc, |
| 2290 | TemplateArgs); |
| 2291 | |
| 2292 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2293 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2294 | // Implicit member accesses. |
| 2295 | if (!Base) { |
| 2296 | QualType RecordTy = BaseType; |
| 2297 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2298 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2299 | RecordTy->getAs<RecordType>(), |
| 2300 | OpLoc, SS)) |
| 2301 | return ExprError(); |
| 2302 | |
| 2303 | // Explicit member accesses. |
| 2304 | } else { |
| 2305 | OwningExprResult Result = |
| 2306 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 2307 | SS, FirstQualifierInScope, |
| 2308 | /*ObjCImpDecl*/ DeclPtrTy()); |
| 2309 | |
| 2310 | if (Result.isInvalid()) { |
| 2311 | Owned(Base); |
| 2312 | return ExprError(); |
| 2313 | } |
| 2314 | |
| 2315 | if (Result.get()) |
| 2316 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2319 | return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType, |
| 2320 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2324 | Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType, |
| 2325 | SourceLocation OpLoc, bool IsArrow, |
| 2326 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2327 | LookupResult &R, |
| 2328 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2329 | Expr *BaseExpr = Base.takeAs<Expr>(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2330 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2331 | if (IsArrow) { |
| 2332 | assert(BaseType->isPointerType()); |
| 2333 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2334 | } |
| 2335 | |
| 2336 | NestedNameSpecifier *Qualifier = |
| 2337 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 2338 | DeclarationName MemberName = R.getLookupName(); |
| 2339 | SourceLocation MemberLoc = R.getNameLoc(); |
| 2340 | |
| 2341 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2342 | return ExprError(); |
| 2343 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2344 | if (R.empty()) { |
| 2345 | // Rederive where we looked up. |
| 2346 | DeclContext *DC = (SS.isSet() |
| 2347 | ? computeDeclContext(SS, false) |
| 2348 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2349 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2350 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2351 | << MemberName << DC |
| 2352 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2353 | return ExprError(); |
| 2354 | } |
| 2355 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2356 | // Diagnose qualified lookups that find only declarations from a |
| 2357 | // non-base type. Note that it's okay for lookup to find |
| 2358 | // declarations from a non-base type as long as those aren't the |
| 2359 | // ones picked by overload resolution. |
| 2360 | if (SS.isSet() && CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2361 | return ExprError(); |
| 2362 | |
| 2363 | // Construct an unresolved result if we in fact got an unresolved |
| 2364 | // result. |
| 2365 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2366 | bool Dependent = |
| 2367 | R.isUnresolvableResult() || |
| 2368 | UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2369 | |
| 2370 | UnresolvedMemberExpr *MemExpr |
| 2371 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2372 | R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2373 | BaseExpr, BaseExprType, |
| 2374 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2375 | Qualifier, SS.getRange(), |
| 2376 | MemberName, MemberLoc, |
| 2377 | TemplateArgs); |
| 2378 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2379 | MemExpr->addDecl(*I); |
| 2380 | |
| 2381 | return Owned(MemExpr); |
| 2382 | } |
| 2383 | |
| 2384 | assert(R.isSingleResult()); |
| 2385 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2386 | |
| 2387 | // FIXME: diagnose the presence of template arguments now. |
| 2388 | |
| 2389 | // If the decl being referenced had an error, return an error for this |
| 2390 | // sub-expr without emitting another error, in order to avoid cascading |
| 2391 | // error cases. |
| 2392 | if (MemberDecl->isInvalidDecl()) |
| 2393 | return ExprError(); |
| 2394 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2395 | // Handle the implicit-member-access case. |
| 2396 | if (!BaseExpr) { |
| 2397 | // If this is not an instance member, convert to a non-member access. |
| 2398 | if (!IsInstanceMember(MemberDecl)) |
| 2399 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl); |
| 2400 | |
| 2401 | BaseExpr = new (Context) CXXThisExpr(SourceLocation(), BaseExprType); |
| 2402 | } |
| 2403 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2404 | bool ShouldCheckUse = true; |
| 2405 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2406 | // Don't diagnose the use of a virtual member function unless it's |
| 2407 | // explicitly qualified. |
| 2408 | if (MD->isVirtual() && !SS.isSet()) |
| 2409 | ShouldCheckUse = false; |
| 2410 | } |
| 2411 | |
| 2412 | // Check the use of this member. |
| 2413 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2414 | Owned(BaseExpr); |
| 2415 | return ExprError(); |
| 2416 | } |
| 2417 | |
| 2418 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2419 | // We may have found a field within an anonymous union or struct |
| 2420 | // (C++ [class.union]). |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2421 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2422 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2423 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2424 | BaseExpr, OpLoc); |
| 2425 | |
| 2426 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2427 | QualType MemberType = FD->getType(); |
| 2428 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2429 | MemberType = Ref->getPointeeType(); |
| 2430 | else { |
| 2431 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2432 | BaseQuals.removeObjCGCAttr(); |
| 2433 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2434 | |
| 2435 | Qualifiers MemberQuals |
| 2436 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2437 | |
| 2438 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2439 | if (Combined != MemberQuals) |
| 2440 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2441 | } |
| 2442 | |
| 2443 | MarkDeclarationReferenced(MemberLoc, FD); |
| 2444 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2445 | return ExprError(); |
| 2446 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2447 | FD, MemberLoc, MemberType)); |
| 2448 | } |
| 2449 | |
| 2450 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2451 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2452 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2453 | Var, MemberLoc, |
| 2454 | Var->getType().getNonReferenceType())); |
| 2455 | } |
| 2456 | |
| 2457 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2458 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2459 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2460 | MemberFn, MemberLoc, |
| 2461 | MemberFn->getType())); |
| 2462 | } |
| 2463 | |
| 2464 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2465 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2466 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2467 | Enum, MemberLoc, Enum->getType())); |
| 2468 | } |
| 2469 | |
| 2470 | Owned(BaseExpr); |
| 2471 | |
| 2472 | if (isa<TypeDecl>(MemberDecl)) |
| 2473 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2474 | << MemberName << int(IsArrow)); |
| 2475 | |
| 2476 | // We found a declaration kind that we didn't expect. This is a |
| 2477 | // generic error message that tells the user that she can't refer |
| 2478 | // to this member with '.' or '->'. |
| 2479 | return ExprError(Diag(MemberLoc, |
| 2480 | diag::err_typecheck_member_reference_unknown) |
| 2481 | << MemberName << int(IsArrow)); |
| 2482 | } |
| 2483 | |
| 2484 | /// Look up the given member of the given non-type-dependent |
| 2485 | /// expression. This can return in one of two ways: |
| 2486 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2487 | /// assume that lookup was performed and the results written into |
| 2488 | /// the provided structure. It will take over from there. |
| 2489 | /// * Otherwise, the returned expression will be produced in place of |
| 2490 | /// an ordinary member expression. |
| 2491 | /// |
| 2492 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2493 | /// fixed for ObjC++. |
| 2494 | Sema::OwningExprResult |
| 2495 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2496 | bool &IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2497 | const CXXScopeSpec &SS, |
| 2498 | NamedDecl *FirstQualifierInScope, |
| 2499 | DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2500 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2502 | // Perform default conversions. |
| 2503 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2504 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2505 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2506 | assert(!BaseType->isDependentType()); |
| 2507 | |
| 2508 | DeclarationName MemberName = R.getLookupName(); |
| 2509 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2510 | |
| 2511 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2512 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2513 | // function. Suggest the addition of those parentheses, build the |
| 2514 | // call, and continue on. |
| 2515 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 2516 | if (const FunctionProtoType *Fun |
| 2517 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 2518 | QualType ResultTy = Fun->getResultType(); |
| 2519 | if (Fun->getNumArgs() == 0 && |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2520 | ((!IsArrow && ResultTy->isRecordType()) || |
| 2521 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2522 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 2523 | ->isRecordType()))) { |
| 2524 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2525 | Diag(Loc, diag::err_member_reference_needs_call) |
| 2526 | << QualType(Fun, 0) |
| 2527 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2528 | |
| 2529 | OwningExprResult NewBase |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2530 | = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc, |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2531 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 2532 | if (NewBase.isInvalid()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2533 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2534 | |
| 2535 | BaseExpr = NewBase.takeAs<Expr>(); |
| 2536 | DefaultFunctionArrayConversion(BaseExpr); |
| 2537 | BaseType = BaseExpr->getType(); |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2542 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2543 | // use that. |
| 2544 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2545 | if (IsArrow) { |
| 2546 | // Handle the following exceptional case PObj->isa. |
| 2547 | if (const ObjCObjectPointerType *OPT = |
| 2548 | BaseType->getAs<ObjCObjectPointerType>()) { |
| 2549 | if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && |
| 2550 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2551 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 2552 | Context.getObjCClassType())); |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2553 | } |
| 2554 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2555 | // We have an 'id' type. Rather than fall through, we check if this |
| 2556 | // is a reference to 'isa'. |
| 2557 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2558 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2559 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2560 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2561 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2562 | |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 2563 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2564 | // use that. |
| 2565 | if (Context.isObjCSelType(BaseType)) { |
| 2566 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 2567 | // is a reference to 'sel_id'. |
| 2568 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 2569 | BaseType = Context.ObjCSelRedefinitionType; |
| 2570 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
| 2571 | } |
| 2572 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2573 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2574 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2575 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2576 | // Handle properties on ObjC 'Class' types. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2577 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2578 | // Also must look for a getter name which uses property syntax. |
| 2579 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2580 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 2581 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2582 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2583 | ObjCMethodDecl *Getter; |
| 2584 | // FIXME: need to also look locally in the implementation. |
| 2585 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2586 | // Check the use of this method. |
| 2587 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2588 | return ExprError(); |
| 2589 | } |
| 2590 | // If we found a getter then this may be a valid dot-reference, we |
| 2591 | // will look for the matching setter, in case it is needed. |
| 2592 | Selector SetterSel = |
| 2593 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2594 | PP.getSelectorTable(), Member); |
| 2595 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2596 | if (!Setter) { |
| 2597 | // If this reference is in an @implementation, also check for 'private' |
| 2598 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2599 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2600 | } |
| 2601 | // Look through local category implementations associated with the class. |
| 2602 | if (!Setter) |
| 2603 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 2604 | |
| 2605 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2606 | return ExprError(); |
| 2607 | |
| 2608 | if (Getter || Setter) { |
| 2609 | QualType PType; |
| 2610 | |
| 2611 | if (Getter) |
| 2612 | PType = Getter->getResultType(); |
| 2613 | else |
| 2614 | // Get the expression type from Setter's incoming parameter. |
| 2615 | PType = (*(Setter->param_end() -1))->getType(); |
| 2616 | // FIXME: we must check that the setter has property type. |
| 2617 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 2618 | PType, |
| 2619 | Setter, MemberLoc, BaseExpr)); |
| 2620 | } |
| 2621 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2622 | << MemberName << BaseType); |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | if (BaseType->isObjCClassType() && |
| 2627 | BaseType != Context.ObjCClassRedefinitionType) { |
| 2628 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2629 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2632 | if (IsArrow) { |
| 2633 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2634 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2635 | else if (BaseType->isObjCObjectPointerType()) |
| 2636 | ; |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2637 | else if (BaseType->isRecordType()) { |
| 2638 | // Recover from arrow accesses to records, e.g.: |
| 2639 | // struct MyRecord foo; |
| 2640 | // foo->bar |
| 2641 | // This is actually well-formed in C++ if MyRecord has an |
| 2642 | // overloaded operator->, but that should have been dealt with |
| 2643 | // by now. |
| 2644 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2645 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2646 | << CodeModificationHint::CreateReplacement(OpLoc, "."); |
| 2647 | IsArrow = false; |
| 2648 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2649 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 2650 | << BaseType << BaseExpr->getSourceRange(); |
| 2651 | return ExprError(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2652 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2653 | } else { |
| 2654 | // Recover from dot accesses to pointers, e.g.: |
| 2655 | // type *foo; |
| 2656 | // foo.bar |
| 2657 | // This is actually well-formed in two cases: |
| 2658 | // - 'type' is an Objective C type |
| 2659 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 2660 | // the appropriate pointer type |
| 2661 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 2662 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 2663 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 2664 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2665 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2666 | << CodeModificationHint::CreateReplacement(OpLoc, "->"); |
| 2667 | BaseType = PT->getPointeeType(); |
| 2668 | IsArrow = true; |
| 2669 | } |
| 2670 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2671 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2672 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2673 | // Handle field access to simple records. This also handles access |
| 2674 | // to fields of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2675 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2676 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 2677 | RTy, OpLoc, SS)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2678 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2679 | return Owned((Expr*) 0); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2680 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2682 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2683 | // into a record type was handled above, any destructor we see here is a |
| 2684 | // pseudo-destructor. |
| 2685 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2686 | // C++ [expr.pseudo]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | // The left hand side of the dot operator shall be of scalar type. The |
| 2688 | // left hand side of the arrow operator shall be of pointer to scalar |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2689 | // type. |
| 2690 | if (!BaseType->isScalarType()) |
| 2691 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2692 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2694 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2695 | // same as the object type. |
| 2696 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2697 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2698 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2699 | << BaseType << MemberName.getCXXNameType() |
| 2700 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
| 2702 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2703 | // the form |
| 2704 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2706 | // |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2707 | // shall designate the same scalar type. |
| 2708 | // |
| 2709 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2710 | // isn't checked here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2712 | // FIXME: We've lost the precise spelling of the type by going through |
| 2713 | // DeclarationName. Can we do better? |
| 2714 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2715 | IsArrow, OpLoc, |
| 2716 | (NestedNameSpecifier *) SS.getScopeRep(), |
| 2717 | SS.getRange(), |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2718 | MemberName.getCXXNameType(), |
| 2719 | MemberLoc)); |
| 2720 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2722 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2723 | // (*Obj).ivar. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2724 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2725 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2726 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | const ObjCInterfaceType *IFaceT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2728 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2729 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2730 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2731 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2732 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2733 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2734 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2735 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2736 | if (IV) { |
| 2737 | // If the decl being referenced had an error, return an error for this |
| 2738 | // sub-expr without emitting another error, in order to avoid cascading |
| 2739 | // error cases. |
| 2740 | if (IV->isInvalidDecl()) |
| 2741 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2742 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2743 | // Check whether we can reference this field. |
| 2744 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2745 | return ExprError(); |
| 2746 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2747 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2748 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2749 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2750 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2751 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2752 | // Case of a c-function declared inside an objc implementation. |
| 2753 | // FIXME: For a c-style function nested inside an objc implementation |
| 2754 | // class, there is no implementation context available, so we pass |
| 2755 | // down the context as argument to this routine. Ideally, this context |
| 2756 | // need be passed down in the AST node and somehow calculated from the |
| 2757 | // AST for a function decl. |
| 2758 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2760 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2761 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2762 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2763 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2764 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | |
| 2767 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2768 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2769 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2770 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2771 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2772 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2773 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2775 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2776 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2777 | |
| 2778 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2779 | MemberLoc, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2780 | IsArrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2781 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2782 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2783 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2784 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2785 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2786 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2787 | // Handle properties on 'id' and qualified "id". |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2788 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 2789 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2790 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2791 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2793 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2794 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2795 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2796 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2797 | // Check the use of this declaration |
| 2798 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2799 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2801 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2802 | MemberLoc, BaseExpr)); |
| 2803 | } |
| 2804 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2805 | // Check the use of this method. |
| 2806 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2807 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2809 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | OMD->getResultType(), |
| 2811 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2812 | NULL, 0)); |
| 2813 | } |
| 2814 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2815 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2816 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2817 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2818 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2819 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2820 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2821 | const ObjCObjectPointerType *OPT; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2822 | if (!IsArrow && (OPT = BaseType->getAsObjCInterfacePointerType())) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2823 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2824 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2825 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2827 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2828 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2829 | // Check whether we can reference this property. |
| 2830 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2831 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2832 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2833 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2834 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2835 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2836 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2837 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2838 | MemberLoc, BaseExpr)); |
| 2839 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2840 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2841 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2842 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2843 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2844 | // Check whether we can reference this property. |
| 2845 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2846 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2847 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2848 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2849 | MemberLoc, BaseExpr)); |
| 2850 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2851 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2852 | // selector is implemented. |
| 2853 | |
| 2854 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2855 | // shared with the code in ActOnInstanceMessage. |
| 2856 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2857 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2858 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2859 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2860 | // If this reference is in an @implementation, check for 'private' methods. |
| 2861 | if (!Getter) |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2862 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2863 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2864 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2865 | if (!Getter) |
| 2866 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2867 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2868 | // Check if we can reference this property. |
| 2869 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2870 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2871 | } |
| 2872 | // If we found a getter then this may be a valid dot-reference, we |
| 2873 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | Selector SetterSel = |
| 2875 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2876 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2877 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2878 | if (!Setter) { |
| 2879 | // If this reference is in an @implementation, also check for 'private' |
| 2880 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2881 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2882 | } |
| 2883 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2884 | if (!Setter) |
| 2885 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2886 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2887 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2888 | return ExprError(); |
| 2889 | |
| 2890 | if (Getter || Setter) { |
| 2891 | QualType PType; |
| 2892 | |
| 2893 | if (Getter) |
| 2894 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2895 | else |
| 2896 | // Get the expression type from Setter's incoming parameter. |
| 2897 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2898 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2899 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2900 | Setter, MemberLoc, BaseExpr)); |
| 2901 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2902 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2903 | << MemberName << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2906 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2907 | if (!IsArrow && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2908 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2909 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2910 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2911 | Context.getObjCClassType())); |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2913 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2914 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2915 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2916 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2917 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2918 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2919 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2920 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2921 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2922 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2923 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2924 | << BaseType << BaseExpr->getSourceRange(); |
| 2925 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2926 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2929 | static Sema::OwningExprResult DiagnoseDtorReference(Sema &SemaRef, |
| 2930 | SourceLocation NameLoc, |
| 2931 | Sema::ExprArg MemExpr) { |
| 2932 | Expr *E = (Expr *) MemExpr.get(); |
| 2933 | SourceLocation ExpectedLParenLoc = SemaRef.PP.getLocForEndOfToken(NameLoc); |
| 2934 | SemaRef.Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2935 | << isa<CXXPseudoDestructorExpr>(E) |
| 2936 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 2937 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2938 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, |
| 2939 | move(MemExpr), |
| 2940 | /*LPLoc*/ ExpectedLParenLoc, |
| 2941 | Sema::MultiExprArg(SemaRef, 0, 0), |
| 2942 | /*CommaLocs*/ 0, |
| 2943 | /*RPLoc*/ ExpectedLParenLoc); |
| 2944 | } |
| 2945 | |
| 2946 | /// The main callback when the parser finds something like |
| 2947 | /// expression . [nested-name-specifier] identifier |
| 2948 | /// expression -> [nested-name-specifier] identifier |
| 2949 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 2950 | /// possibilities, including destructor and operator references. |
| 2951 | /// |
| 2952 | /// \param OpKind either tok::arrow or tok::period |
| 2953 | /// \param HasTrailingLParen whether the next token is '(', which |
| 2954 | /// is used to diagnose mis-uses of special members that can |
| 2955 | /// only be called |
| 2956 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 2957 | /// this is an ugly hack around the fact that ObjC @implementations |
| 2958 | /// aren't properly put in the context chain |
| 2959 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg BaseArg, |
| 2960 | SourceLocation OpLoc, |
| 2961 | tok::TokenKind OpKind, |
| 2962 | const CXXScopeSpec &SS, |
| 2963 | UnqualifiedId &Id, |
| 2964 | DeclPtrTy ObjCImpDecl, |
| 2965 | bool HasTrailingLParen) { |
| 2966 | if (SS.isSet() && SS.isInvalid()) |
| 2967 | return ExprError(); |
| 2968 | |
| 2969 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 2970 | |
| 2971 | // Decompose the name into its component parts. |
| 2972 | DeclarationName Name; |
| 2973 | SourceLocation NameLoc; |
| 2974 | const TemplateArgumentListInfo *TemplateArgs; |
| 2975 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 2976 | Name, NameLoc, TemplateArgs); |
| 2977 | |
| 2978 | bool IsArrow = (OpKind == tok::arrow); |
| 2979 | |
| 2980 | NamedDecl *FirstQualifierInScope |
| 2981 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 2982 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 2983 | |
| 2984 | // This is a postfix expression, so get rid of ParenListExprs. |
| 2985 | BaseArg = MaybeConvertParenListExprToParenExpr(S, move(BaseArg)); |
| 2986 | |
| 2987 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2988 | OwningExprResult Result(*this); |
| 2989 | if (Base->getType()->isDependentType()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2990 | Result = ActOnDependentMemberExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2991 | IsArrow, OpLoc, |
| 2992 | SS, FirstQualifierInScope, |
| 2993 | Name, NameLoc, |
| 2994 | TemplateArgs); |
| 2995 | } else { |
| 2996 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
| 2997 | if (TemplateArgs) { |
| 2998 | // Re-use the lookup done for the template name. |
| 2999 | DecomposeTemplateName(R, Id); |
| 3000 | } else { |
| 3001 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 3002 | SS, FirstQualifierInScope, |
| 3003 | ObjCImpDecl); |
| 3004 | |
| 3005 | if (Result.isInvalid()) { |
| 3006 | Owned(Base); |
| 3007 | return ExprError(); |
| 3008 | } |
| 3009 | |
| 3010 | if (Result.get()) { |
| 3011 | // The only way a reference to a destructor can be used is to |
| 3012 | // immediately call it, which falls into this case. If the |
| 3013 | // next token is not a '(', produce a diagnostic and build the |
| 3014 | // call now. |
| 3015 | if (!HasTrailingLParen && |
| 3016 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| 3017 | return DiagnoseDtorReference(*this, NameLoc, move(Result)); |
| 3018 | |
| 3019 | return move(Result); |
| 3020 | } |
| 3021 | } |
| 3022 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3023 | Result = BuildMemberReferenceExpr(ExprArg(*this, Base), Base->getType(), |
| 3024 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3030 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 3031 | FunctionDecl *FD, |
| 3032 | ParmVarDecl *Param) { |
| 3033 | if (Param->hasUnparsedDefaultArg()) { |
| 3034 | Diag (CallLoc, |
| 3035 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3036 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3038 | diag::note_default_argument_declared_here); |
| 3039 | } else { |
| 3040 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3041 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 3042 | |
| 3043 | // Instantiate the expression. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3044 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3045 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3047 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3048 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3049 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3050 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3052 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | |
| 3054 | if (SetParamDefaultArgument(Param, move(Result), |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3055 | /*FIXME:EqualLoc*/ |
| 3056 | UninstExpr->getSourceRange().getBegin())) |
| 3057 | return ExprError(); |
| 3058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3060 | // If the default expression creates temporaries, we need to |
| 3061 | // push them to the current stack of expression temporaries so they'll |
| 3062 | // be properly destroyed. |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 3063 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3064 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | // We already type-checked the argument, so we know it works. |
| 3068 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 3069 | } |
| 3070 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3071 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3072 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3073 | /// function prototype Proto. Call is the call expression itself, and |
| 3074 | /// Fn is the function expression. For a C++ member function, this |
| 3075 | /// routine does not attempt to convert the object argument. Returns |
| 3076 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3077 | bool |
| 3078 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3079 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3080 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3081 | Expr **Args, unsigned NumArgs, |
| 3082 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3083 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3084 | // assignment, to the types of the corresponding parameter, ... |
| 3085 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3086 | bool Invalid = false; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3088 | // If too few arguments are available (and we don't have default |
| 3089 | // arguments for the remaining parameters), don't make the call. |
| 3090 | if (NumArgs < NumArgsInProto) { |
| 3091 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3092 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 3093 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3094 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | // If too many are passed and not variadic, error on the extras and drop |
| 3098 | // them. |
| 3099 | if (NumArgs > NumArgsInProto) { |
| 3100 | if (!Proto->isVariadic()) { |
| 3101 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3102 | diag::err_typecheck_call_too_many_args) |
| 3103 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 3104 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3105 | Args[NumArgs-1]->getLocEnd()); |
| 3106 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3107 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3108 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3109 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3110 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3111 | llvm::SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3112 | VariadicCallType CallType = |
| 3113 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3114 | if (Fn->getType()->isBlockPointerType()) |
| 3115 | CallType = VariadicBlock; // Block |
| 3116 | else if (isa<MemberExpr>(Fn)) |
| 3117 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3118 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3119 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3120 | if (Invalid) |
| 3121 | return true; |
| 3122 | unsigned TotalNumArgs = AllArgs.size(); |
| 3123 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3124 | Call->setArg(i, AllArgs[i]); |
| 3125 | |
| 3126 | return false; |
| 3127 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3128 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3129 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3130 | FunctionDecl *FDecl, |
| 3131 | const FunctionProtoType *Proto, |
| 3132 | unsigned FirstProtoArg, |
| 3133 | Expr **Args, unsigned NumArgs, |
| 3134 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3135 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3136 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3137 | unsigned NumArgsToCheck = NumArgs; |
| 3138 | bool Invalid = false; |
| 3139 | if (NumArgs != NumArgsInProto) |
| 3140 | // Use default arguments for missing arguments |
| 3141 | NumArgsToCheck = NumArgsInProto; |
| 3142 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3143 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3144 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3145 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3146 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3147 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3148 | if (ArgIx < NumArgs) { |
| 3149 | Arg = Args[ArgIx++]; |
| 3150 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3151 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3152 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3153 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3154 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3155 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3156 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3157 | // Pass the argument. |
| 3158 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 3159 | return true; |
Anders Carlsson | 03d8ed4 | 2009-11-13 04:34:45 +0000 | [diff] [blame] | 3160 | |
Anders Carlsson | 4b3cbea | 2009-11-13 17:04:35 +0000 | [diff] [blame] | 3161 | if (!ProtoArgType->isReferenceType()) |
| 3162 | Arg = MaybeBindToTemporary(Arg).takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3163 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3164 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3165 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3167 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3168 | if (ArgExpr.isInvalid()) |
| 3169 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3170 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3171 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3172 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3173 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3174 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3176 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3177 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3178 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3179 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3180 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 3181 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3182 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3183 | } |
| 3184 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3185 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3188 | /// \brief "Deconstruct" the function argument of a call expression to find |
| 3189 | /// the underlying declaration (if any), the name of the called function, |
| 3190 | /// whether argument-dependent lookup is available, whether it has explicit |
| 3191 | /// template arguments, etc. |
| 3192 | void Sema::DeconstructCallFunction(Expr *FnExpr, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3193 | llvm::SmallVectorImpl<NamedDecl*> &Fns, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3194 | DeclarationName &Name, |
| 3195 | NestedNameSpecifier *&Qualifier, |
| 3196 | SourceRange &QualifierRange, |
| 3197 | bool &ArgumentDependentLookup, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3198 | bool &Overloaded, |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3199 | bool &HasExplicitTemplateArguments, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3200 | TemplateArgumentListInfo &ExplicitTemplateArgs) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3201 | // Set defaults for all of the output parameters. |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3202 | Name = DeclarationName(); |
| 3203 | Qualifier = 0; |
| 3204 | QualifierRange = SourceRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3205 | ArgumentDependentLookup = false; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3206 | Overloaded = false; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3207 | HasExplicitTemplateArguments = false; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3209 | // If we're directly calling a function, get the appropriate declaration. |
| 3210 | // Also, in C++, keep track of whether we should perform argument-dependent |
| 3211 | // lookup and whether there were any explicitly-specified template arguments. |
| 3212 | while (true) { |
| 3213 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 3214 | FnExpr = IcExpr->getSubExpr(); |
| 3215 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3216 | FnExpr = PExpr->getSubExpr(); |
| 3217 | } else if (isa<UnaryOperator>(FnExpr) && |
| 3218 | cast<UnaryOperator>(FnExpr)->getOpcode() |
| 3219 | == UnaryOperator::AddrOf) { |
| 3220 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3221 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3222 | Fns.push_back(cast<NamedDecl>(DRExpr->getDecl())); |
| 3223 | ArgumentDependentLookup = false; |
| 3224 | if ((Qualifier = DRExpr->getQualifier())) |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3225 | QualifierRange = DRExpr->getQualifierRange(); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3226 | break; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3227 | } else if (UnresolvedLookupExpr *UnresLookup |
| 3228 | = dyn_cast<UnresolvedLookupExpr>(FnExpr)) { |
| 3229 | Name = UnresLookup->getName(); |
| 3230 | Fns.append(UnresLookup->decls_begin(), UnresLookup->decls_end()); |
| 3231 | ArgumentDependentLookup = UnresLookup->requiresADL(); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3232 | Overloaded = UnresLookup->isOverloaded(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3233 | if ((Qualifier = UnresLookup->getQualifier())) |
| 3234 | QualifierRange = UnresLookup->getQualifierRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 3235 | if (UnresLookup->hasExplicitTemplateArgs()) { |
| 3236 | HasExplicitTemplateArguments = true; |
| 3237 | UnresLookup->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3238 | } |
| 3239 | break; |
| 3240 | } else { |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3246 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3247 | /// This provides the location of the left/right parens and a list of comma |
| 3248 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3249 | Action::OwningExprResult |
| 3250 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 3251 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3252 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3253 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 3256 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 3258 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3259 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 3260 | assert(Fn && "no function call expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3262 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3263 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3264 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3265 | if (NumArgs > 0) { |
| 3266 | // Pseudo-destructor calls should not have any arguments. |
| 3267 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 3268 | << CodeModificationHint::CreateRemoval( |
| 3269 | SourceRange(Args[0]->getLocStart(), |
| 3270 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3272 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3273 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3275 | NumArgs = 0; |
| 3276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3278 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3279 | RParenLoc)); |
| 3280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3282 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3283 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3284 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3285 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3286 | bool Dependent = false; |
| 3287 | if (Fn->isTypeDependent()) |
| 3288 | Dependent = true; |
| 3289 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3290 | Dependent = true; |
| 3291 | |
| 3292 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3293 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3294 | Context.DependentTy, RParenLoc)); |
| 3295 | |
| 3296 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3297 | if (Fn->getType()->isRecordType()) |
| 3298 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 3299 | CommaLocs, RParenLoc)); |
| 3300 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3301 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3302 | |
| 3303 | // Determine whether this is a call to an unresolved member function. |
| 3304 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3305 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3306 | // an unresolved using declaration), it has to be an overloaded |
| 3307 | // function set, which means it must contain either multiple |
| 3308 | // declarations (all methods or method templates) or a single |
| 3309 | // method template. |
| 3310 | assert((MemE->getNumDecls() > 1) || |
| 3311 | isa<FunctionTemplateDecl>(*MemE->decls_begin())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3312 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3313 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3314 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3315 | CommaLocs, RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3318 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3319 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3320 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3321 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3322 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3323 | CommaLocs, RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3324 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3325 | |
| 3326 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3327 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3328 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 3329 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3330 | if (const FunctionProtoType *FPT = |
| 3331 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 3332 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3333 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3334 | ExprOwningPtr<CXXMemberCallExpr> |
| 3335 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3336 | NumArgs, ResultTy, |
| 3337 | RParenLoc)); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3338 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3339 | if (CheckCallReturnType(FPT->getResultType(), |
| 3340 | BO->getRHS()->getSourceRange().getBegin(), |
| 3341 | TheCall.get(), 0)) |
| 3342 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3343 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3344 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 3345 | RParenLoc)) |
| 3346 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3347 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3348 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 3349 | } |
| 3350 | return ExprError(Diag(Fn->getLocStart(), |
| 3351 | diag::err_typecheck_call_not_function) |
| 3352 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3353 | } |
| 3354 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3357 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3359 | // lookup and whether there were any explicitly-specified template arguments. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3360 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 3361 | DeclarationName UnqualifiedName; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3362 | bool Overloaded; |
| 3363 | bool ADL; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3364 | bool HasExplicitTemplateArgs = 0; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3365 | TemplateArgumentListInfo ExplicitTemplateArgs; |
Douglas Gregor | 9c6a0e9 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 3366 | NestedNameSpecifier *Qualifier = 0; |
| 3367 | SourceRange QualifierRange; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3368 | DeconstructCallFunction(Fn, Fns, UnqualifiedName, Qualifier, QualifierRange, |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3369 | ADL, Overloaded, HasExplicitTemplateArgs, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3370 | ExplicitTemplateArgs); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3371 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3372 | NamedDecl *NDecl; // the specific declaration we're calling, if applicable |
| 3373 | FunctionDecl *FDecl; // same, if it's known to be a function |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3374 | |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3375 | if (Overloaded || ADL) { |
| 3376 | #ifndef NDEBUG |
| 3377 | if (ADL) { |
| 3378 | // To do ADL, we must have found an unqualified name. |
| 3379 | assert(UnqualifiedName && "found no unqualified name for ADL"); |
| 3380 | |
| 3381 | // We don't perform ADL for implicit declarations of builtins. |
| 3382 | // Verify that this was correctly set up. |
| 3383 | if (Fns.size() == 1 && (FDecl = dyn_cast<FunctionDecl>(Fns[0])) && |
| 3384 | FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 3385 | assert(0 && "performing ADL for builtin"); |
| 3386 | |
| 3387 | // We don't perform ADL in C. |
| 3388 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 3389 | } |
| 3390 | |
| 3391 | if (Overloaded) { |
| 3392 | // To be overloaded, we must either have multiple functions or |
| 3393 | // at least one function template (which is effectively an |
| 3394 | // infinite set of functions). |
| 3395 | assert((Fns.size() > 1 || |
| 3396 | (Fns.size() == 1 && |
| 3397 | isa<FunctionTemplateDecl>(Fns[0]->getUnderlyingDecl()))) |
| 3398 | && "unrecognized overload situation"); |
| 3399 | } |
| 3400 | #endif |
| 3401 | |
| 3402 | FDecl = ResolveOverloadedCallFn(Fn, Fns, UnqualifiedName, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3403 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3404 | LParenLoc, Args, NumArgs, CommaLocs, |
| 3405 | RParenLoc, ADL); |
| 3406 | if (!FDecl) |
| 3407 | return ExprError(); |
| 3408 | |
| 3409 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 3410 | |
| 3411 | NDecl = FDecl; |
| 3412 | } else { |
| 3413 | assert(Fns.size() <= 1 && "overloaded without Overloaded flag"); |
| 3414 | if (Fns.empty()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3415 | NDecl = 0; |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 3416 | else { |
| 3417 | NDecl = Fns[0]; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3418 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3419 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3420 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3421 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3422 | } |
| 3423 | |
| 3424 | /// BuildCallExpr - Build a call to a resolved expression, i.e. an |
| 3425 | /// expression not of \p OverloadTy. The expression should |
| 3426 | /// unary-convert to an expression of function-pointer or |
| 3427 | /// block-pointer type. |
| 3428 | /// |
| 3429 | /// \param NDecl the declaration being called, if available |
| 3430 | Sema::OwningExprResult |
| 3431 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3432 | SourceLocation LParenLoc, |
| 3433 | Expr **Args, unsigned NumArgs, |
| 3434 | SourceLocation RParenLoc) { |
| 3435 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3436 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3437 | // Promote the function operand. |
| 3438 | UsualUnaryConversions(Fn); |
| 3439 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3440 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3441 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3442 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 3443 | Args, NumArgs, |
| 3444 | Context.BoolTy, |
| 3445 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3446 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3447 | const FunctionType *FuncT; |
| 3448 | if (!Fn->getType()->isBlockPointerType()) { |
| 3449 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3450 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3451 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3452 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3453 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3454 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3455 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3456 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3457 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3458 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3459 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3460 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3461 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3462 | << Fn->getType() << Fn->getSourceRange()); |
| 3463 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3464 | // Check for a valid return type |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3465 | if (CheckCallReturnType(FuncT->getResultType(), |
| 3466 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 3467 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3468 | return ExprError(); |
| 3469 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3470 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3471 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3473 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3474 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3475 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3476 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3477 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3478 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3480 | if (FDecl) { |
| 3481 | // Check if we have too few/too many template arguments, based |
| 3482 | // on our knowledge of the function definition. |
| 3483 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3484 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3485 | const FunctionProtoType *Proto = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3486 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3487 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3488 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3489 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3490 | } |
| 3491 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3494 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3495 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3496 | Expr *Arg = Args[i]; |
| 3497 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3498 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3499 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3500 | PDiag(diag::err_call_incomplete_argument) |
| 3501 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3502 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3503 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3504 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3505 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3507 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3508 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3509 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3510 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3511 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3512 | // Check for sentinels |
| 3513 | if (NDecl) |
| 3514 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3516 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3517 | if (FDecl) { |
| 3518 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3519 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3521 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3522 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3523 | } else if (NDecl) { |
| 3524 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3525 | return ExprError(); |
| 3526 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3527 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3528 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3531 | Action::OwningExprResult |
| 3532 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3533 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3534 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3535 | |
| 3536 | TypeSourceInfo *TInfo = 0; |
| 3537 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3538 | if (!TInfo) |
| 3539 | TInfo = Context.getTrivialTypeSourceInfo(literalType, LParenLoc); |
| 3540 | |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3541 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3542 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3543 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3544 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3545 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3546 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3547 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3548 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3549 | } else if (!literalType->isDependentType() && |
| 3550 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3551 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3553 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3554 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3556 | InitializedEntity Entity |
| 3557 | = InitializedEntity::InitializeTemporary(TInfo->getTypeLoc()); |
| 3558 | InitializationKind Kind |
| 3559 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| 3560 | /*IsCStyleCast=*/true); |
| 3561 | if (CheckInitializerTypes(literalExpr, literalType, Entity, Kind)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3562 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3563 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3564 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3565 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3566 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3567 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3568 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3569 | InitExpr.release(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3570 | |
| 3571 | // FIXME: Store the TInfo to preserve type information better. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3572 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3573 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3576 | Action::OwningExprResult |
| 3577 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3578 | SourceLocation RBraceLoc) { |
| 3579 | unsigned NumInit = initlist.size(); |
| 3580 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3581 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3582 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3583 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3584 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3585 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3586 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3587 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3588 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3591 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3592 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3593 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3594 | return CastExpr::CK_NoOp; |
| 3595 | |
| 3596 | if (SrcTy->hasPointerRepresentation()) { |
| 3597 | if (DestTy->hasPointerRepresentation()) |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 3598 | return DestTy->isObjCObjectPointerType() ? |
| 3599 | CastExpr::CK_AnyPointerToObjCPointerCast : |
| 3600 | CastExpr::CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3601 | if (DestTy->isIntegerType()) |
| 3602 | return CastExpr::CK_PointerToIntegral; |
| 3603 | } |
| 3604 | |
| 3605 | if (SrcTy->isIntegerType()) { |
| 3606 | if (DestTy->isIntegerType()) |
| 3607 | return CastExpr::CK_IntegralCast; |
| 3608 | if (DestTy->hasPointerRepresentation()) |
| 3609 | return CastExpr::CK_IntegralToPointer; |
| 3610 | if (DestTy->isRealFloatingType()) |
| 3611 | return CastExpr::CK_IntegralToFloating; |
| 3612 | } |
| 3613 | |
| 3614 | if (SrcTy->isRealFloatingType()) { |
| 3615 | if (DestTy->isRealFloatingType()) |
| 3616 | return CastExpr::CK_FloatingCast; |
| 3617 | if (DestTy->isIntegerType()) |
| 3618 | return CastExpr::CK_FloatingToIntegral; |
| 3619 | } |
| 3620 | |
| 3621 | // FIXME: Assert here. |
| 3622 | // assert(false && "Unhandled cast combination!"); |
| 3623 | return CastExpr::CK_Unknown; |
| 3624 | } |
| 3625 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3626 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3627 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3629 | CXXMethodDecl *& ConversionDecl, |
| 3630 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3631 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3632 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3633 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3634 | |
Eli Friedman | 199ea95 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3635 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3636 | |
| 3637 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3638 | // type needs to be scalar. |
| 3639 | if (castType->isVoidType()) { |
| 3640 | // Cast to void allows any expr type. |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3641 | Kind = CastExpr::CK_ToVoid; |
| 3642 | return false; |
| 3643 | } |
| 3644 | |
| 3645 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3646 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3647 | (castType->isStructureType() || castType->isUnionType())) { |
| 3648 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3649 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3650 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3651 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3652 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3653 | return false; |
| 3654 | } |
| 3655 | |
| 3656 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3657 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3658 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3659 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3660 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3661 | Field != FieldEnd; ++Field) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3662 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3663 | castExpr->getType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3664 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3665 | << castExpr->getSourceRange(); |
| 3666 | break; |
| 3667 | } |
| 3668 | } |
| 3669 | if (Field == FieldEnd) |
| 3670 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3671 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3672 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3673 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3674 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3675 | |
| 3676 | // Reject any other conversions to non-scalar types. |
| 3677 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3678 | << castType << castExpr->getSourceRange(); |
| 3679 | } |
| 3680 | |
| 3681 | if (!castExpr->getType()->isScalarType() && |
| 3682 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3683 | return Diag(castExpr->getLocStart(), |
| 3684 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3685 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3688 | if (castType->isExtVectorType()) |
| 3689 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3690 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3691 | if (castType->isVectorType()) |
| 3692 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3693 | if (castExpr->getType()->isVectorType()) |
| 3694 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3695 | |
| 3696 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3697 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3698 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3699 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3700 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3701 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3702 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3703 | QualType castExprType = castExpr->getType(); |
| 3704 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3705 | return Diag(castExpr->getLocStart(), |
| 3706 | diag::err_cast_pointer_from_non_pointer_int) |
| 3707 | << castExprType << castExpr->getSourceRange(); |
| 3708 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3709 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3710 | return Diag(castExpr->getLocStart(), |
| 3711 | diag::err_cast_pointer_to_non_pointer_int) |
| 3712 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3713 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3714 | |
| 3715 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3716 | return false; |
| 3717 | } |
| 3718 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3719 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3720 | CastExpr::CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3721 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3722 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3723 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3724 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3725 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3726 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3727 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3728 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3729 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3730 | } else |
| 3731 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3732 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3733 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3734 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3735 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3736 | return false; |
| 3737 | } |
| 3738 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3739 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3740 | CastExpr::CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3741 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3742 | |
| 3743 | QualType SrcTy = CastExpr->getType(); |
| 3744 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3745 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3746 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3747 | if (SrcTy->isVectorType()) { |
| 3748 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3749 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3750 | << DestTy << SrcTy << R; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3751 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3752 | return false; |
| 3753 | } |
| 3754 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3755 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3756 | // conversion will take place first from scalar to elt type, and then |
| 3757 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3758 | if (SrcTy->isPointerType()) |
| 3759 | return Diag(R.getBegin(), |
| 3760 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3761 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3762 | |
| 3763 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3764 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3765 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3766 | |
| 3767 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3768 | return false; |
| 3769 | } |
| 3770 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3771 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3772 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3773 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3774 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3776 | assert((Ty != 0) && (Op.get() != 0) && |
| 3777 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3778 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3779 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3780 | //FIXME: Preserve type source info. |
| 3781 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3783 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3784 | if (isa<ParenListExpr>(castExpr)) |
| 3785 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3786 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3788 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3789 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3790 | |
| 3791 | if (Method) { |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3792 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3793 | Method, move(Op)); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3794 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3795 | if (CastArg.isInvalid()) |
| 3796 | return ExprError(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3797 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3798 | castExpr = CastArg.takeAs<Expr>(); |
| 3799 | } else { |
| 3800 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3803 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | Kind, castExpr, castType, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3805 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3808 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3809 | /// of comma binary operators. |
| 3810 | Action::OwningExprResult |
| 3811 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3812 | Expr *expr = EA.takeAs<Expr>(); |
| 3813 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3814 | if (!E) |
| 3815 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3817 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3819 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3820 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3821 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3823 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3824 | } |
| 3825 | |
| 3826 | Action::OwningExprResult |
| 3827 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3828 | SourceLocation RParenLoc, ExprArg Op, |
| 3829 | QualType Ty) { |
| 3830 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
| 3832 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3833 | // then handle it as such. |
| 3834 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3835 | if (PE->getNumExprs() == 0) { |
| 3836 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3837 | return ExprError(); |
| 3838 | } |
| 3839 | |
| 3840 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3841 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3842 | initExprs.push_back(PE->getExpr(i)); |
| 3843 | |
| 3844 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3845 | // braces instead of the original commas. |
| 3846 | Op.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3847 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3848 | initExprs.size(), RParenLoc); |
| 3849 | E->setType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3850 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3851 | Owned(E)); |
| 3852 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3853 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3854 | // sequence of BinOp comma operators. |
| 3855 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3856 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3857 | } |
| 3858 | } |
| 3859 | |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3860 | Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3861 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3862 | MultiExprArg Val, |
| 3863 | TypeTy *TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3864 | unsigned nexprs = Val.size(); |
| 3865 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3866 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 3867 | Expr *expr; |
| 3868 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 3869 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 3870 | else |
| 3871 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3872 | return Owned(expr); |
| 3873 | } |
| 3874 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3875 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3876 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3877 | /// C99 6.5.15 |
| 3878 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3879 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3880 | // C++ is sufficiently different to merit its own checker. |
| 3881 | if (getLangOptions().CPlusPlus) |
| 3882 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3883 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 3884 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 3885 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3886 | UsualUnaryConversions(Cond); |
| 3887 | UsualUnaryConversions(LHS); |
| 3888 | UsualUnaryConversions(RHS); |
| 3889 | QualType CondTy = Cond->getType(); |
| 3890 | QualType LHSTy = LHS->getType(); |
| 3891 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3893 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3894 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3895 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3896 | << CondTy; |
| 3897 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3898 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3899 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3900 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3901 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3902 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3903 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3904 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3905 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3906 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3907 | UsualArithmeticConversions(LHS, RHS); |
| 3908 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3909 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3910 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3911 | // If both operands are the same structure or union type, the result is that |
| 3912 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3913 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3914 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3915 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3916 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3917 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3918 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3919 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3920 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3921 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3922 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3923 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3924 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3925 | if (!LHSTy->isVoidType()) |
| 3926 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3927 | << RHS->getSourceRange(); |
| 3928 | if (!RHSTy->isVoidType()) |
| 3929 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3930 | << LHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3931 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 3932 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3933 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3934 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3935 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3936 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3937 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3938 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3939 | // promote the null to a pointer. |
| 3940 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3941 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3942 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3943 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3944 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3945 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3946 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3947 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3948 | |
| 3949 | // All objective-c pointer type analysis is done here. |
| 3950 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 3951 | QuestionLoc); |
| 3952 | if (!compositeType.isNull()) |
| 3953 | return compositeType; |
| 3954 | |
| 3955 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3956 | // Handle block pointer types. |
| 3957 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3958 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3959 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3960 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3961 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 3962 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3963 | return destType; |
| 3964 | } |
| 3965 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3966 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3967 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3968 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3969 | // We have 2 block pointer types. |
| 3970 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3971 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3972 | return LHSTy; |
| 3973 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3974 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3975 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3976 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3977 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3978 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3979 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3980 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3981 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3982 | // In this situation, we assume void* type. No especially good |
| 3983 | // reason, but this is what gcc does, and we do have to pick |
| 3984 | // to get a consistent AST. |
| 3985 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3986 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3987 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3988 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3989 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3990 | // The block pointer types are compatible. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3991 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3992 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3993 | return LHSTy; |
| 3994 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3995 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3996 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3997 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3998 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3999 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4000 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4001 | |
| 4002 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4003 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4004 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4005 | QualType destPointee |
| 4006 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4007 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4008 | // Add qualifiers if necessary. |
| 4009 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4010 | // Promote to void*. |
| 4011 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4012 | return destType; |
| 4013 | } |
| 4014 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4015 | QualType destPointee |
| 4016 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4017 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4018 | // Add qualifiers if necessary. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4019 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4020 | // Promote to void*. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4021 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4022 | return destType; |
| 4023 | } |
| 4024 | |
| 4025 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4026 | // Two identical pointer types are always compatible. |
| 4027 | return LHSTy; |
| 4028 | } |
| 4029 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4030 | rhptee.getUnqualifiedType())) { |
| 4031 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4032 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4033 | // In this situation, we assume void* type. No especially good |
| 4034 | // reason, but this is what gcc does, and we do have to pick |
| 4035 | // to get a consistent AST. |
| 4036 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4037 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4038 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4039 | return incompatTy; |
| 4040 | } |
| 4041 | // The pointer types are compatible. |
| 4042 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4043 | // differently qualified versions of compatible types, the result type is |
| 4044 | // a pointer to an appropriately qualified version of the *composite* |
| 4045 | // type. |
| 4046 | // FIXME: Need to calculate the composite type. |
| 4047 | // FIXME: Need to add qualifiers |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4048 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4049 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4050 | return LHSTy; |
| 4051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4052 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4053 | // GCC compatibility: soften pointer/integer mismatch. |
| 4054 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4055 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4056 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4057 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4058 | return RHSTy; |
| 4059 | } |
| 4060 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4061 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4062 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4063 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4064 | return LHSTy; |
| 4065 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4066 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4067 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4068 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4069 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4070 | return QualType(); |
| 4071 | } |
| 4072 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4073 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4074 | /// two objective-c pointer types of the two input expressions. |
| 4075 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4076 | SourceLocation QuestionLoc) { |
| 4077 | QualType LHSTy = LHS->getType(); |
| 4078 | QualType RHSTy = RHS->getType(); |
| 4079 | |
| 4080 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4081 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4082 | // redefinition type if an attempt is made to access its fields. |
| 4083 | if (LHSTy->isObjCClassType() && |
| 4084 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4085 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4086 | return LHSTy; |
| 4087 | } |
| 4088 | if (RHSTy->isObjCClassType() && |
| 4089 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4090 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4091 | return RHSTy; |
| 4092 | } |
| 4093 | // And the same for struct objc_object* / id |
| 4094 | if (LHSTy->isObjCIdType() && |
| 4095 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4096 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4097 | return LHSTy; |
| 4098 | } |
| 4099 | if (RHSTy->isObjCIdType() && |
| 4100 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4101 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4102 | return RHSTy; |
| 4103 | } |
| 4104 | // And the same for struct objc_selector* / SEL |
| 4105 | if (Context.isObjCSelType(LHSTy) && |
| 4106 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4107 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4108 | return LHSTy; |
| 4109 | } |
| 4110 | if (Context.isObjCSelType(RHSTy) && |
| 4111 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4112 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4113 | return RHSTy; |
| 4114 | } |
| 4115 | // Check constraints for Objective-C object pointers types. |
| 4116 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4117 | |
| 4118 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4119 | // Two identical object pointer types are always compatible. |
| 4120 | return LHSTy; |
| 4121 | } |
| 4122 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4123 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4124 | QualType compositeType = LHSTy; |
| 4125 | |
| 4126 | // If both operands are interfaces and either operand can be |
| 4127 | // assigned to the other, use that type as the composite |
| 4128 | // type. This allows |
| 4129 | // xxx ? (A*) a : (B*) b |
| 4130 | // where B is a subclass of A. |
| 4131 | // |
| 4132 | // Additionally, as for assignment, if either type is 'id' |
| 4133 | // allow silent coercion. Finally, if the types are |
| 4134 | // incompatible then make sure to use 'id' as the composite |
| 4135 | // type so the result is acceptable for sending messages to. |
| 4136 | |
| 4137 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4138 | // It could return the composite type. |
| 4139 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4140 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4141 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4142 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4143 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4144 | RHSTy->isObjCQualifiedIdType()) && |
| 4145 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4146 | // Need to handle "id<xx>" explicitly. |
| 4147 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4148 | // id. Currently localizing to here until clear this should be |
| 4149 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4150 | compositeType = Context.getObjCIdType(); |
| 4151 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4152 | compositeType = Context.getObjCIdType(); |
| 4153 | } else if (!(compositeType = |
| 4154 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4155 | ; |
| 4156 | else { |
| 4157 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4158 | << LHSTy << RHSTy |
| 4159 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4160 | QualType incompatTy = Context.getObjCIdType(); |
| 4161 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4162 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
| 4163 | return incompatTy; |
| 4164 | } |
| 4165 | // The object pointer types are compatible. |
| 4166 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 4167 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
| 4168 | return compositeType; |
| 4169 | } |
| 4170 | // Check Objective-C object pointer types and 'void *' |
| 4171 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4172 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4173 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4174 | QualType destPointee |
| 4175 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4176 | QualType destType = Context.getPointerType(destPointee); |
| 4177 | // Add qualifiers if necessary. |
| 4178 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4179 | // Promote to void*. |
| 4180 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
| 4181 | return destType; |
| 4182 | } |
| 4183 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4184 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4185 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4186 | QualType destPointee |
| 4187 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4188 | QualType destType = Context.getPointerType(destPointee); |
| 4189 | // Add qualifiers if necessary. |
| 4190 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 4191 | // Promote to void*. |
| 4192 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4193 | return destType; |
| 4194 | } |
| 4195 | return QualType(); |
| 4196 | } |
| 4197 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4198 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4199 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4200 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 4201 | SourceLocation ColonLoc, |
| 4202 | ExprArg Cond, ExprArg LHS, |
| 4203 | ExprArg RHS) { |
| 4204 | Expr *CondExpr = (Expr *) Cond.get(); |
| 4205 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4206 | |
| 4207 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4208 | // was the condition. |
| 4209 | bool isLHSNull = LHSExpr == 0; |
| 4210 | if (isLHSNull) |
| 4211 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4212 | |
| 4213 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4214 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4215 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4216 | return ExprError(); |
| 4217 | |
| 4218 | Cond.release(); |
| 4219 | LHS.release(); |
| 4220 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4221 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4222 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4223 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4226 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4227 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4228 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4229 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4230 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4231 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4232 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4233 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4234 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4235 | if ((lhsType->isObjCClassType() && |
| 4236 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4237 | (rhsType->isObjCClassType() && |
| 4238 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4239 | return Compatible; |
| 4240 | } |
| 4241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4242 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4243 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4244 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4245 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4246 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4247 | lhptee = Context.getCanonicalType(lhptee); |
| 4248 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4249 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4250 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4251 | |
| 4252 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4253 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4254 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4255 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4256 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4257 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4258 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4259 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4260 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4261 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4262 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4263 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4264 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4265 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4266 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4267 | assert(rhptee->isFunctionType()); |
| 4268 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4269 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4270 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4271 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4272 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4273 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4274 | |
| 4275 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4276 | assert(lhptee->isFunctionType()); |
| 4277 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4278 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4279 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4280 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4281 | lhptee = lhptee.getUnqualifiedType(); |
| 4282 | rhptee = rhptee.getUnqualifiedType(); |
| 4283 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4284 | // Check if the pointee types are compatible ignoring the sign. |
| 4285 | // We explicitly check for char so that we catch "char" vs |
| 4286 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4287 | if (lhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4288 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4289 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4290 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4291 | |
| 4292 | if (rhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4293 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4294 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4295 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4296 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4297 | if (lhptee == rhptee) { |
| 4298 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4299 | // takes priority over sign incompatibility because the sign |
| 4300 | // warning can be disabled. |
| 4301 | if (ConvTy != Compatible) |
| 4302 | return ConvTy; |
| 4303 | return IncompatiblePointerSign; |
| 4304 | } |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4305 | |
| 4306 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4307 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4308 | // the eventual target type is the same and the pointers have the same |
| 4309 | // level of indirection, this must be the issue. |
| 4310 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4311 | do { |
| 4312 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4313 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 4314 | |
| 4315 | lhptee = Context.getCanonicalType(lhptee); |
| 4316 | rhptee = Context.getCanonicalType(rhptee); |
| 4317 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 4318 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4319 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4320 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4323 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4325 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4326 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4329 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4330 | /// block pointer types are compatible or whether a block and normal pointer |
| 4331 | /// are compatible. It is more restrict than comparing two function pointer |
| 4332 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4333 | Sema::AssignConvertType |
| 4334 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4335 | QualType rhsType) { |
| 4336 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4337 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4338 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4339 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4340 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4341 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4342 | // make sure we operate on the canonical type |
| 4343 | lhptee = Context.getCanonicalType(lhptee); |
| 4344 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4345 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4346 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4347 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4348 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4349 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4350 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4351 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4352 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4353 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4354 | return ConvTy; |
| 4355 | } |
| 4356 | |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4357 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4358 | /// for assignment compatibility. |
| 4359 | Sema::AssignConvertType |
| 4360 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4361 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 4362 | return Compatible; |
| 4363 | QualType lhptee = |
| 4364 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4365 | QualType rhptee = |
| 4366 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4367 | // make sure we operate on the canonical type |
| 4368 | lhptee = Context.getCanonicalType(lhptee); |
| 4369 | rhptee = Context.getCanonicalType(rhptee); |
| 4370 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4371 | return CompatiblePointerDiscardsQualifiers; |
| 4372 | |
| 4373 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4374 | return Compatible; |
| 4375 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4376 | return IncompatibleObjCQualifiedId; |
| 4377 | return IncompatiblePointer; |
| 4378 | } |
| 4379 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4380 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4381 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4382 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4383 | /// |
| 4384 | /// int a, *pint; |
| 4385 | /// short *pshort; |
| 4386 | /// struct foo *pfoo; |
| 4387 | /// |
| 4388 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4389 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4390 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4391 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4392 | /// |
| 4393 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4394 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4395 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4396 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4397 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4398 | // Get canonical types. We're not formatting these types, just comparing |
| 4399 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4400 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4401 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4402 | |
| 4403 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4404 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4405 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4406 | if ((lhsType->isObjCClassType() && |
| 4407 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4408 | (rhsType->isObjCClassType() && |
| 4409 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4410 | return Compatible; |
| 4411 | } |
| 4412 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4413 | // If the left-hand side is a reference type, then we are in a |
| 4414 | // (rare!) case where we've allowed the use of references in C, |
| 4415 | // e.g., as a parameter type in a built-in function. In this case, |
| 4416 | // just make sure that the type referenced is compatible with the |
| 4417 | // right-hand side type. The caller is responsible for adjusting |
| 4418 | // lhsType so that the resulting expression does not have reference |
| 4419 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4420 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4421 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4422 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4423 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4424 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4425 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4426 | // to the same ExtVector type. |
| 4427 | if (lhsType->isExtVectorType()) { |
| 4428 | if (rhsType->isExtVectorType()) |
| 4429 | return lhsType == rhsType ? Compatible : Incompatible; |
| 4430 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 4431 | return Compatible; |
| 4432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4434 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4435 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4436 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4437 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4438 | if (getLangOptions().LaxVectorConversions && |
| 4439 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4440 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4441 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4442 | } |
| 4443 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4444 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4445 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4446 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4447 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4448 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4449 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4450 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4451 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4452 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4453 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4454 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4455 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4456 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4457 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4458 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4459 | return Compatible; |
| 4460 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4461 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4462 | if (rhsType->getAs<BlockPointerType>()) { |
| 4463 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4464 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4467 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4468 | return Compatible; |
| 4469 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4470 | return Incompatible; |
| 4471 | } |
| 4472 | |
| 4473 | if (isa<BlockPointerType>(lhsType)) { |
| 4474 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4475 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4476 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4477 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4478 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4479 | return Compatible; |
| 4480 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4481 | if (rhsType->isBlockPointerType()) |
| 4482 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4483 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4484 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4485 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4486 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4487 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4488 | return Incompatible; |
| 4489 | } |
| 4490 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4491 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4492 | if (rhsType->isIntegerType()) |
| 4493 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4495 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4496 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4497 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4498 | return Compatible; |
| 4499 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4500 | } |
| 4501 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4502 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4503 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4504 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4505 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4506 | return Compatible; |
| 4507 | } |
| 4508 | // Treat block pointers as objects. |
| 4509 | if (rhsType->isBlockPointerType()) |
| 4510 | return Compatible; |
| 4511 | return Incompatible; |
| 4512 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4513 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4514 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4515 | if (lhsType == Context.BoolTy) |
| 4516 | return Compatible; |
| 4517 | |
| 4518 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4519 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4520 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4521 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4522 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4523 | |
| 4524 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4525 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4526 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4527 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4528 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4529 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4530 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4531 | if (lhsType == Context.BoolTy) |
| 4532 | return Compatible; |
| 4533 | |
| 4534 | if (lhsType->isIntegerType()) |
| 4535 | return PointerToInt; |
| 4536 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4537 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4538 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4539 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4540 | return Compatible; |
| 4541 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4542 | } |
| 4543 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4544 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4545 | return Compatible; |
| 4546 | return Incompatible; |
| 4547 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4548 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4549 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4550 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4551 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4552 | } |
| 4553 | return Incompatible; |
| 4554 | } |
| 4555 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4556 | /// \brief Constructs a transparent union from an expression that is |
| 4557 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4558 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4559 | QualType UnionType, FieldDecl *Field) { |
| 4560 | // Build an initializer list that designates the appropriate member |
| 4561 | // of the transparent union. |
| 4562 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 4563 | &E, 1, |
| 4564 | SourceLocation()); |
| 4565 | Initializer->setType(UnionType); |
| 4566 | Initializer->setInitializedFieldInUnion(Field); |
| 4567 | |
| 4568 | // Build a compound literal constructing a value of the transparent |
| 4569 | // union type from this initializer list. |
| 4570 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 4571 | false); |
| 4572 | } |
| 4573 | |
| 4574 | Sema::AssignConvertType |
| 4575 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4576 | QualType FromType = rExpr->getType(); |
| 4577 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4578 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4579 | // transparent_union GCC extension. |
| 4580 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4581 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4582 | return Incompatible; |
| 4583 | |
| 4584 | // The field to initialize within the transparent union. |
| 4585 | RecordDecl *UD = UT->getDecl(); |
| 4586 | FieldDecl *InitField = 0; |
| 4587 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4588 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4589 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4590 | it != itend; ++it) { |
| 4591 | if (it->getType()->isPointerType()) { |
| 4592 | // If the transparent union contains a pointer type, we allow: |
| 4593 | // 1) void pointer |
| 4594 | // 2) null pointer constant |
| 4595 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4596 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4597 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4598 | InitField = *it; |
| 4599 | break; |
| 4600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4602 | if (rExpr->isNullPointerConstant(Context, |
| 4603 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4604 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4605 | InitField = *it; |
| 4606 | break; |
| 4607 | } |
| 4608 | } |
| 4609 | |
| 4610 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4611 | == Compatible) { |
| 4612 | InitField = *it; |
| 4613 | break; |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | if (!InitField) |
| 4618 | return Incompatible; |
| 4619 | |
| 4620 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4621 | return Compatible; |
| 4622 | } |
| 4623 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4624 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4625 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4626 | if (getLangOptions().CPlusPlus) { |
| 4627 | if (!lhsType->isRecordType()) { |
| 4628 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4629 | // expression is implicitly converted (C++ 4) to the |
| 4630 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4631 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 4632 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4633 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4634 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4638 | // structures. |
| 4639 | } |
| 4640 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4641 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4642 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | if ((lhsType->isPointerType() || |
| 4644 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4645 | lhsType->isBlockPointerType()) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4646 | && rExpr->isNullPointerConstant(Context, |
| 4647 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4648 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4649 | return Compatible; |
| 4650 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4651 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4652 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4653 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4654 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4655 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4656 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4657 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4658 | if (!lhsType->isReferenceType()) |
| 4659 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4660 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4661 | Sema::AssignConvertType result = |
| 4662 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4663 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4664 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4665 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4666 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4667 | // so that we can use references in built-in functions even in C. |
| 4668 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4669 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4670 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4671 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4672 | CastExpr::CK_Unknown); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4673 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4676 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4677 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4678 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4679 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4680 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4683 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4684 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4685 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4686 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4687 | QualType lhsType = |
| 4688 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4689 | QualType rhsType = |
| 4690 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4691 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4692 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4693 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4694 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4695 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4696 | // Handle the case of a vector & extvector type of the same size and element |
| 4697 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4698 | if (getLangOptions().LaxVectorConversions) { |
| 4699 | // FIXME: Should we warn here? |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4700 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4701 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4702 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4703 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4704 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
| 4707 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4708 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4709 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4710 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4711 | bool swapped = false; |
| 4712 | if (rhsType->isExtVectorType()) { |
| 4713 | swapped = true; |
| 4714 | std::swap(rex, lex); |
| 4715 | std::swap(rhsType, lhsType); |
| 4716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4717 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4718 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4719 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4720 | QualType EltTy = LV->getElementType(); |
| 4721 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4722 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4723 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4724 | if (swapped) std::swap(rex, lex); |
| 4725 | return lhsType; |
| 4726 | } |
| 4727 | } |
| 4728 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4729 | rhsType->isRealFloatingType()) { |
| 4730 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4731 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4732 | if (swapped) std::swap(rex, lex); |
| 4733 | return lhsType; |
| 4734 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4735 | } |
| 4736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4738 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4739 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4740 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4741 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4742 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4745 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4746 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4747 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4748 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4749 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4750 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4751 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4752 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4753 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4754 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4759 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4760 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4761 | return CheckVectorOperands(Loc, lex, rex); |
| 4762 | return InvalidOperands(Loc, lex, rex); |
| 4763 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4764 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4765 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4766 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4767 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4768 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4769 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4774 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4775 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4776 | if (CompLHSTy) *CompLHSTy = compType; |
| 4777 | return compType; |
| 4778 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4779 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4780 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4781 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4782 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4783 | if (lex->getType()->isArithmeticType() && |
| 4784 | rex->getType()->isArithmeticType()) { |
| 4785 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4786 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4787 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4788 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4789 | // Put any potential pointer into PExp |
| 4790 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4791 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4792 | std::swap(PExp, IExp); |
| 4793 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4794 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4795 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4796 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4797 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4799 | // Check for arithmetic on pointers to incomplete types. |
| 4800 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4801 | if (getLangOptions().CPlusPlus) { |
| 4802 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4803 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4804 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4805 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4806 | |
| 4807 | // GNU extension: arithmetic on pointer to void |
| 4808 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4809 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4810 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4811 | if (getLangOptions().CPlusPlus) { |
| 4812 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4813 | << lex->getType() << lex->getSourceRange(); |
| 4814 | return QualType(); |
| 4815 | } |
| 4816 | |
| 4817 | // GNU extension: arithmetic on pointer to function |
| 4818 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4819 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4820 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4821 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4823 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4824 | PExp->getType()->isObjCObjectPointerType()) && |
| 4825 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4827 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4828 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4829 | return QualType(); |
| 4830 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4831 | // Diagnose bad cases where we step over interface counts. |
| 4832 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4833 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4834 | << PointeeTy << PExp->getSourceRange(); |
| 4835 | return QualType(); |
| 4836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4837 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4838 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4839 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4840 | if (LHSTy.isNull()) { |
| 4841 | LHSTy = lex->getType(); |
| 4842 | if (LHSTy->isPromotableIntegerType()) |
| 4843 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4844 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4845 | *CompLHSTy = LHSTy; |
| 4846 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4847 | return PExp->getType(); |
| 4848 | } |
| 4849 | } |
| 4850 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4851 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4854 | // C99 6.5.6 |
| 4855 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4856 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4857 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4858 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4859 | if (CompLHSTy) *CompLHSTy = compType; |
| 4860 | return compType; |
| 4861 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4862 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4863 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4864 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4865 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4866 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4867 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4868 | if (lex->getType()->isArithmeticType() |
| 4869 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4870 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4871 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4873 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4874 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4875 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4876 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4878 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4879 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4880 | bool ComplainAboutVoid = false; |
| 4881 | Expr *ComplainAboutFunc = 0; |
| 4882 | if (lpointee->isVoidType()) { |
| 4883 | if (getLangOptions().CPlusPlus) { |
| 4884 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4885 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4886 | return QualType(); |
| 4887 | } |
| 4888 | |
| 4889 | // GNU C extension: arithmetic on pointer to void |
| 4890 | ComplainAboutVoid = true; |
| 4891 | } else if (lpointee->isFunctionType()) { |
| 4892 | if (getLangOptions().CPlusPlus) { |
| 4893 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4894 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4895 | return QualType(); |
| 4896 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4897 | |
| 4898 | // GNU C extension: arithmetic on pointer to function |
| 4899 | ComplainAboutFunc = lex; |
| 4900 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4901 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4902 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4904 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4905 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4906 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4907 | // Diagnose bad cases where we step over interface counts. |
| 4908 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4909 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4910 | << lpointee << lex->getSourceRange(); |
| 4911 | return QualType(); |
| 4912 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4913 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4914 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4915 | if (rex->getType()->isIntegerType()) { |
| 4916 | if (ComplainAboutVoid) |
| 4917 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4918 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4919 | if (ComplainAboutFunc) |
| 4920 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4922 | << ComplainAboutFunc->getSourceRange(); |
| 4923 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4924 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4925 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4926 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4927 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4928 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4929 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4930 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4931 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4932 | // RHS must be a completely-type object type. |
| 4933 | // Handle the GNU void* extension. |
| 4934 | if (rpointee->isVoidType()) { |
| 4935 | if (getLangOptions().CPlusPlus) { |
| 4936 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4937 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4938 | return QualType(); |
| 4939 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4941 | ComplainAboutVoid = true; |
| 4942 | } else if (rpointee->isFunctionType()) { |
| 4943 | if (getLangOptions().CPlusPlus) { |
| 4944 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4945 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4946 | return QualType(); |
| 4947 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4948 | |
| 4949 | // GNU extension: arithmetic on pointer to function |
| 4950 | if (!ComplainAboutFunc) |
| 4951 | ComplainAboutFunc = rex; |
| 4952 | } else if (!rpointee->isDependentType() && |
| 4953 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4954 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4955 | << rex->getSourceRange() |
| 4956 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4957 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4958 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4959 | if (getLangOptions().CPlusPlus) { |
| 4960 | // Pointee types must be the same: C++ [expr.add] |
| 4961 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4962 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4963 | << lex->getType() << rex->getType() |
| 4964 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4965 | return QualType(); |
| 4966 | } |
| 4967 | } else { |
| 4968 | // Pointee types must be compatible C99 6.5.6p3 |
| 4969 | if (!Context.typesAreCompatible( |
| 4970 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4971 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4972 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4973 | << lex->getType() << rex->getType() |
| 4974 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4975 | return QualType(); |
| 4976 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4977 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4978 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4979 | if (ComplainAboutVoid) |
| 4980 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4981 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4982 | if (ComplainAboutFunc) |
| 4983 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4984 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4985 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4986 | |
| 4987 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4988 | return Context.getPointerDiffType(); |
| 4989 | } |
| 4990 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4991 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4992 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4995 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4996 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4997 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4998 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4999 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5000 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5001 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 5002 | // Vector shifts promote their scalar inputs to vector type. |
| 5003 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 5004 | return CheckVectorOperands(Loc, lex, rex); |
| 5005 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5006 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 5007 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5008 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5009 | if (LHSTy.isNull()) { |
| 5010 | LHSTy = lex->getType(); |
| 5011 | if (LHSTy->isPromotableIntegerType()) |
| 5012 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5013 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 5014 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5015 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5016 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5017 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5018 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5019 | // Sanity-check shift operands |
| 5020 | llvm::APSInt Right; |
| 5021 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 5022 | if (!rex->isValueDependent() && |
| 5023 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 5024 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5025 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 5026 | else { |
| 5027 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 5028 | Context.getTypeSize(lex->getType())); |
| 5029 | if (Right.uge(LeftBits)) |
| 5030 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 5031 | } |
| 5032 | } |
| 5033 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5034 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5035 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5038 | /// \brief Implements -Wsign-compare. |
| 5039 | /// |
| 5040 | /// \param lex the left-hand expression |
| 5041 | /// \param rex the right-hand expression |
| 5042 | /// \param OpLoc the location of the joining operator |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 5043 | /// \param Equality whether this is an "equality-like" join, which |
| 5044 | /// suppresses the warning in some cases |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 5045 | void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc, |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5046 | const PartialDiagnostic &PD, bool Equality) { |
John McCall | 7d62a8f | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 5047 | // Don't warn if we're in an unevaluated context. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 5048 | if (ExprEvalContexts.back().Context == Unevaluated) |
John McCall | 7d62a8f | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 5049 | return; |
| 5050 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5051 | QualType lt = lex->getType(), rt = rex->getType(); |
| 5052 | |
| 5053 | // Only warn if both operands are integral. |
| 5054 | if (!lt->isIntegerType() || !rt->isIntegerType()) |
| 5055 | return; |
| 5056 | |
Sebastian Redl | 732429c | 2009-11-05 21:09:23 +0000 | [diff] [blame] | 5057 | // If either expression is value-dependent, don't warn. We'll get another |
| 5058 | // chance at instantiation time. |
| 5059 | if (lex->isValueDependent() || rex->isValueDependent()) |
| 5060 | return; |
| 5061 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5062 | // The rule is that the signed operand becomes unsigned, so isolate the |
| 5063 | // signed operand. |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5064 | Expr *signedOperand, *unsignedOperand; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5065 | if (lt->isSignedIntegerType()) { |
| 5066 | if (rt->isSignedIntegerType()) return; |
| 5067 | signedOperand = lex; |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5068 | unsignedOperand = rex; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5069 | } else { |
| 5070 | if (!rt->isSignedIntegerType()) return; |
| 5071 | signedOperand = rex; |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5072 | unsignedOperand = lex; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5075 | // If the unsigned type is strictly smaller than the signed type, |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 5076 | // then (1) the result type will be signed and (2) the unsigned |
| 5077 | // value will fit fully within the signed type, and thus the result |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5078 | // of the comparison will be exact. |
| 5079 | if (Context.getIntWidth(signedOperand->getType()) > |
| 5080 | Context.getIntWidth(unsignedOperand->getType())) |
| 5081 | return; |
| 5082 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5083 | // If the value is a non-negative integer constant, then the |
| 5084 | // signed->unsigned conversion won't change it. |
| 5085 | llvm::APSInt value; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 5086 | if (signedOperand->isIntegerConstantExpr(value, Context)) { |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5087 | assert(value.isSigned() && "result of signed expression not signed"); |
| 5088 | |
| 5089 | if (value.isNonNegative()) |
| 5090 | return; |
| 5091 | } |
| 5092 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5093 | if (Equality) { |
| 5094 | // For (in)equality comparisons, if the unsigned operand is a |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 5095 | // constant which cannot collide with a overflowed signed operand, |
| 5096 | // then reinterpreting the signed operand as unsigned will not |
| 5097 | // change the result of the comparison. |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5098 | if (unsignedOperand->isIntegerConstantExpr(value, Context)) { |
| 5099 | assert(!value.isSigned() && "result of unsigned expression is signed"); |
| 5100 | |
| 5101 | // 2's complement: test the top bit. |
| 5102 | if (value.isNonNegative()) |
| 5103 | return; |
| 5104 | } |
| 5105 | } |
| 5106 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 5107 | Diag(OpLoc, PD) |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5108 | << lex->getType() << rex->getType() |
| 5109 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5110 | } |
| 5111 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5112 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5113 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5114 | unsigned OpaqueOpc, bool isRelational) { |
| 5115 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 5116 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5117 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5118 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5119 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5120 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5121 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 5122 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5123 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5124 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 5125 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5126 | UsualArithmeticConversions(lex, rex); |
| 5127 | else { |
| 5128 | UsualUnaryConversions(lex); |
| 5129 | UsualUnaryConversions(rex); |
| 5130 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5131 | QualType lType = lex->getType(); |
| 5132 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5133 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5134 | if (!lType->isFloatingType() |
| 5135 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5136 | // For non-floating point types, check for self-comparisons of the form |
| 5137 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5138 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5139 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 5140 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5141 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5142 | Expr *RHSStripped = rex->IgnoreParens(); |
| 5143 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 5144 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 5145 | if (DRL->getDecl() == DRR->getDecl() && |
| 5146 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5147 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5148 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5149 | if (isa<CastExpr>(LHSStripped)) |
| 5150 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5151 | if (isa<CastExpr>(RHSStripped)) |
| 5152 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5153 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5154 | // Warn about comparisons against a string constant (unless the other |
| 5155 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5156 | Expr *literalString = 0; |
| 5157 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5158 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5159 | !RHSStripped->isNullPointerConstant(Context, |
| 5160 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5161 | literalString = lex; |
| 5162 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5163 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5164 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5165 | !LHSStripped->isNullPointerConstant(Context, |
| 5166 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5167 | literalString = rex; |
| 5168 | literalStringStripped = RHSStripped; |
| 5169 | } |
| 5170 | |
| 5171 | if (literalString) { |
| 5172 | std::string resultComparison; |
| 5173 | switch (Opc) { |
| 5174 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 5175 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 5176 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 5177 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 5178 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 5179 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 5180 | default: assert(false && "Invalid comparison operator"); |
| 5181 | } |
| 5182 | Diag(Loc, diag::warn_stringcompare) |
| 5183 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 5184 | << literalString->getSourceRange() |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5185 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 5186 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 5187 | "strcmp(") |
| 5188 | << CodeModificationHint::CreateInsertion( |
| 5189 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5190 | resultComparison); |
| 5191 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5192 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5193 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5194 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5195 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5196 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5197 | if (isRelational) { |
| 5198 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5199 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5200 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5201 | // Check for comparisons of floating point operands using != and ==. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5202 | if (lType->isFloatingType() && rType->isFloatingType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5203 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5204 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5205 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5206 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5207 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5208 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5209 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 5210 | Expr::NPC_ValueDependentIsNull); |
| 5211 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 5212 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5213 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5214 | // All of the following pointer related warnings are GCC extensions, except |
| 5215 | // when handling null pointer constants. One day, we can consider making them |
| 5216 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5217 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5218 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5219 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5220 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5221 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5223 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5224 | if (LCanPointeeTy == RCanPointeeTy) |
| 5225 | return ResultTy; |
| 5226 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5227 | // C++ [expr.rel]p2: |
| 5228 | // [...] Pointer conversions (4.10) and qualification |
| 5229 | // conversions (4.4) are performed on pointer operands (or on |
| 5230 | // a pointer operand and a null pointer constant) to bring |
| 5231 | // them to their composite pointer type. [...] |
| 5232 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5233 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5234 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 5235 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5236 | if (T.isNull()) { |
| 5237 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5238 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5239 | return QualType(); |
| 5240 | } |
| 5241 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5242 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5243 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5244 | return ResultTy; |
| 5245 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5246 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5247 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5248 | RCanPointeeTy.getUnqualifiedType())) { |
| 5249 | // Valid unless a relational comparison of function pointers |
| 5250 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5251 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5252 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5253 | } |
| 5254 | } else if (!isRelational && |
| 5255 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5256 | // Valid unless comparison between non-null pointer and function pointer |
| 5257 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5258 | && !LHSIsNull && !RHSIsNull) { |
| 5259 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5260 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5261 | } |
| 5262 | } else { |
| 5263 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5264 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5265 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5266 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5267 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5268 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5269 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5272 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5274 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5276 | (lType->isPointerType() || |
| 5277 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5278 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5279 | return ResultTy; |
| 5280 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5281 | if (LHSIsNull && |
| 5282 | (rType->isPointerType() || |
| 5283 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5284 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5285 | return ResultTy; |
| 5286 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5287 | |
| 5288 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5290 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5291 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | // In addition, pointers to members can be compared, or a pointer to |
| 5293 | // member and a null pointer constant. Pointer to member conversions |
| 5294 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5295 | // them to a common type. If one operand is a null pointer constant, |
| 5296 | // the common type is the type of the other operand. Otherwise, the |
| 5297 | // common type is a pointer to member type similar (4.4) to the type |
| 5298 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5299 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5300 | // types. |
| 5301 | QualType T = FindCompositePointerType(lex, rex); |
| 5302 | if (T.isNull()) { |
| 5303 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5304 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5305 | return QualType(); |
| 5306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5307 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5308 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5309 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5310 | return ResultTy; |
| 5311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5312 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5313 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5314 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5315 | return ResultTy; |
| 5316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5317 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5318 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5319 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5320 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5321 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5322 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5323 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5324 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5325 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5326 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5327 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5328 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5329 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5330 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5331 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5332 | if (!isRelational |
| 5333 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5334 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5335 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5336 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5337 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5338 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5339 | ->getPointeeType()->isVoidType()))) |
| 5340 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5341 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5342 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5343 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5344 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5345 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5346 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5347 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5348 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5349 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5350 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5351 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5352 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5353 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5354 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5355 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5356 | if (!LPtrToVoid && !RPtrToVoid && |
| 5357 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5358 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5359 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5360 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5361 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5362 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5363 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5364 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5365 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5366 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5367 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5368 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5369 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5370 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5371 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5372 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5373 | unsigned DiagID = 0; |
| 5374 | if (RHSIsNull) { |
| 5375 | if (isRelational) |
| 5376 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5377 | } else if (isRelational) |
| 5378 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5379 | else |
| 5380 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5381 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5382 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5383 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5384 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5385 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5386 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5387 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5388 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5389 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5390 | unsigned DiagID = 0; |
| 5391 | if (LHSIsNull) { |
| 5392 | if (isRelational) |
| 5393 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5394 | } else if (isRelational) |
| 5395 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5396 | else |
| 5397 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5398 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5399 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5400 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5401 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5402 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5403 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5404 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5405 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5406 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5407 | if (!isRelational && RHSIsNull |
| 5408 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5409 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5410 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5411 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5412 | if (!isRelational && LHSIsNull |
| 5413 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5414 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5415 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5416 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5417 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5420 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5421 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5422 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5423 | /// types. |
| 5424 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5425 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5426 | bool isRelational) { |
| 5427 | // Check to make sure we're operating on vectors of the same type and width, |
| 5428 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5429 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5430 | if (vType.isNull()) |
| 5431 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5432 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5433 | QualType lType = lex->getType(); |
| 5434 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5435 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5436 | // For non-floating point types, check for self-comparisons of the form |
| 5437 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5438 | // often indicate logic errors in the program. |
| 5439 | if (!lType->isFloatingType()) { |
| 5440 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5441 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5442 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5443 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5444 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5445 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5446 | // Check for comparisons of floating point operands using != and ==. |
| 5447 | if (!isRelational && lType->isFloatingType()) { |
| 5448 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5449 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5450 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5451 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5452 | // Return the type for the comparison, which is the same as vector type for |
| 5453 | // integer vectors, or an integer type of identical size and number of |
| 5454 | // elements for floating point vectors. |
| 5455 | if (lType->isIntegerType()) |
| 5456 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5457 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5458 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5459 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5460 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5461 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5462 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5463 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5464 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5465 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5466 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5467 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5468 | } |
| 5469 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5470 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 5472 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5473 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5474 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5475 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5476 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5477 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5478 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5479 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5484 | if (!Context.getLangOptions().CPlusPlus) { |
| 5485 | UsualUnaryConversions(lex); |
| 5486 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5487 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5488 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5489 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5490 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5491 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5492 | } |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5493 | |
| 5494 | // C++ [expr.log.and]p1 |
| 5495 | // C++ [expr.log.or]p1 |
| 5496 | // The operands are both implicitly converted to type bool (clause 4). |
| 5497 | StandardConversionSequence LHS; |
| 5498 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 5499 | /*InOverloadResolution=*/false, LHS)) |
| 5500 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5501 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5502 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
| 5503 | "passing", /*IgnoreBaseAccess=*/false)) |
| 5504 | return InvalidOperands(Loc, lex, rex); |
| 5505 | |
| 5506 | StandardConversionSequence RHS; |
| 5507 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 5508 | /*InOverloadResolution=*/false, RHS)) |
| 5509 | return InvalidOperands(Loc, lex, rex); |
| 5510 | |
| 5511 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
| 5512 | "passing", /*IgnoreBaseAccess=*/false)) |
| 5513 | return InvalidOperands(Loc, lex, rex); |
| 5514 | |
| 5515 | // C++ [expr.log.and]p2 |
| 5516 | // C++ [expr.log.or]p2 |
| 5517 | // The result is a bool. |
| 5518 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5521 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5522 | /// is a read-only property; return true if so. A readonly property expression |
| 5523 | /// depends on various declarations and thus must be treated specially. |
| 5524 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5525 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5526 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5527 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5528 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5529 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5530 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5531 | BaseType->getAsObjCInterfacePointerType()) |
| 5532 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5533 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5534 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
| 5537 | return false; |
| 5538 | } |
| 5539 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5540 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5541 | /// emit an error and return true. If so, return false. |
| 5542 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5543 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5544 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5545 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5546 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5547 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5548 | if (IsLV == Expr::MLV_Valid) |
| 5549 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5550 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5551 | unsigned Diag = 0; |
| 5552 | bool NeedType = false; |
| 5553 | switch (IsLV) { // C99 6.5.16p2 |
| 5554 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 5555 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5556 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5557 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5558 | NeedType = true; |
| 5559 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5560 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5561 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5562 | NeedType = true; |
| 5563 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5564 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5565 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5566 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5567 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5568 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5569 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5570 | case Expr::MLV_IncompleteType: |
| 5571 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5572 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5573 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 5574 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5575 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5576 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5577 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5578 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5579 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5580 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5581 | case Expr::MLV_ReadonlyProperty: |
| 5582 | Diag = diag::error_readonly_property_assignment; |
| 5583 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5584 | case Expr::MLV_NoSetterProperty: |
| 5585 | Diag = diag::error_nosetter_property_assignment; |
| 5586 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5587 | case Expr::MLV_SubObjCPropertySetting: |
| 5588 | Diag = diag::error_no_subobject_property_setting; |
| 5589 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5590 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5591 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5592 | SourceRange Assign; |
| 5593 | if (Loc != OrigLoc) |
| 5594 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5595 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5596 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5597 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5598 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5599 | return true; |
| 5600 | } |
| 5601 | |
| 5602 | |
| 5603 | |
| 5604 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5605 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5606 | SourceLocation Loc, |
| 5607 | QualType CompoundType) { |
| 5608 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5609 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5610 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5611 | |
| 5612 | QualType LHSType = LHS->getType(); |
| 5613 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5614 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5615 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5616 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5617 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5618 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5619 | // Special case of NSObject attributes on c-style pointer types. |
| 5620 | if (ConvTy == IncompatiblePointer && |
| 5621 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5622 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5623 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5624 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5625 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5626 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5627 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5628 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5629 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5630 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5631 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5632 | RHSCheck = ICE->getSubExpr(); |
| 5633 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5634 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5635 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5636 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5637 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5638 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5639 | // And there is a space or other character before the subexpr of the |
| 5640 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5641 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5642 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5643 | Diag(Loc, diag::warn_not_compound_assign) |
| 5644 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5645 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5646 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5647 | } |
| 5648 | } else { |
| 5649 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5650 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5651 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5652 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5653 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 5654 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5655 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5656 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5657 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5658 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5659 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5660 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5661 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5662 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5663 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5664 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5665 | } |
| 5666 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5667 | // C99 6.5.17 |
| 5668 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5669 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5670 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5671 | |
| 5672 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5673 | // incomplete in C++). |
| 5674 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5675 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5678 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5679 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5680 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5681 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5682 | if (Op->isTypeDependent()) |
| 5683 | return Context.DependentTy; |
| 5684 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5685 | QualType ResType = Op->getType(); |
| 5686 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5687 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5688 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5689 | // Decrement of bool is not allowed. |
| 5690 | if (!isInc) { |
| 5691 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5692 | return QualType(); |
| 5693 | } |
| 5694 | // Increment of bool sets it to true, but is deprecated. |
| 5695 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5696 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5697 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5698 | } else if (ResType->isAnyPointerType()) { |
| 5699 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5700 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5701 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5702 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5703 | if (getLangOptions().CPlusPlus) { |
| 5704 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5705 | << Op->getSourceRange(); |
| 5706 | return QualType(); |
| 5707 | } |
| 5708 | |
| 5709 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5710 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5711 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5712 | if (getLangOptions().CPlusPlus) { |
| 5713 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5714 | << Op->getType() << Op->getSourceRange(); |
| 5715 | return QualType(); |
| 5716 | } |
| 5717 | |
| 5718 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5719 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5720 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5721 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5722 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5723 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5724 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5725 | // Diagnose bad cases where we step over interface counts. |
| 5726 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5727 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5728 | << PointeeTy << Op->getSourceRange(); |
| 5729 | return QualType(); |
| 5730 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5731 | } else if (ResType->isComplexType()) { |
| 5732 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5733 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5734 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5735 | } else { |
| 5736 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 5737 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5738 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5739 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5740 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 5741 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5742 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5743 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5744 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5747 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5748 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5749 | /// where the declaration is needed for type checking. We only need to |
| 5750 | /// handle cases when the expression references a function designator |
| 5751 | /// or is an lvalue. Here are some examples: |
| 5752 | /// - &(x) => x |
| 5753 | /// - &*****f => f for f a function designator. |
| 5754 | /// - &s.xx => s |
| 5755 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5756 | /// - *(x + 1) -> x, if x is an array |
| 5757 | /// - &"123"[2] -> 0 |
| 5758 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5759 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5760 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5761 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5762 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5763 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5764 | // If this is an arrow operator, the address is an offset from |
| 5765 | // the base's value, so the object the base refers to is |
| 5766 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5767 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5768 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5769 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5770 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5771 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5772 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5773 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5774 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5775 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5776 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5777 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5778 | } |
| 5779 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5780 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5781 | case Stmt::UnaryOperatorClass: { |
| 5782 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5783 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5784 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5785 | case UnaryOperator::Real: |
| 5786 | case UnaryOperator::Imag: |
| 5787 | case UnaryOperator::Extension: |
| 5788 | return getPrimaryDecl(UO->getSubExpr()); |
| 5789 | default: |
| 5790 | return 0; |
| 5791 | } |
| 5792 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5793 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5794 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5795 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5796 | // If the result of an implicit cast is an l-value, we care about |
| 5797 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5798 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5799 | default: |
| 5800 | return 0; |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5805 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5806 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5807 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5808 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5809 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5810 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5811 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5812 | // Make sure to ignore parentheses in subsequent checks |
| 5813 | op = op->IgnoreParens(); |
| 5814 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5815 | if (op->isTypeDependent()) |
| 5816 | return Context.DependentTy; |
| 5817 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5818 | if (getLangOptions().C99) { |
| 5819 | // Implement C99-only parts of addressof rules. |
| 5820 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5821 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5822 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5823 | // (assuming the deref expression is valid). |
| 5824 | return uOp->getSubExpr()->getType(); |
| 5825 | } |
| 5826 | // Technically, there should be a check for array subscript |
| 5827 | // expressions here, but the result of one is always an lvalue anyway. |
| 5828 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5829 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5830 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5831 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5832 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5833 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5834 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5835 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5836 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5837 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5838 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5839 | return QualType(); |
| 5840 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5841 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5842 | // The operand cannot be a bit-field |
| 5843 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5844 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5845 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5846 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5847 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5848 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5849 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5850 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5851 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5852 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5853 | // cannot take address of a property expression. |
| 5854 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5855 | << "property expression" << op->getSourceRange(); |
| 5856 | return QualType(); |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5857 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5858 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 474e102 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5859 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5860 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5861 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5862 | return Context.OverloadTy; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5863 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5864 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5865 | // with the register storage-class specifier. |
| 5866 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5867 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5868 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5869 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5870 | return QualType(); |
| 5871 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5872 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5873 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5874 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5875 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5876 | // Could be a pointer to member, though, if there is an explicit |
| 5877 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5878 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5879 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5880 | if (Ctx && Ctx->isRecord()) { |
| 5881 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5882 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5883 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5884 | << FD->getDeclName() << FD->getType(); |
| 5885 | return QualType(); |
| 5886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5887 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5888 | return Context.getMemberPointerType(op->getType(), |
| 5889 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5890 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5891 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5892 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5893 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5894 | // As above. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5895 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 5896 | MD->isInstance()) |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5897 | return Context.getMemberPointerType(op->getType(), |
| 5898 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5899 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5900 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5901 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5902 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5903 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5904 | // Taking the address of a void variable is technically illegal, but we |
| 5905 | // allow it in cases which are otherwise valid. |
| 5906 | // Example: "extern void x; void* y = &x;". |
| 5907 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5908 | } |
| 5909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5910 | // If the operand has type "type", the result has type "pointer to type". |
| 5911 | return Context.getPointerType(op->getType()); |
| 5912 | } |
| 5913 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5914 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5915 | if (Op->isTypeDependent()) |
| 5916 | return Context.DependentTy; |
| 5917 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5918 | UsualUnaryConversions(Op); |
| 5919 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5920 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5921 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5922 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5923 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5924 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5925 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5926 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5927 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5928 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5929 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5930 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5931 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5932 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5933 | return QualType(); |
| 5934 | } |
| 5935 | |
| 5936 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5937 | tok::TokenKind Kind) { |
| 5938 | BinaryOperator::Opcode Opc; |
| 5939 | switch (Kind) { |
| 5940 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5941 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5942 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5943 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5944 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5945 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5946 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5947 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5948 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5949 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5950 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5951 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5952 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5953 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5954 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5955 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5956 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5957 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5958 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5959 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5960 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5961 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5962 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5963 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5964 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5965 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5966 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5967 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5968 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5969 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5970 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5971 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5972 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5973 | } |
| 5974 | return Opc; |
| 5975 | } |
| 5976 | |
| 5977 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5978 | tok::TokenKind Kind) { |
| 5979 | UnaryOperator::Opcode Opc; |
| 5980 | switch (Kind) { |
| 5981 | default: assert(0 && "Unknown unary op!"); |
| 5982 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5983 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5984 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5985 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5986 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5987 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5988 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5989 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5990 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5991 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 5992 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 5993 | } |
| 5994 | return Opc; |
| 5995 | } |
| 5996 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5997 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5998 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5999 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6000 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 6001 | unsigned Op, |
| 6002 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6003 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6004 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6005 | // The following two variables are used for compound assignment operators |
| 6006 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 6007 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6008 | |
| 6009 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6010 | case BinaryOperator::Assign: |
| 6011 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 6012 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6013 | case BinaryOperator::PtrMemD: |
| 6014 | case BinaryOperator::PtrMemI: |
| 6015 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 6016 | Opc == BinaryOperator::PtrMemI); |
| 6017 | break; |
| 6018 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6019 | case BinaryOperator::Div: |
| 6020 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 6021 | break; |
| 6022 | case BinaryOperator::Rem: |
| 6023 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 6024 | break; |
| 6025 | case BinaryOperator::Add: |
| 6026 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 6027 | break; |
| 6028 | case BinaryOperator::Sub: |
| 6029 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 6030 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6031 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6032 | case BinaryOperator::Shr: |
| 6033 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 6034 | break; |
| 6035 | case BinaryOperator::LE: |
| 6036 | case BinaryOperator::LT: |
| 6037 | case BinaryOperator::GE: |
| 6038 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6039 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6040 | break; |
| 6041 | case BinaryOperator::EQ: |
| 6042 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6043 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6044 | break; |
| 6045 | case BinaryOperator::And: |
| 6046 | case BinaryOperator::Xor: |
| 6047 | case BinaryOperator::Or: |
| 6048 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 6049 | break; |
| 6050 | case BinaryOperator::LAnd: |
| 6051 | case BinaryOperator::LOr: |
| 6052 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 6053 | break; |
| 6054 | case BinaryOperator::MulAssign: |
| 6055 | case BinaryOperator::DivAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6056 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 6057 | CompLHSTy = CompResultTy; |
| 6058 | if (!CompResultTy.isNull()) |
| 6059 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6060 | break; |
| 6061 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6062 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6063 | CompLHSTy = CompResultTy; |
| 6064 | if (!CompResultTy.isNull()) |
| 6065 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6066 | break; |
| 6067 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6068 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6069 | if (!CompResultTy.isNull()) |
| 6070 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6071 | break; |
| 6072 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6073 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6074 | if (!CompResultTy.isNull()) |
| 6075 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6076 | break; |
| 6077 | case BinaryOperator::ShlAssign: |
| 6078 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6079 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6080 | CompLHSTy = CompResultTy; |
| 6081 | if (!CompResultTy.isNull()) |
| 6082 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6083 | break; |
| 6084 | case BinaryOperator::AndAssign: |
| 6085 | case BinaryOperator::XorAssign: |
| 6086 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6087 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6088 | CompLHSTy = CompResultTy; |
| 6089 | if (!CompResultTy.isNull()) |
| 6090 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6091 | break; |
| 6092 | case BinaryOperator::Comma: |
| 6093 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6094 | break; |
| 6095 | } |
| 6096 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6097 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6098 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6099 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6100 | else |
| 6101 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6102 | CompLHSTy, CompResultTy, |
| 6103 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6104 | } |
| 6105 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6106 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6107 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6108 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6109 | const PartialDiagnostic &PD, |
| 6110 | SourceRange ParenRange) |
| 6111 | { |
| 6112 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6113 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6114 | // We can't display the parentheses, so just dig the |
| 6115 | // warning/error and return. |
| 6116 | Self.Diag(Loc, PD); |
| 6117 | return; |
| 6118 | } |
| 6119 | |
| 6120 | Self.Diag(Loc, PD) |
| 6121 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6122 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
| 6123 | } |
| 6124 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6125 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6126 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6127 | /// comparison operators have higher precedence. The most typical example of |
| 6128 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6129 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6130 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6131 | typedef BinaryOperator BinOp; |
| 6132 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6133 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6134 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6135 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6136 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6137 | rhsopc = BO->getOpcode(); |
| 6138 | |
| 6139 | // Subs are not binary operators. |
| 6140 | if (lhsopc == -1 && rhsopc == -1) |
| 6141 | return; |
| 6142 | |
| 6143 | // Bitwise operations are sometimes used as eager logical ops. |
| 6144 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6145 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6146 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6147 | return; |
| 6148 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6149 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6150 | SuggestParentheses(Self, OpLoc, |
| 6151 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6152 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6153 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
| 6154 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 6155 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6156 | SuggestParentheses(Self, OpLoc, |
| 6157 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6158 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6159 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
| 6160 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6164 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6165 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 6166 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6167 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6168 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6169 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6170 | } |
| 6171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6172 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6173 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 6174 | tok::TokenKind Kind, |
| 6175 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6176 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6177 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6178 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6179 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6180 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6181 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6182 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6183 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6184 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6185 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6186 | } |
| 6187 | |
| 6188 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 6189 | BinaryOperator::Opcode Opc, |
| 6190 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6191 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6192 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6193 | rhs->getType()->isOverloadableType())) { |
| 6194 | // Find all of the overloaded operators visible from this |
| 6195 | // point. We perform both an operator-name lookup from the local |
| 6196 | // scope and an argument-dependent lookup based on the types of |
| 6197 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6198 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6199 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 6200 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6201 | if (S) |
| 6202 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6203 | Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6204 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6205 | DeclarationName OpName |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6206 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6207 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6208 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6209 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6210 | // Build the (potentially-overloaded, potentially-dependent) |
| 6211 | // binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6212 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6213 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6214 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6215 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6216 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6217 | } |
| 6218 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6219 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6220 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6221 | ExprArg InputArg) { |
| 6222 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6223 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6224 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6225 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6226 | QualType resultType; |
| 6227 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6228 | case UnaryOperator::OffsetOf: |
| 6229 | assert(false && "Invalid unary operator"); |
| 6230 | break; |
| 6231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6232 | case UnaryOperator::PreInc: |
| 6233 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6234 | case UnaryOperator::PostInc: |
| 6235 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6236 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6237 | Opc == UnaryOperator::PreInc || |
| 6238 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6239 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6240 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6241 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 6242 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6243 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 6244 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6245 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 6246 | break; |
| 6247 | case UnaryOperator::Plus: |
| 6248 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6249 | UsualUnaryConversions(Input); |
| 6250 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6251 | if (resultType->isDependentType()) |
| 6252 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6253 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 6254 | break; |
| 6255 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6256 | resultType->isEnumeralType()) |
| 6257 | break; |
| 6258 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 6259 | Opc == UnaryOperator::Plus && |
| 6260 | resultType->isPointerType()) |
| 6261 | break; |
| 6262 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6263 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6264 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6265 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6266 | UsualUnaryConversions(Input); |
| 6267 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6268 | if (resultType->isDependentType()) |
| 6269 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6270 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6271 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6272 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6273 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6274 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6275 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6276 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6277 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6278 | break; |
| 6279 | case UnaryOperator::LNot: // logical negation |
| 6280 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6281 | DefaultFunctionArrayConversion(Input); |
| 6282 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6283 | if (resultType->isDependentType()) |
| 6284 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6285 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6286 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6287 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6288 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6289 | // In C++, it's bool. C++ 5.3.1p8 |
| 6290 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6291 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6292 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6293 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 6294 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6295 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6296 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6297 | resultType = Input->getType(); |
| 6298 | break; |
| 6299 | } |
| 6300 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6301 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6302 | |
| 6303 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6304 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6307 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6308 | UnaryOperator::Opcode Opc, |
| 6309 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6310 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6311 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 6312 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6313 | // Find all of the overloaded operators visible from this |
| 6314 | // point. We perform both an operator-name lookup from the local |
| 6315 | // scope and an argument-dependent lookup based on the types of |
| 6316 | // the arguments. |
| 6317 | FunctionSet Functions; |
| 6318 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 6319 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6320 | if (S) |
| 6321 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 6322 | Functions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6323 | DeclarationName OpName |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6324 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6325 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6326 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6327 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6328 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 6329 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6330 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6331 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 6332 | } |
| 6333 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6334 | // Unary Operators. 'Tok' is the token for the operator. |
| 6335 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6336 | tok::TokenKind Op, ExprArg input) { |
| 6337 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 6338 | } |
| 6339 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6340 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6341 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 6342 | SourceLocation LabLoc, |
| 6343 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6344 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 6345 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6346 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6347 | // If we haven't seen this label yet, create a forward reference. It |
| 6348 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6349 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6350 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6352 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6353 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6354 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6357 | Sema::OwningExprResult |
| 6358 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 6359 | SourceLocation RPLoc) { // "({..})" |
| 6360 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6361 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6362 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6363 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6364 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6365 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6366 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6367 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6368 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6369 | // example, it is not possible to goto into a stmt expression apparently. |
| 6370 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6371 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6372 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6373 | // as the type of the stmtexpr. |
| 6374 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6375 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6376 | if (!Compound->body_empty()) { |
| 6377 | Stmt *LastStmt = Compound->body_back(); |
| 6378 | // If LastStmt is a label, skip down through into the body. |
| 6379 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6380 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6381 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6382 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6383 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6384 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6385 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6386 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6387 | // expressions are not lvalues. |
| 6388 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6389 | substmt.release(); |
| 6390 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6391 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6392 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6393 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 6394 | SourceLocation BuiltinLoc, |
| 6395 | SourceLocation TypeLoc, |
| 6396 | TypeTy *argty, |
| 6397 | OffsetOfComponent *CompPtr, |
| 6398 | unsigned NumComponents, |
| 6399 | SourceLocation RPLoc) { |
| 6400 | // FIXME: This function leaks all expressions in the offset components on |
| 6401 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6402 | // FIXME: Preserve type source info. |
| 6403 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6404 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6405 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6406 | bool Dependent = ArgTy->isDependentType(); |
| 6407 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6408 | // We must have at least one component that refers to the type, and the first |
| 6409 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6410 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6411 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6412 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6413 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6414 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 6415 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 6416 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6417 | // Otherwise, create a null pointer as the base, and iteratively process |
| 6418 | // the offsetof designators. |
| 6419 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 6420 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6421 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6422 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 6423 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6424 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6425 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6426 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6427 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6428 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6429 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6430 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6431 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6432 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 6433 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6434 | |
John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 6435 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 6436 | diag::err_offsetof_incomplete_type)) |
| 6437 | return ExprError(); |
| 6438 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6439 | // FIXME: Dependent case loses a lot of information here. And probably |
| 6440 | // leaks like a sieve. |
| 6441 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6442 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6443 | if (OC.isBrackets) { |
| 6444 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6445 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 6446 | if (!AT) { |
| 6447 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6448 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6449 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 6453 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6454 | // Promote the array so it looks more like a normal array subscript |
| 6455 | // expression. |
| 6456 | DefaultFunctionArrayConversion(Res); |
| 6457 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6458 | // C99 6.5.2.1p1 |
| 6459 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6460 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6461 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6462 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 6463 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6464 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6465 | |
| 6466 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 6467 | OC.LocEnd); |
| 6468 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6469 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6470 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6471 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6472 | if (!RC) { |
| 6473 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6474 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6475 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6476 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 6477 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6478 | // Get the decl corresponding to this. |
| 6479 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6480 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 6481 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 6482 | switch (ExprEvalContexts.back().Context ) { |
| 6483 | case Unevaluated: |
| 6484 | // The argument will never be evaluated, so don't complain. |
| 6485 | break; |
| 6486 | |
| 6487 | case PotentiallyEvaluated: |
| 6488 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 6489 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6490 | << Res->getType()); |
| 6491 | DidWarnAboutNonPOD = true; |
| 6492 | break; |
| 6493 | |
| 6494 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 6495 | ExprEvalContexts.back().addDiagnostic(BuiltinLoc, |
| 6496 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6497 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6498 | << Res->getType()); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 6499 | DidWarnAboutNonPOD = true; |
| 6500 | break; |
| 6501 | } |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 6502 | } |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6503 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6504 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6505 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6506 | LookupQualifiedName(R, RD); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6507 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6508 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6509 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6510 | if (!MemberDecl) |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6511 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6512 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6513 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6514 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 6515 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6516 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 6517 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 09b6d0e | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 6518 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6519 | } else { |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 6520 | PerformObjectMemberConversion(Res, MemberDecl); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6521 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 6522 | // doesn't matter here. |
| 6523 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 6524 | MemberDecl->getType().getNonReferenceType()); |
| 6525 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6526 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6527 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6528 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6529 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 6530 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6534 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 6535 | TypeTy *arg1,TypeTy *arg2, |
| 6536 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6537 | // FIXME: Preserve type source info. |
| 6538 | QualType argT1 = GetTypeFromParser(arg1); |
| 6539 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6540 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6541 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 6543 | if (getLangOptions().CPlusPlus) { |
| 6544 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 6545 | << SourceRange(BuiltinLoc, RPLoc); |
| 6546 | return ExprError(); |
| 6547 | } |
| 6548 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6549 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 6550 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6553 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 6554 | ExprArg cond, |
| 6555 | ExprArg expr1, ExprArg expr2, |
| 6556 | SourceLocation RPLoc) { |
| 6557 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 6558 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 6559 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6560 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6561 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 6562 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6563 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6564 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 6565 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6566 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6567 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6568 | } else { |
| 6569 | // The conditional expression is required to be a constant expression. |
| 6570 | llvm::APSInt condEval(32); |
| 6571 | SourceLocation ExpLoc; |
| 6572 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6573 | return ExprError(Diag(ExpLoc, |
| 6574 | diag::err_typecheck_choose_expr_requires_constant) |
| 6575 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6576 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6577 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 6578 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6579 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 6580 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6581 | } |
| 6582 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6583 | cond.release(); expr1.release(); expr2.release(); |
| 6584 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6585 | resType, RPLoc, |
| 6586 | resType->isDependentType(), |
| 6587 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6590 | //===----------------------------------------------------------------------===// |
| 6591 | // Clang Extensions. |
| 6592 | //===----------------------------------------------------------------------===// |
| 6593 | |
| 6594 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6595 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6596 | // Analyze block parameters. |
| 6597 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6598 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6599 | // Add BSI to CurBlock. |
| 6600 | BSI->PrevBlockInfo = CurBlock; |
| 6601 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6602 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6603 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6604 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 6605 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 6606 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6607 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 6608 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6609 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6610 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Ted Kremenek | 3cdff23 | 2009-12-07 22:01:30 +0000 | [diff] [blame] | 6611 | CurContext->addDecl(BSI->TheDecl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6612 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6615 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6616 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6617 | |
| 6618 | if (ParamInfo.getNumTypeObjects() == 0 |
| 6619 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6620 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6621 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 6622 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6623 | if (T->isArrayType()) { |
| 6624 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6625 | diag::err_block_returns_array); |
| 6626 | return; |
| 6627 | } |
| 6628 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6629 | // The parameter list is optional, if there was none, assume (). |
| 6630 | if (!T->isFunctionType()) |
| 6631 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6632 | |
| 6633 | CurBlock->hasPrototype = true; |
| 6634 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6635 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6636 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6637 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6638 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6639 | // FIXME: remove the attribute. |
| 6640 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6641 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6642 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6643 | // Do not allow returning a objc interface by-value. |
| 6644 | if (RetTy->isObjCInterfaceType()) { |
| 6645 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6646 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6647 | return; |
| 6648 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6649 | return; |
| 6650 | } |
| 6651 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6652 | // Analyze arguments to block. |
| 6653 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6654 | "Not a function declarator!"); |
| 6655 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6656 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6657 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6658 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6659 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6660 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6661 | // no arguments, not a function that takes a single void argument. |
| 6662 | if (FTI.hasPrototype && |
| 6663 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6664 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6665 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6666 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6667 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6668 | } else if (FTI.hasPrototype) { |
| 6669 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6670 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6671 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6672 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6673 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6674 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6675 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6676 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6677 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6678 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6679 | // If this has an identifier, add it to the scope stack. |
| 6680 | if ((*AI)->getIdentifier()) |
| 6681 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6682 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6683 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6684 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6685 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6686 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6687 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6688 | // FIXME: remove the attribute. |
| 6689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6690 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6691 | // Analyze the return type. |
| 6692 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6693 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6694 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6695 | // Do not allow returning a objc interface by-value. |
| 6696 | if (RetTy->isObjCInterfaceType()) { |
| 6697 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6698 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6699 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6700 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6704 | /// is invoked to pop the information about the block from the action impl. |
| 6705 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6706 | // Ensure that CurBlock is deleted. |
| 6707 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6708 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6709 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6710 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6711 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6712 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6713 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6714 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6718 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6719 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6720 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6721 | // If blocks are disabled, emit an error. |
| 6722 | if (!LangOpts.Blocks) |
| 6723 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6724 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6725 | // Ensure that CurBlock is deleted. |
| 6726 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6727 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6728 | PopDeclContext(); |
| 6729 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6730 | // Pop off CurBlock, handle nested blocks. |
| 6731 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6732 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6733 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6734 | if (!BSI->ReturnType.isNull()) |
| 6735 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6736 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6737 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6738 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6739 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6740 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6741 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6742 | QualType BlockTy; |
| 6743 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6744 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6745 | NoReturn); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6746 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6747 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6748 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6749 | NoReturn); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6750 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6751 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6752 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6753 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6754 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6755 | // If needed, diagnose invalid gotos and switches in the block. |
| 6756 | if (CurFunctionNeedsScopeChecking) |
| 6757 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6758 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6759 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6760 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6761 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6762 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6763 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6766 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6767 | ExprArg expr, TypeTy *type, |
| 6768 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6769 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6770 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6771 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6772 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6773 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6774 | |
| 6775 | // Get the va_list type |
| 6776 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6777 | if (VaListType->isArrayType()) { |
| 6778 | // Deal with implicit array decay; for example, on x86-64, |
| 6779 | // va_list is an array, but it's supposed to decay to |
| 6780 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6781 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6782 | // Make sure the input expression also decays appropriately. |
| 6783 | UsualUnaryConversions(E); |
| 6784 | } else { |
| 6785 | // Otherwise, the va_list argument must be an l-value because |
| 6786 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6787 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6788 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6789 | return ExprError(); |
| 6790 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6791 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6792 | if (!E->isTypeDependent() && |
| 6793 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6794 | return ExprError(Diag(E->getLocStart(), |
| 6795 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6796 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6797 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6798 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6799 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6800 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6801 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6802 | expr.release(); |
| 6803 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6804 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6807 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6808 | // The type of __null will be int or long, depending on the size of |
| 6809 | // pointers on the target. |
| 6810 | QualType Ty; |
| 6811 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6812 | Ty = Context.IntTy; |
| 6813 | else |
| 6814 | Ty = Context.LongTy; |
| 6815 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6816 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6817 | } |
| 6818 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6819 | static void |
| 6820 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6821 | QualType DstType, |
| 6822 | Expr *SrcExpr, |
| 6823 | CodeModificationHint &Hint) { |
| 6824 | if (!SemaRef.getLangOptions().ObjC1) |
| 6825 | return; |
| 6826 | |
| 6827 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6828 | if (!PT) |
| 6829 | return; |
| 6830 | |
| 6831 | // Check if the destination is of type 'id'. |
| 6832 | if (!PT->isObjCIdType()) { |
| 6833 | // Check if the destination is the 'NSString' interface. |
| 6834 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6835 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6836 | return; |
| 6837 | } |
| 6838 | |
| 6839 | // Strip off any parens and casts. |
| 6840 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6841 | if (!SL || SL->isWide()) |
| 6842 | return; |
| 6843 | |
| 6844 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6845 | } |
| 6846 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6847 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6848 | SourceLocation Loc, |
| 6849 | QualType DstType, QualType SrcType, |
| 6850 | Expr *SrcExpr, const char *Flavor) { |
| 6851 | // Decode the result (notice that AST's are still created for extensions). |
| 6852 | bool isInvalid = false; |
| 6853 | unsigned DiagKind; |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6854 | CodeModificationHint Hint; |
| 6855 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6856 | switch (ConvTy) { |
| 6857 | default: assert(0 && "Unknown conversion type"); |
| 6858 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6859 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6860 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6861 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6862 | case IntToPointer: |
| 6863 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6864 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6865 | case IncompatiblePointer: |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6866 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6867 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6868 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6869 | case IncompatiblePointerSign: |
| 6870 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6871 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6872 | case FunctionVoidPointer: |
| 6873 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6874 | break; |
| 6875 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 6876 | // If the qualifiers lost were because we were applying the |
| 6877 | // (deprecated) C++ conversion from a string literal to a char* |
| 6878 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 6879 | // Ideally, this check would be performed in |
| 6880 | // CheckPointerTypesForAssignment. However, that would require a |
| 6881 | // bit of refactoring (so that the second argument is an |
| 6882 | // expression, rather than a type), which should be done as part |
| 6883 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 6884 | // C++ semantics. |
| 6885 | if (getLangOptions().CPlusPlus && |
| 6886 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 6887 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6888 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 6889 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 6890 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 6891 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6892 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6893 | case IntToBlockPointer: |
| 6894 | DiagKind = diag::err_int_to_block_pointer; |
| 6895 | break; |
| 6896 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 6897 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6898 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6899 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6900 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6901 | // it can give a more specific diagnostic. |
| 6902 | DiagKind = diag::warn_incompatible_qualified_id; |
| 6903 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6904 | case IncompatibleVectors: |
| 6905 | DiagKind = diag::warn_incompatible_vectors; |
| 6906 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6907 | case Incompatible: |
| 6908 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 6909 | isInvalid = true; |
| 6910 | break; |
| 6911 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6912 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6913 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6914 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6915 | return isInvalid; |
| 6916 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6917 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 6918 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6919 | llvm::APSInt ICEResult; |
| 6920 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 6921 | if (Result) |
| 6922 | *Result = ICEResult; |
| 6923 | return false; |
| 6924 | } |
| 6925 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6926 | Expr::EvalResult EvalResult; |
| 6927 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6928 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6929 | EvalResult.HasSideEffects) { |
| 6930 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 6931 | |
| 6932 | if (EvalResult.Diag) { |
| 6933 | // We only show the note if it's not the usual "invalid subexpression" |
| 6934 | // or if it's actually in a subexpression. |
| 6935 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 6936 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 6937 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 6938 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6939 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6940 | return true; |
| 6941 | } |
| 6942 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6943 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 6944 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6945 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6946 | if (EvalResult.Diag && |
| 6947 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 6948 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6949 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6950 | if (Result) |
| 6951 | *Result = EvalResult.Val.getInt(); |
| 6952 | return false; |
| 6953 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6954 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6955 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6956 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6957 | ExprEvalContexts.push_back( |
| 6958 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6961 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6962 | Sema::PopExpressionEvaluationContext() { |
| 6963 | // Pop the current expression evaluation context off the stack. |
| 6964 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 6965 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6966 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 6967 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 6968 | if (Rec.PotentiallyReferenced) { |
| 6969 | // Mark any remaining declarations in the current position of the stack |
| 6970 | // as "referenced". If they were not meant to be referenced, semantic |
| 6971 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6972 | for (PotentiallyReferencedDecls::iterator |
| 6973 | I = Rec.PotentiallyReferenced->begin(), |
| 6974 | IEnd = Rec.PotentiallyReferenced->end(); |
| 6975 | I != IEnd; ++I) |
| 6976 | MarkDeclarationReferenced(I->first, I->second); |
| 6977 | } |
| 6978 | |
| 6979 | if (Rec.PotentiallyDiagnosed) { |
| 6980 | // Emit any pending diagnostics. |
| 6981 | for (PotentiallyEmittedDiagnostics::iterator |
| 6982 | I = Rec.PotentiallyDiagnosed->begin(), |
| 6983 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 6984 | I != IEnd; ++I) |
| 6985 | Diag(I->first, I->second); |
| 6986 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | // When are coming out of an unevaluated context, clear out any |
| 6990 | // temporaries that we may have created as part of the evaluation of |
| 6991 | // the expression in that context: they aren't relevant because they |
| 6992 | // will never be constructed. |
| 6993 | if (Rec.Context == Unevaluated && |
| 6994 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 6995 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 6996 | ExprTemporaries.end()); |
| 6997 | |
| 6998 | // Destroy the popped expression evaluation record. |
| 6999 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7000 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7001 | |
| 7002 | /// \brief Note that the given declaration was referenced in the source code. |
| 7003 | /// |
| 7004 | /// This routine should be invoke whenever a given declaration is referenced |
| 7005 | /// in the source code, and where that reference occurred. If this declaration |
| 7006 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 7007 | /// C99 6.9p3), then the declaration will be marked as used. |
| 7008 | /// |
| 7009 | /// \param Loc the location where the declaration was referenced. |
| 7010 | /// |
| 7011 | /// \param D the declaration that has been referenced by the source code. |
| 7012 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 7013 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7014 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7015 | if (D->isUsed()) |
| 7016 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7017 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 7018 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 7019 | // template or not. The reason for this is that unevaluated expressions |
| 7020 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 7021 | // -Wunused-parameters) |
| 7022 | if (isa<ParmVarDecl>(D) || |
| 7023 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7024 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7025 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7026 | // Do not mark anything as "used" within a dependent context; wait for |
| 7027 | // an instantiation. |
| 7028 | if (CurContext->isDependentContext()) |
| 7029 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7030 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7031 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7032 | case Unevaluated: |
| 7033 | // We are in an expression that is not potentially evaluated; do nothing. |
| 7034 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7035 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7036 | case PotentiallyEvaluated: |
| 7037 | // We are in a potentially-evaluated expression, so this declaration is |
| 7038 | // "used"; handle this below. |
| 7039 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7040 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7041 | case PotentiallyPotentiallyEvaluated: |
| 7042 | // We are in an expression that may be potentially evaluated; queue this |
| 7043 | // declaration reference until we know whether the expression is |
| 7044 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7045 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7046 | return; |
| 7047 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7048 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7049 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 7050 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7051 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7052 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 7053 | if (!Constructor->isUsed()) |
| 7054 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7055 | } else if (Constructor->isImplicit() && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7056 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7057 | if (!Constructor->isUsed()) |
| 7058 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7059 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7060 | |
| 7061 | MaybeMarkVirtualMembersReferenced(Loc, Constructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7062 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 7063 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 7064 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7065 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7066 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7067 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7068 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 7069 | if (!MethodDecl->isUsed()) |
| 7070 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 7071 | } |
| 7072 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7073 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7074 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7075 | // class templates. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7076 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7077 | bool AlreadyInstantiated = false; |
| 7078 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7079 | = Function->getTemplateSpecializationInfo()) { |
| 7080 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7081 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7082 | else if (SpecInfo->getTemplateSpecializationKind() |
| 7083 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7084 | AlreadyInstantiated = true; |
| 7085 | } else if (MemberSpecializationInfo *MSInfo |
| 7086 | = Function->getMemberSpecializationInfo()) { |
| 7087 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7088 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7089 | else if (MSInfo->getTemplateSpecializationKind() |
| 7090 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7091 | AlreadyInstantiated = true; |
| 7092 | } |
| 7093 | |
| 7094 | if (!AlreadyInstantiated) |
| 7095 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
| 7096 | } |
| 7097 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7098 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7099 | Function->setUsed(true); |
| 7100 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7102 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7103 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7104 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7105 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7106 | Var->getInstantiatedFromStaticDataMember()) { |
| 7107 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7108 | assert(MSInfo && "Missing member specialization information?"); |
| 7109 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7110 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7111 | MSInfo->setPointOfInstantiation(Loc); |
| 7112 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 7113 | } |
| 7114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7115 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7116 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7117 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7118 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7119 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7120 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7121 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7122 | |
| 7123 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7124 | CallExpr *CE, FunctionDecl *FD) { |
| 7125 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7126 | return false; |
| 7127 | |
| 7128 | PartialDiagnostic Note = |
| 7129 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7130 | << FD->getDeclName() : PDiag(); |
| 7131 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 7132 | |
| 7133 | if (RequireCompleteType(Loc, ReturnType, |
| 7134 | FD ? |
| 7135 | PDiag(diag::err_call_function_incomplete_return) |
| 7136 | << CE->getSourceRange() << FD->getDeclName() : |
| 7137 | PDiag(diag::err_call_incomplete_return) |
| 7138 | << CE->getSourceRange(), |
| 7139 | std::make_pair(NoteLoc, Note))) |
| 7140 | return true; |
| 7141 | |
| 7142 | return false; |
| 7143 | } |
| 7144 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7145 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7146 | // will prevent this condition from triggering, which is what we want. |
| 7147 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7148 | SourceLocation Loc; |
| 7149 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7150 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7151 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7152 | if (isa<BinaryOperator>(E)) { |
| 7153 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 7154 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 7155 | return; |
| 7156 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7157 | // Greylist some idioms by putting them into a warning subcategory. |
| 7158 | if (ObjCMessageExpr *ME |
| 7159 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7160 | Selector Sel = ME->getSelector(); |
| 7161 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7162 | // self = [<foo> init...] |
| 7163 | if (isSelfExpr(Op->getLHS()) |
| 7164 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7165 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7166 | |
| 7167 | // <foo> = [<bar> nextObject] |
| 7168 | else if (Sel.isUnarySelector() && |
| 7169 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7170 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7171 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7172 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7173 | Loc = Op->getOperatorLoc(); |
| 7174 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7175 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7176 | if (Op->getOperator() != OO_Equal) |
| 7177 | return; |
| 7178 | |
| 7179 | Loc = Op->getOperatorLoc(); |
| 7180 | } else { |
| 7181 | // Not an assignment. |
| 7182 | return; |
| 7183 | } |
| 7184 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7185 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7186 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7187 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7188 | Diag(Loc, diagnostic) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7189 | << E->getSourceRange() |
| 7190 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 7191 | << CodeModificationHint::CreateInsertion(Close, ")"); |
| 7192 | } |
| 7193 | |
| 7194 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7195 | DiagnoseAssignmentAsCondition(E); |
| 7196 | |
| 7197 | if (!E->isTypeDependent()) { |
| 7198 | DefaultFunctionArrayConversion(E); |
| 7199 | |
| 7200 | QualType T = E->getType(); |
| 7201 | |
| 7202 | if (getLangOptions().CPlusPlus) { |
| 7203 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7204 | return true; |
| 7205 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7206 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7207 | << T << E->getSourceRange(); |
| 7208 | return true; |
| 7209 | } |
| 7210 | } |
| 7211 | |
| 7212 | return false; |
| 7213 | } |