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 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 262 | if (Expr->getType()->isObjCInterfaceType() && |
| 263 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 264 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 265 | << Expr->getType() << CT)) |
| 266 | return true; |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 268 | if (!Expr->getType()->isPODType() && |
| 269 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 270 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 271 | << Expr->getType() << CT)) |
| 272 | return true; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 273 | |
| 274 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 278 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 279 | /// 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] | 280 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 281 | /// responsible for emitting appropriate error diagnostics. |
| 282 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 283 | /// GCC. |
| 284 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 285 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 286 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 287 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 288 | |
| 289 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 290 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 292 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 293 | QualType lhs = |
| 294 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 296 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 297 | |
| 298 | // If both types are identical, no conversion is needed. |
| 299 | if (lhs == rhs) |
| 300 | return lhs; |
| 301 | |
| 302 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 303 | // The caller can deal with this (e.g. pointer + int). |
| 304 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 305 | return lhs; |
| 306 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 307 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 308 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 309 | if (!LHSBitfieldPromoteTy.isNull()) |
| 310 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 311 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 312 | if (!RHSBitfieldPromoteTy.isNull()) |
| 313 | rhs = RHSBitfieldPromoteTy; |
| 314 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 315 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 316 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 317 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 318 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 319 | return destType; |
| 320 | } |
| 321 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 322 | //===----------------------------------------------------------------------===// |
| 323 | // Semantic Analysis for various Expression Types |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 327 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 329 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 330 | /// multiple tokens. However, the common case is that StringToks points to one |
| 331 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 332 | /// |
| 333 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 334 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | assert(NumStringToks && "Must have at least one string!"); |
| 336 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 337 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 339 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | |
| 341 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 342 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 343 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 344 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 345 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 346 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 347 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 348 | |
| 349 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 350 | if (getLangOptions().CPlusPlus) |
| 351 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 352 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 353 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 354 | // the nul terminator character as well as the string length for pascal |
| 355 | // strings. |
| 356 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 357 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 358 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 362 | Literal.GetStringLength(), |
| 363 | Literal.AnyWide, StrTy, |
| 364 | &StringTokLocs[0], |
| 365 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 368 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 369 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 370 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 371 | /// for values inside the block or for globals). |
| 372 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 373 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 374 | /// up-to-date. |
| 375 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 376 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 377 | ValueDecl *VD) { |
| 378 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 379 | // we wanted to. |
| 380 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 381 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 383 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 384 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 385 | return false; |
| 386 | |
| 387 | // If this is a reference to an extern, static, or global variable, no need to |
| 388 | // snapshot it. |
| 389 | // FIXME: What about 'const' variables in C++? |
| 390 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 391 | if (!Var->hasLocalStorage()) |
| 392 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 394 | // Blocks that have these can't be constant. |
| 395 | CurBlock->hasBlockDeclRefExprs = true; |
| 396 | |
| 397 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 398 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 399 | // be defined outside all of the current blocks (in which case the blocks do |
| 400 | // all get the bit). Walk the nesting chain. |
| 401 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 402 | NextBlock = NextBlock->PrevBlockInfo) { |
| 403 | // If we found the defining block for the variable, don't mark the block as |
| 404 | // having a reference outside it. |
| 405 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 406 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 408 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 409 | // a snapshot as well. |
| 410 | NextBlock->hasBlockDeclRefExprs = true; |
| 411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 413 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 416 | |
| 417 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 418 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 419 | Sema::OwningExprResult |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 420 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 421 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 422 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 423 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 425 | << D->getDeclName(); |
| 426 | return ExprError(); |
| 427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 429 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 430 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 431 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 432 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 434 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 436 | << D->getIdentifier(); |
| 437 | return ExprError(); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 443 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 445 | return Owned(DeclRefExpr::Create(Context, |
| 446 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 447 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 448 | D, Loc, Ty)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 451 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 452 | /// variable corresponding to the anonymous union or struct whose type |
| 453 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 454 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 455 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 457 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 459 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 460 | // operation rather than a slow walk through DeclContext's vector (which |
| 461 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 462 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 464 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 465 | D != DEnd; ++D) { |
| 466 | if (*D == Record) { |
| 467 | // The object for the anonymous struct/union directly |
| 468 | // follows its type in the list of declarations. |
| 469 | ++D; |
| 470 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 471 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 472 | return *D; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | assert(false && "Missing object for anonymous record"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 480 | /// \brief Given a field that represents a member of an anonymous |
| 481 | /// struct/union, build the path from that field's context to the |
| 482 | /// actual member. |
| 483 | /// |
| 484 | /// Construct the sequence of field member references we'll have to |
| 485 | /// perform to get to the field in the anonymous union/struct. The |
| 486 | /// list of members is built from the field outward, so traverse it |
| 487 | /// backwards to go from an object in the current context to the field |
| 488 | /// we found. |
| 489 | /// |
| 490 | /// \returns The variable from which the field access should begin, |
| 491 | /// for an anonymous struct/union that is not a member of another |
| 492 | /// class. Otherwise, returns NULL. |
| 493 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 494 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 495 | assert(Field->getDeclContext()->isRecord() && |
| 496 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 497 | && "Field must be stored inside an anonymous struct or union"); |
| 498 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 499 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 500 | VarDecl *BaseObject = 0; |
| 501 | DeclContext *Ctx = Field->getDeclContext(); |
| 502 | do { |
| 503 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 504 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 505 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 506 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 507 | else { |
| 508 | BaseObject = cast<VarDecl>(AnonObject); |
| 509 | break; |
| 510 | } |
| 511 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 513 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 514 | |
| 515 | return BaseObject; |
| 516 | } |
| 517 | |
| 518 | Sema::OwningExprResult |
| 519 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 520 | FieldDecl *Field, |
| 521 | Expr *BaseObjectExpr, |
| 522 | SourceLocation OpLoc) { |
| 523 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 525 | AnonFields); |
| 526 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 527 | // Build the expression that refers to the base object, from |
| 528 | // which we will build a sequence of member references to each |
| 529 | // of the anonymous union objects and, eventually, the field we |
| 530 | // found via name lookup. |
| 531 | bool BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 532 | Qualifiers BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 533 | if (BaseObject) { |
| 534 | // BaseObject is an anonymous struct/union variable (and is, |
| 535 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 536 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 537 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 538 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 539 | SourceLocation()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 540 | BaseQuals |
| 541 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 542 | } else if (BaseObjectExpr) { |
| 543 | // The caller provided the base object expression. Determine |
| 544 | // whether its a pointer and whether it adds any qualifiers to the |
| 545 | // anonymous struct/union fields we're looking into. |
| 546 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 547 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 548 | BaseObjectIsPointer = true; |
| 549 | ObjectType = ObjectPtr->getPointeeType(); |
| 550 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 551 | BaseQuals |
| 552 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 553 | } else { |
| 554 | // We've found a member of an anonymous struct/union that is |
| 555 | // inside a non-anonymous struct/union, so in a well-formed |
| 556 | // program our base object expression is "this". |
| 557 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 558 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 560 | = Context.getTagDeclType( |
| 561 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 562 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 564 | == Context.getCanonicalType(ThisType)) || |
| 565 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 566 | // Our base object expression is "this". |
Douglas Gregor | 8aa5f40 | 2009-12-24 20:23:34 +0000 | [diff] [blame] | 567 | BaseObjectExpr = new (Context) CXXThisExpr(Loc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 568 | MD->getThisType(Context), |
| 569 | /*isImplicit=*/true); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 570 | BaseObjectIsPointer = true; |
| 571 | } |
| 572 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 573 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 574 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 575 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 576 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 580 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 581 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Build the implicit member references to the field of the |
| 585 | // anonymous struct/union. |
| 586 | Expr *Result = BaseObjectExpr; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 587 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 588 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 589 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 590 | FI != FIEnd; ++FI) { |
| 591 | QualType MemberType = (*FI)->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 592 | Qualifiers MemberTypeQuals = |
| 593 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 594 | |
| 595 | // CVR attributes from the base are picked up by members, |
| 596 | // except that 'mutable' members don't pick up 'const'. |
| 597 | if ((*FI)->isMutable()) |
| 598 | ResultQuals.removeConst(); |
| 599 | |
| 600 | // GC attributes are never picked up by members. |
| 601 | ResultQuals.removeObjCGCAttr(); |
| 602 | |
| 603 | // TR 18037 does not allow fields to be declared with address spaces. |
| 604 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 605 | |
| 606 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 607 | if (NewQuals != MemberTypeQuals) |
| 608 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 609 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 610 | MarkDeclarationReferenced(Loc, *FI); |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 611 | PerformObjectMemberConversion(Result, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 612 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 613 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 614 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 615 | BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 616 | ResultQuals = NewQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 619 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 620 | } |
| 621 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 622 | /// Decomposes the given name into a DeclarationName, its location, and |
| 623 | /// possibly a list of template arguments. |
| 624 | /// |
| 625 | /// If this produces template arguments, it is permitted to call |
| 626 | /// DecomposeTemplateName. |
| 627 | /// |
| 628 | /// This actually loses a lot of source location information for |
| 629 | /// non-standard name kinds; we should consider preserving that in |
| 630 | /// some way. |
| 631 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 632 | const UnqualifiedId &Id, |
| 633 | TemplateArgumentListInfo &Buffer, |
| 634 | DeclarationName &Name, |
| 635 | SourceLocation &NameLoc, |
| 636 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 637 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 638 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 639 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 640 | |
| 641 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 642 | Id.TemplateId->getTemplateArgs(), |
| 643 | Id.TemplateId->NumArgs); |
| 644 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 645 | TemplateArgsPtr.release(); |
| 646 | |
| 647 | TemplateName TName = |
| 648 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 649 | |
| 650 | Name = SemaRef.Context.getNameForTemplate(TName); |
| 651 | NameLoc = Id.TemplateId->TemplateNameLoc; |
| 652 | TemplateArgs = &Buffer; |
| 653 | } else { |
| 654 | Name = SemaRef.GetNameFromUnqualifiedId(Id); |
| 655 | NameLoc = Id.StartLocation; |
| 656 | TemplateArgs = 0; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /// Decompose the given template name into a list of lookup results. |
| 661 | /// |
| 662 | /// The unqualified ID must name a non-dependent template, which can |
| 663 | /// be more easily tested by checking whether DecomposeUnqualifiedId |
| 664 | /// found template arguments. |
| 665 | static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) { |
| 666 | assert(Id.getKind() == UnqualifiedId::IK_TemplateId); |
| 667 | TemplateName TName = |
| 668 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 669 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 670 | if (TemplateDecl *TD = TName.getAsTemplateDecl()) |
| 671 | R.addDecl(TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 672 | else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate()) |
| 673 | for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end(); |
| 674 | I != E; ++I) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 675 | R.addDecl(*I); |
John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 676 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 677 | R.resolveKind(); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 678 | } |
| 679 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 680 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
| 681 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 682 | E = Record->bases_end(); I != E; ++I) { |
| 683 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 684 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 685 | if (!BaseRT) return false; |
| 686 | |
| 687 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 688 | if (!BaseRecord->isDefinition() || |
| 689 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | return true; |
| 694 | } |
| 695 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 696 | /// Determines whether we can lookup this id-expression now or whether |
| 697 | /// we have to wait until template instantiation is complete. |
| 698 | static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 699 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 700 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 701 | // If the qualifier scope isn't computable, it's definitely dependent. |
| 702 | if (!DC) return true; |
| 703 | |
| 704 | // If the qualifier scope doesn't name a record, we can always look into it. |
| 705 | if (!isa<CXXRecordDecl>(DC)) return false; |
| 706 | |
| 707 | // We can't look into record types unless they're fully-formed. |
| 708 | if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true; |
| 709 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 710 | return false; |
| 711 | } |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 712 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 713 | /// Determines if the given class is provably not derived from all of |
| 714 | /// the prospective base classes. |
| 715 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 716 | CXXRecordDecl *Record, |
| 717 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 718 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 719 | return false; |
| 720 | |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 721 | RecordDecl *RD = Record->getDefinition(SemaRef.Context); |
| 722 | if (!RD) return false; |
| 723 | Record = cast<CXXRecordDecl>(RD); |
| 724 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 725 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 726 | E = Record->bases_end(); I != E; ++I) { |
| 727 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 728 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 729 | if (!BaseRT) return false; |
| 730 | |
| 731 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 732 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | return true; |
| 737 | } |
| 738 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 739 | /// Determines if this is an instance member of a class. |
| 740 | static bool IsInstanceMember(NamedDecl *D) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 741 | assert(D->isCXXClassMember() && |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 742 | "checking whether non-member is instance member"); |
| 743 | |
| 744 | if (isa<FieldDecl>(D)) return true; |
| 745 | |
| 746 | if (isa<CXXMethodDecl>(D)) |
| 747 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 748 | |
| 749 | if (isa<FunctionTemplateDecl>(D)) { |
| 750 | D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 751 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 752 | } |
| 753 | |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | enum IMAKind { |
| 758 | /// The reference is definitely not an instance member access. |
| 759 | IMA_Static, |
| 760 | |
| 761 | /// The reference may be an implicit instance member access. |
| 762 | IMA_Mixed, |
| 763 | |
| 764 | /// The reference may be to an instance member, but it is invalid if |
| 765 | /// so, because the context is not an instance method. |
| 766 | IMA_Mixed_StaticContext, |
| 767 | |
| 768 | /// The reference may be to an instance member, but it is invalid if |
| 769 | /// so, because the context is from an unrelated class. |
| 770 | IMA_Mixed_Unrelated, |
| 771 | |
| 772 | /// The reference is definitely an implicit instance member access. |
| 773 | IMA_Instance, |
| 774 | |
| 775 | /// The reference may be to an unresolved using declaration. |
| 776 | IMA_Unresolved, |
| 777 | |
| 778 | /// The reference may be to an unresolved using declaration and the |
| 779 | /// context is not an instance method. |
| 780 | IMA_Unresolved_StaticContext, |
| 781 | |
| 782 | /// The reference is to a member of an anonymous structure in a |
| 783 | /// non-class context. |
| 784 | IMA_AnonymousMember, |
| 785 | |
| 786 | /// All possible referrents are instance members and the current |
| 787 | /// context is not an instance method. |
| 788 | IMA_Error_StaticContext, |
| 789 | |
| 790 | /// All possible referrents are instance members of an unrelated |
| 791 | /// class. |
| 792 | IMA_Error_Unrelated |
| 793 | }; |
| 794 | |
| 795 | /// The given lookup names class member(s) and is not being used for |
| 796 | /// an address-of-member expression. Classify the type of access |
| 797 | /// according to whether it's possible that this reference names an |
| 798 | /// instance member. This is best-effort; it is okay to |
| 799 | /// conservatively answer "yes", in which case some errors will simply |
| 800 | /// not be caught until template-instantiation. |
| 801 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 802 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 803 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 804 | |
| 805 | bool isStaticContext = |
| 806 | (!isa<CXXMethodDecl>(SemaRef.CurContext) || |
| 807 | cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic()); |
| 808 | |
| 809 | if (R.isUnresolvableResult()) |
| 810 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 811 | |
| 812 | // Collect all the declaring classes of instance members we find. |
| 813 | bool hasNonInstance = false; |
| 814 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 815 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 816 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 817 | if (IsInstanceMember(D)) { |
| 818 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 819 | |
| 820 | // If this is a member of an anonymous record, move out to the |
| 821 | // innermost non-anonymous struct or union. If there isn't one, |
| 822 | // that's a special case. |
| 823 | while (R->isAnonymousStructOrUnion()) { |
| 824 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 825 | if (!R) return IMA_AnonymousMember; |
| 826 | } |
| 827 | Classes.insert(R->getCanonicalDecl()); |
| 828 | } |
| 829 | else |
| 830 | hasNonInstance = true; |
| 831 | } |
| 832 | |
| 833 | // If we didn't find any instance members, it can't be an implicit |
| 834 | // member reference. |
| 835 | if (Classes.empty()) |
| 836 | return IMA_Static; |
| 837 | |
| 838 | // If the current context is not an instance method, it can't be |
| 839 | // an implicit member reference. |
| 840 | if (isStaticContext) |
| 841 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 842 | |
| 843 | // If we can prove that the current context is unrelated to all the |
| 844 | // declaring classes, it can't be an implicit member reference (in |
| 845 | // which case it's an error if any of those members are selected). |
| 846 | if (IsProvablyNotDerivedFrom(SemaRef, |
| 847 | cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(), |
| 848 | Classes)) |
| 849 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 850 | |
| 851 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 852 | } |
| 853 | |
| 854 | /// Diagnose a reference to a field with no object available. |
| 855 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 856 | const CXXScopeSpec &SS, |
| 857 | const LookupResult &R) { |
| 858 | SourceLocation Loc = R.getNameLoc(); |
| 859 | SourceRange Range(Loc); |
| 860 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 861 | |
| 862 | if (R.getAsSingle<FieldDecl>()) { |
| 863 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 864 | if (MD->isStatic()) { |
| 865 | // "invalid use of member 'x' in static member function" |
| 866 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 867 | << Range << R.getLookupName(); |
| 868 | return; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 873 | << R.getLookupName() << Range; |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 878 | } |
| 879 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 880 | /// Diagnose an empty lookup. |
| 881 | /// |
| 882 | /// \return false if new lookup candidates were found |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 883 | bool Sema::DiagnoseEmptyLookup(Scope *S, const CXXScopeSpec &SS, |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 884 | LookupResult &R) { |
| 885 | DeclarationName Name = R.getLookupName(); |
| 886 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 887 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 888 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 889 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 890 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 891 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 892 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 893 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 894 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 896 | // If the original lookup was an unqualified lookup, fake an |
| 897 | // unqualified lookup. This is useful when (for example) the |
| 898 | // original lookup would not have found something because it was a |
| 899 | // dependent name. |
| 900 | for (DeclContext *DC = SS.isEmpty()? CurContext : 0; |
| 901 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 902 | if (isa<CXXRecordDecl>(DC)) { |
| 903 | LookupQualifiedName(R, DC); |
| 904 | |
| 905 | if (!R.empty()) { |
| 906 | // Don't give errors about ambiguities in this lookup. |
| 907 | R.suppressDiagnostics(); |
| 908 | |
| 909 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 910 | bool isInstance = CurMethod && |
| 911 | CurMethod->isInstance() && |
| 912 | DC == CurMethod->getParent(); |
| 913 | |
| 914 | // Give a code modification hint to insert 'this->'. |
| 915 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 916 | // Actually quite difficult! |
| 917 | if (isInstance) |
| 918 | Diag(R.getNameLoc(), diagnostic) << Name |
| 919 | << CodeModificationHint::CreateInsertion(R.getNameLoc(), |
| 920 | "this->"); |
| 921 | else |
| 922 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 923 | |
| 924 | // Do we really want to note all of these? |
| 925 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 926 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 927 | |
| 928 | // Tell the callee to try to recover. |
| 929 | return false; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 934 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 935 | if (S && CorrectTypo(R, S, &SS)) { |
| 936 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 937 | if (SS.isEmpty()) |
| 938 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 939 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 940 | R.getLookupName().getAsString()); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 941 | else |
| 942 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 943 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 944 | << SS.getRange() |
| 945 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 946 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 947 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 948 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 949 | << ND->getDeclName(); |
| 950 | |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 951 | // Tell the callee to try to recover. |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 956 | // FIXME: If we ended up with a typo for a type name or |
| 957 | // Objective-C class name, we're in trouble because the parser |
| 958 | // is in the wrong place to recover. Suggest the typo |
| 959 | // correction, but don't make it a fix-it since we're not going |
| 960 | // to recover well anyway. |
| 961 | if (SS.isEmpty()) |
| 962 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 963 | else |
| 964 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 965 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 966 | << SS.getRange(); |
| 967 | |
| 968 | // Don't try to recover; it won't work. |
| 969 | return true; |
| 970 | } |
| 971 | |
| 972 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | // Emit a special diagnostic for failed member lookups. |
| 976 | // FIXME: computing the declaration context might fail here (?) |
| 977 | if (!SS.isEmpty()) { |
| 978 | Diag(R.getNameLoc(), diag::err_no_member) |
| 979 | << Name << computeDeclContext(SS, false) |
| 980 | << SS.getRange(); |
| 981 | return true; |
| 982 | } |
| 983 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 984 | // Give up, we can't recover. |
| 985 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 986 | return true; |
| 987 | } |
| 988 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 989 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 990 | const CXXScopeSpec &SS, |
| 991 | UnqualifiedId &Id, |
| 992 | bool HasTrailingLParen, |
| 993 | bool isAddressOfOperand) { |
| 994 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 995 | "cannot be direct & operand and have a trailing lparen"); |
| 996 | |
| 997 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 998 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 999 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1000 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Decompose the UnqualifiedId into the following data. |
| 1003 | DeclarationName Name; |
| 1004 | SourceLocation NameLoc; |
| 1005 | const TemplateArgumentListInfo *TemplateArgs; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1006 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 1007 | Name, NameLoc, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1008 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1009 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1010 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1011 | // C++ [temp.dep.expr]p3: |
| 1012 | // An id-expression is type-dependent if it contains: |
| 1013 | // -- a nested-name-specifier that contains a class-name that |
| 1014 | // names a dependent type. |
| 1015 | // Determine whether this is a member of an unknown specialization; |
| 1016 | // we need to handle these differently. |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 1017 | if (SS.isSet() && IsDependentIdExpression(*this, SS)) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1018 | return ActOnDependentIdExpression(SS, Name, NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 1019 | isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1020 | TemplateArgs); |
| 1021 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1022 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1023 | // Perform the required lookup. |
| 1024 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1025 | if (TemplateArgs) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1026 | // Just re-use the lookup done by isTemplateName. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1027 | DecomposeTemplateName(R, Id); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1028 | } else { |
| 1029 | LookupParsedName(R, S, &SS, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1031 | // If this reference is in an Objective-C method, then we need to do |
| 1032 | // some special Objective-C lookup, too. |
| 1033 | if (!SS.isSet() && II && getCurMethodDecl()) { |
| 1034 | OwningExprResult E(LookupInObjCMethod(R, S, II)); |
| 1035 | if (E.isInvalid()) |
| 1036 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1038 | Expr *Ex = E.takeAs<Expr>(); |
| 1039 | if (Ex) return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1041 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1042 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1043 | if (R.isAmbiguous()) |
| 1044 | return ExprError(); |
| 1045 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1046 | // Determine whether this name might be a candidate for |
| 1047 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1048 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1049 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1050 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1051 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1052 | // in C90, extension in C99, forbidden in C++). |
| 1053 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1054 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1055 | if (D) R.addDecl(D); |
| 1056 | } |
| 1057 | |
| 1058 | // If this name wasn't predeclared and if this is not a function |
| 1059 | // call, diagnose the problem. |
| 1060 | if (R.empty()) { |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1061 | if (DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1062 | return ExprError(); |
| 1063 | |
| 1064 | assert(!R.empty() && |
| 1065 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1066 | |
| 1067 | // If we found an Objective-C instance variable, let |
| 1068 | // LookupInObjCMethod build the appropriate expression to |
| 1069 | // reference the ivar. |
| 1070 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1071 | R.clear(); |
| 1072 | OwningExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
| 1073 | assert(E.isInvalid() || E.get()); |
| 1074 | return move(E); |
| 1075 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | } |
| 1077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1079 | // This is guaranteed from this point on. |
| 1080 | assert(!R.empty() || ADL); |
| 1081 | |
| 1082 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1083 | // Warn about constructs like: |
| 1084 | // if (void *X = foo()) { ... } else { X }. |
| 1085 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1087 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1088 | Scope *CheckS = S; |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1089 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1091 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1092 | ExprError(Diag(NameLoc, diag::warn_value_always_zero) |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1093 | << Var->getDeclName() |
| 1094 | << (Var->getType()->isPointerType()? 2 : |
| 1095 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1099 | // Move to the parent of this scope. |
| 1100 | CheckS = CheckS->getParent(); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1103 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1104 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1105 | // C99 DR 316 says that, if a function type comes from a |
| 1106 | // function definition (without a prototype), that type is only |
| 1107 | // used for checking compatibility. Therefore, when referencing |
| 1108 | // the function, we pretend that we don't have the full function |
| 1109 | // type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1110 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1111 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1112 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1113 | QualType T = Func->getType(); |
| 1114 | QualType NoProtoType = T; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1115 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1116 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1117 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1121 | // Check whether this might be a C++ implicit instance member access. |
| 1122 | // C++ [expr.prim.general]p6: |
| 1123 | // Within the definition of a non-static member function, an |
| 1124 | // identifier that names a non-static member is transformed to a |
| 1125 | // class member access expression. |
| 1126 | // But note that &SomeClass::foo is grammatically distinct, even |
| 1127 | // though we don't parse it that way. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1128 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1129 | bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1130 | if (!isAbstractMemberPointer) |
| 1131 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1134 | if (TemplateArgs) |
| 1135 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1136 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1137 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1138 | } |
| 1139 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1140 | /// Builds an expression which might be an implicit member expression. |
| 1141 | Sema::OwningExprResult |
| 1142 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1143 | LookupResult &R, |
| 1144 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1145 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1146 | case IMA_Instance: |
| 1147 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1148 | |
| 1149 | case IMA_AnonymousMember: |
| 1150 | assert(R.isSingleResult()); |
| 1151 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1152 | R.getAsSingle<FieldDecl>()); |
| 1153 | |
| 1154 | case IMA_Mixed: |
| 1155 | case IMA_Mixed_Unrelated: |
| 1156 | case IMA_Unresolved: |
| 1157 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1158 | |
| 1159 | case IMA_Static: |
| 1160 | case IMA_Mixed_StaticContext: |
| 1161 | case IMA_Unresolved_StaticContext: |
| 1162 | if (TemplateArgs) |
| 1163 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1164 | return BuildDeclarationNameExpr(SS, R, false); |
| 1165 | |
| 1166 | case IMA_Error_StaticContext: |
| 1167 | case IMA_Error_Unrelated: |
| 1168 | DiagnoseInstanceReference(*this, SS, R); |
| 1169 | return ExprError(); |
| 1170 | } |
| 1171 | |
| 1172 | llvm_unreachable("unexpected instance member access kind"); |
| 1173 | return ExprError(); |
| 1174 | } |
| 1175 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1176 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1177 | /// declaration name, generally during template instantiation. |
| 1178 | /// There's a large number of things which don't need to be done along |
| 1179 | /// this path. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1180 | Sema::OwningExprResult |
| 1181 | Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS, |
| 1182 | DeclarationName Name, |
| 1183 | SourceLocation NameLoc) { |
| 1184 | DeclContext *DC; |
| 1185 | if (!(DC = computeDeclContext(SS, false)) || |
| 1186 | DC->isDependentContext() || |
| 1187 | RequireCompleteDeclContext(SS)) |
| 1188 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0); |
| 1189 | |
| 1190 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1191 | LookupQualifiedName(R, DC); |
| 1192 | |
| 1193 | if (R.isAmbiguous()) |
| 1194 | return ExprError(); |
| 1195 | |
| 1196 | if (R.empty()) { |
| 1197 | Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange(); |
| 1198 | return ExprError(); |
| 1199 | } |
| 1200 | |
| 1201 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1202 | } |
| 1203 | |
| 1204 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1205 | /// detected that we're currently inside an ObjC method. Perform some |
| 1206 | /// additional lookup. |
| 1207 | /// |
| 1208 | /// Ideally, most of this would be done by lookup, but there's |
| 1209 | /// actually quite a lot of extra work involved. |
| 1210 | /// |
| 1211 | /// Returns a null sentinel to indicate trivial success. |
| 1212 | Sema::OwningExprResult |
| 1213 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
| 1214 | IdentifierInfo *II) { |
| 1215 | SourceLocation Loc = Lookup.getNameLoc(); |
| 1216 | |
| 1217 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1218 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1219 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1220 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1221 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1222 | |
| 1223 | // If we're in a class method, we don't normally want to look for |
| 1224 | // ivars. But if we don't find anything else, and there's an |
| 1225 | // ivar, that's an error. |
| 1226 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 1227 | |
| 1228 | bool LookForIvars; |
| 1229 | if (Lookup.empty()) |
| 1230 | LookForIvars = true; |
| 1231 | else if (IsClassMethod) |
| 1232 | LookForIvars = false; |
| 1233 | else |
| 1234 | LookForIvars = (Lookup.isSingleResult() && |
| 1235 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1236 | |
| 1237 | if (LookForIvars) { |
| 1238 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1239 | ObjCInterfaceDecl *ClassDeclared; |
| 1240 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1241 | // Diagnose using an ivar in a class method. |
| 1242 | if (IsClassMethod) |
| 1243 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1244 | << IV->getDeclName()); |
| 1245 | |
| 1246 | // If we're referencing an invalid decl, just return this as a silent |
| 1247 | // error node. The error diagnostic was already emitted on the decl. |
| 1248 | if (IV->isInvalidDecl()) |
| 1249 | return ExprError(); |
| 1250 | |
| 1251 | // Check if referencing a field with __attribute__((deprecated)). |
| 1252 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1253 | return ExprError(); |
| 1254 | |
| 1255 | // Diagnose the use of an ivar outside of the declaring class. |
| 1256 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1257 | ClassDeclared != IFace) |
| 1258 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1259 | |
| 1260 | // FIXME: This should use a new expr for a direct reference, don't |
| 1261 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1262 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1263 | UnqualifiedId SelfName; |
| 1264 | SelfName.setIdentifier(&II, SourceLocation()); |
| 1265 | CXXScopeSpec SelfScopeSpec; |
| 1266 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 1267 | SelfName, false, false); |
| 1268 | MarkDeclarationReferenced(Loc, IV); |
| 1269 | return Owned(new (Context) |
| 1270 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1271 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1272 | } |
| 1273 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
| 1274 | // We should warn if a local variable hides an ivar. |
| 1275 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1276 | ObjCInterfaceDecl *ClassDeclared; |
| 1277 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1278 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1279 | IFace == ClassDeclared) |
| 1280 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | // Needed to implement property "super.method" notation. |
| 1285 | if (Lookup.empty() && II->isStr("super")) { |
| 1286 | QualType T; |
| 1287 | |
| 1288 | if (getCurMethodDecl()->isInstanceMethod()) |
| 1289 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 1290 | getCurMethodDecl()->getClassInterface())); |
| 1291 | else |
| 1292 | T = Context.getObjCClassType(); |
| 1293 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
| 1294 | } |
| 1295 | |
| 1296 | // Sentinel value saying that we didn't do anything special. |
| 1297 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1298 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1299 | |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1300 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1301 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1302 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 1303 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1305 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1307 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1308 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 1309 | return false; |
| 1310 | QualType FromRecordType = From->getType(); |
| 1311 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1312 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1313 | DestType = Context.getPointerType(DestType); |
| 1314 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1315 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1316 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 1317 | CheckDerivedToBaseConversion(FromRecordType, |
| 1318 | DestRecordType, |
| 1319 | From->getSourceRange().getBegin(), |
| 1320 | From->getSourceRange())) |
| 1321 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 1322 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 1323 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1324 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1325 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1326 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1327 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1328 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1330 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1331 | SourceLocation Loc, QualType Ty, |
| 1332 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1333 | NestedNameSpecifier *Qualifier = 0; |
| 1334 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1335 | if (SS.isSet()) { |
| 1336 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1337 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1340 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
| 1341 | Member, Loc, TemplateArgs, Ty); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1344 | /// Builds an implicit member access expression. The current context |
| 1345 | /// is known to be an instance method, and the given unqualified lookup |
| 1346 | /// set is known to contain only instance members, at least one of which |
| 1347 | /// is from an appropriate type. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1348 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1349 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1350 | LookupResult &R, |
| 1351 | const TemplateArgumentListInfo *TemplateArgs, |
| 1352 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1353 | assert(!R.empty() && !R.isAmbiguous()); |
| 1354 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1355 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1356 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1357 | // We may have found a field within an anonymous union or struct |
| 1358 | // (C++ [class.union]). |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1359 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1360 | // FIXME: template-ids inside anonymous structs? |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1361 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1362 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1363 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1364 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1365 | // If this is known to be an instance access, go ahead and build a |
| 1366 | // 'this' expression now. |
| 1367 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 1368 | Expr *This = 0; // null signifies implicit access |
| 1369 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 1370 | SourceLocation Loc = R.getNameLoc(); |
| 1371 | if (SS.getRange().isValid()) |
| 1372 | Loc = SS.getRange().getBegin(); |
| 1373 | This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1376 | return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType, |
| 1377 | /*OpLoc*/ SourceLocation(), |
| 1378 | /*IsArrow*/ true, |
| 1379 | SS, R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1382 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1383 | const LookupResult &R, |
| 1384 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1385 | // Only when used directly as the postfix-expression of a call. |
| 1386 | if (!HasTrailingLParen) |
| 1387 | return false; |
| 1388 | |
| 1389 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1390 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1391 | return false; |
| 1392 | |
| 1393 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1394 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | |
| 1397 | // Turn off ADL when we find certain kinds of declarations during |
| 1398 | // normal lookup: |
| 1399 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1400 | NamedDecl *D = *I; |
| 1401 | |
| 1402 | // C++0x [basic.lookup.argdep]p3: |
| 1403 | // -- a declaration of a class member |
| 1404 | // Since using decls preserve this property, we check this on the |
| 1405 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1406 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1407 | return false; |
| 1408 | |
| 1409 | // C++0x [basic.lookup.argdep]p3: |
| 1410 | // -- a block-scope function declaration that is not a |
| 1411 | // using-declaration |
| 1412 | // NOTE: we also trigger this for function templates (in fact, we |
| 1413 | // don't check the decl type at all, since all other decl types |
| 1414 | // turn off ADL anyway). |
| 1415 | if (isa<UsingShadowDecl>(D)) |
| 1416 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1417 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1418 | return false; |
| 1419 | |
| 1420 | // C++0x [basic.lookup.argdep]p3: |
| 1421 | // -- a declaration that is neither a function or a function |
| 1422 | // template |
| 1423 | // And also for builtin functions. |
| 1424 | if (isa<FunctionDecl>(D)) { |
| 1425 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1426 | |
| 1427 | // But also builtin functions. |
| 1428 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1429 | return false; |
| 1430 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
| 1434 | return true; |
| 1435 | } |
| 1436 | |
| 1437 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1438 | /// Diagnoses obvious problems with the use of the given declaration |
| 1439 | /// as an expression. This is only actually called for lookups that |
| 1440 | /// were not overloaded, and it doesn't promise that the declaration |
| 1441 | /// will in fact be used. |
| 1442 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1443 | if (isa<TypedefDecl>(D)) { |
| 1444 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
| 1448 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1449 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1450 | return true; |
| 1451 | } |
| 1452 | |
| 1453 | if (isa<NamespaceDecl>(D)) { |
| 1454 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1462 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1463 | LookupResult &R, |
| 1464 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1465 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1466 | // just build an ordinary singleton decl ref. |
| 1467 | if (!NeedsADL && R.isSingleResult()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1468 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1469 | |
| 1470 | // We only need to check the declaration if there's exactly one |
| 1471 | // result, because in the overloaded case the results can only be |
| 1472 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1473 | if (R.isSingleResult() && |
| 1474 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1475 | return ExprError(); |
| 1476 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1477 | bool Dependent |
| 1478 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1479 | UnresolvedLookupExpr *ULE |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1480 | = UnresolvedLookupExpr::Create(Context, Dependent, |
| 1481 | (NestedNameSpecifier*) SS.getScopeRep(), |
| 1482 | SS.getRange(), |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1483 | R.getLookupName(), R.getNameLoc(), |
| 1484 | NeedsADL, R.isOverloadedResult()); |
| 1485 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1486 | ULE->addDecl(*I); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1487 | |
| 1488 | return Owned(ULE); |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1493 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1494 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1495 | SourceLocation Loc, NamedDecl *D) { |
| 1496 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1497 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1498 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1499 | |
| 1500 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1501 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1503 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1504 | // Specifically diagnose references to class templates that are missing |
| 1505 | // a template argument list. |
| 1506 | Diag(Loc, diag::err_template_decl_ref) |
| 1507 | << Template << SS.getRange(); |
| 1508 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1509 | return ExprError(); |
| 1510 | } |
| 1511 | |
| 1512 | // Make sure that we're referring to a value. |
| 1513 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1514 | if (!VD) { |
| 1515 | Diag(Loc, diag::err_ref_non_value) |
| 1516 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 1517 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1518 | return ExprError(); |
| 1519 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1521 | // Check whether this declaration can be used. Note that we suppress |
| 1522 | // this check when we're going to perform argument-dependent lookup |
| 1523 | // on this function name, because this might not be the function |
| 1524 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1525 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1526 | return ExprError(); |
| 1527 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1528 | // Only create DeclRefExpr's for valid Decl's. |
| 1529 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1530 | return ExprError(); |
| 1531 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1532 | // If the identifier reference is inside a block, and it refers to a value |
| 1533 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1534 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1535 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1536 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1537 | // We do not do this for things like enum constants, global variables, etc, |
| 1538 | // as they do not get snapshotted. |
| 1539 | // |
| 1540 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 1541 | if (VD->getType().getTypePtr()->isVariablyModifiedType()) { |
| 1542 | Diag(Loc, diag::err_ref_vm_type); |
| 1543 | Diag(D->getLocation(), diag::note_declared_at); |
| 1544 | return ExprError(); |
| 1545 | } |
| 1546 | |
Mike Stump | 2849734 | 2010-01-05 03:10:36 +0000 | [diff] [blame] | 1547 | if (VD->getType()->isArrayType()) { |
| 1548 | Diag(Loc, diag::err_ref_array_type); |
| 1549 | Diag(D->getLocation(), diag::note_declared_at); |
| 1550 | return ExprError(); |
| 1551 | } |
| 1552 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1553 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1554 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1555 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1556 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1557 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1558 | // This is to record that a 'const' was actually synthesize and added. |
| 1559 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1560 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1562 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1564 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1565 | } |
| 1566 | // If this reference is not in a block or if the referenced variable is |
| 1567 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1568 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1569 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1572 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1573 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1574 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1576 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1577 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1578 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1579 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1580 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1583 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1584 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1586 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1587 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1588 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1589 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1590 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1591 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1592 | QualType ResTy; |
| 1593 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1594 | ResTy = Context.DependentTy; |
| 1595 | } else { |
| 1596 | unsigned Length = |
| 1597 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1598 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1599 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1600 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1601 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1602 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1603 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1606 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | llvm::SmallString<16> CharBuffer; |
| 1608 | CharBuffer.resize(Tok.getLength()); |
| 1609 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1610 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1613 | Tok.getLocation(), PP); |
| 1614 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1615 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1617 | QualType Ty; |
| 1618 | if (!getLangOptions().CPlusPlus) |
| 1619 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 1620 | else if (Literal.isWide()) |
| 1621 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
| 1622 | else |
| 1623 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1624 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1625 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1626 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1627 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1630 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1631 | // 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] | 1632 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1633 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1634 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1635 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1636 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1637 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1638 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1639 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1640 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1641 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1642 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1644 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1646 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1647 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | Tok.getLocation(), PP); |
| 1650 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1651 | return ExprError(); |
| 1652 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1653 | Expr *Res; |
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 (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1656 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1657 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1658 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1659 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1660 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1661 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1662 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1663 | |
| 1664 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1665 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1666 | using llvm::APFloat; |
| 1667 | APFloat Val(Format); |
| 1668 | |
| 1669 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Overflow is always an error, but underflow is only an error if |
| 1672 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 1673 | if ((result & APFloat::opOverflow) || |
| 1674 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1675 | unsigned diagnostic; |
| 1676 | llvm::SmallVector<char, 20> buffer; |
| 1677 | if (result & APFloat::opOverflow) { |
| 1678 | diagnostic = diag::err_float_overflow; |
| 1679 | APFloat::getLargest(Format).toString(buffer); |
| 1680 | } else { |
| 1681 | diagnostic = diag::err_float_underflow; |
| 1682 | APFloat::getSmallest(Format).toString(buffer); |
| 1683 | } |
| 1684 | |
| 1685 | Diag(Tok.getLocation(), diagnostic) |
| 1686 | << Ty |
| 1687 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 1688 | } |
| 1689 | |
| 1690 | bool isExact = (result == APFloat::opOK); |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1691 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1692 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1693 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1694 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1695 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1696 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1698 | // long long is a C99 feature. |
| 1699 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1700 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1701 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1702 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1703 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1704 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1706 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1707 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1708 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1709 | Ty = Context.UnsignedLongLongTy; |
| 1710 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1711 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | } else { |
| 1713 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1714 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1715 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1717 | // be an unsigned int. |
| 1718 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1719 | |
| 1720 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1721 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1722 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1723 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1724 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1726 | // Does it fit in a unsigned int? |
| 1727 | if (ResultVal.isIntN(IntSize)) { |
| 1728 | // Does it fit in a signed int? |
| 1729 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1730 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1731 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1732 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1733 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1735 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1737 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1738 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1739 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1740 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1741 | // Does it fit in a unsigned long? |
| 1742 | if (ResultVal.isIntN(LongSize)) { |
| 1743 | // Does it fit in a signed long? |
| 1744 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1745 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1746 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1747 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1748 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1752 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1753 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1754 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1756 | // Does it fit in a unsigned long long? |
| 1757 | if (ResultVal.isIntN(LongLongSize)) { |
| 1758 | // Does it fit in a signed long long? |
| 1759 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1760 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1761 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1762 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1763 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | // If we still couldn't decide a type, we probably have something that |
| 1768 | // 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] | 1769 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1770 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1771 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1772 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1773 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1775 | if (ResultVal.getBitWidth() != Width) |
| 1776 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1777 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1778 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1779 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1781 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1782 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1784 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1785 | |
| 1786 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1789 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1790 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1791 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1792 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1793 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1797 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1798 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1799 | SourceLocation OpLoc, |
| 1800 | const SourceRange &ExprRange, |
| 1801 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1802 | if (exprType->isDependentType()) |
| 1803 | return false; |
| 1804 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1805 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1806 | // the result is the size of the referenced type." |
| 1807 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1808 | // result shall be the alignment of the referenced type." |
| 1809 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1810 | exprType = Ref->getPointeeType(); |
| 1811 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1813 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1814 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1815 | if (isSizeof) |
| 1816 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1817 | return false; |
| 1818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1820 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1821 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1822 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1823 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1824 | return false; |
| 1825 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1827 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 1828 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 1829 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1830 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1832 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1833 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1834 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1835 | << exprType << isSizeof << ExprRange; |
| 1836 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1839 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1842 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1843 | const SourceRange &ExprRange) { |
| 1844 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1845 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1847 | if (isa<DeclRefExpr>(E)) |
| 1848 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Cannot know anything else if the expression is dependent. |
| 1851 | if (E->isTypeDependent()) |
| 1852 | return false; |
| 1853 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1854 | if (E->getBitField()) { |
| 1855 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1856 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1857 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1858 | |
| 1859 | // Alignment of a field access is always okay, so long as it isn't a |
| 1860 | // bit-field. |
| 1861 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1862 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1865 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1866 | } |
| 1867 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1868 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | Action::OwningExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1870 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1871 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1872 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1873 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1874 | return ExprError(); |
| 1875 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1876 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1878 | if (!T->isDependentType() && |
| 1879 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1880 | return ExprError(); |
| 1881 | |
| 1882 | // 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] | 1883 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1884 | Context.getSizeType(), OpLoc, |
| 1885 | R.getEnd())); |
| 1886 | } |
| 1887 | |
| 1888 | /// \brief Build a sizeof or alignof expression given an expression |
| 1889 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | Action::OwningExprResult |
| 1891 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1892 | bool isSizeOf, SourceRange R) { |
| 1893 | // Verify that the operand is valid. |
| 1894 | bool isInvalid = false; |
| 1895 | if (E->isTypeDependent()) { |
| 1896 | // Delay type-checking for type-dependent expressions. |
| 1897 | } else if (!isSizeOf) { |
| 1898 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1899 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1900 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1901 | isInvalid = true; |
| 1902 | } else { |
| 1903 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1904 | } |
| 1905 | |
| 1906 | if (isInvalid) |
| 1907 | return ExprError(); |
| 1908 | |
| 1909 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1910 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1911 | Context.getSizeType(), OpLoc, |
| 1912 | R.getEnd())); |
| 1913 | } |
| 1914 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1915 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1916 | /// the same for @c alignof and @c __alignof |
| 1917 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1918 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1919 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1920 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1921 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1922 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1923 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1924 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1925 | TypeSourceInfo *TInfo; |
| 1926 | (void) GetTypeFromParser(TyOrEx, &TInfo); |
| 1927 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1930 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1931 | Action::OwningExprResult Result |
| 1932 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1933 | |
| 1934 | if (Result.isInvalid()) |
| 1935 | DeleteExpr(ArgEx); |
| 1936 | |
| 1937 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1940 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1941 | if (V->isTypeDependent()) |
| 1942 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1944 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1945 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1946 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1948 | // Otherwise they pass through real integer and floating point types here. |
| 1949 | if (V->getType()->isArithmeticType()) |
| 1950 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1952 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1953 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1954 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1955 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1960 | Action::OwningExprResult |
| 1961 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1962 | tok::TokenKind Kind, ExprArg Input) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1963 | UnaryOperator::Opcode Opc; |
| 1964 | switch (Kind) { |
| 1965 | default: assert(0 && "Unknown unary op!"); |
| 1966 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1967 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1968 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1969 | |
Eli Friedman | e4216e9 | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 1970 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1973 | Action::OwningExprResult |
| 1974 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1975 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1976 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1977 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1978 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1979 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1980 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1982 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1983 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1984 | Base.release(); |
| 1985 | Idx.release(); |
| 1986 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1987 | Context.DependentTy, RLoc)); |
| 1988 | } |
| 1989 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1991 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1992 | LHSExp->getType()->isEnumeralType() || |
| 1993 | RHSExp->getType()->isRecordType() || |
| 1994 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1995 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1998 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 1999 | } |
| 2000 | |
| 2001 | |
| 2002 | Action::OwningExprResult |
| 2003 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 2004 | ExprArg Idx, SourceLocation RLoc) { |
| 2005 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 2006 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 2007 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2008 | // Perform default conversions. |
| 2009 | DefaultFunctionArrayConversion(LHSExp); |
| 2010 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2011 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2012 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2013 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2014 | // 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] | 2015 | // 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] | 2016 | // 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] | 2017 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2018 | Expr *BaseExpr, *IndexExpr; |
| 2019 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2020 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2021 | BaseExpr = LHSExp; |
| 2022 | IndexExpr = RHSExp; |
| 2023 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2024 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2025 | BaseExpr = LHSExp; |
| 2026 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2027 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2028 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2029 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2030 | BaseExpr = RHSExp; |
| 2031 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2032 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2034 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2035 | BaseExpr = LHSExp; |
| 2036 | IndexExpr = RHSExp; |
| 2037 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2039 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2040 | // Handle the uncommon case of "123[Ptr]". |
| 2041 | BaseExpr = RHSExp; |
| 2042 | IndexExpr = LHSExp; |
| 2043 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2044 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2045 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2046 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2048 | // FIXME: need to deal with const... |
| 2049 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2050 | } else if (LHSTy->isArrayType()) { |
| 2051 | // If we see an array that wasn't promoted by |
| 2052 | // DefaultFunctionArrayConversion, it must be an array that |
| 2053 | // wasn't promoted because of the C90 rule that doesn't |
| 2054 | // allow promoting non-lvalue arrays. Warn, then |
| 2055 | // force the promotion here. |
| 2056 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2057 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2058 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 2059 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2060 | LHSTy = LHSExp->getType(); |
| 2061 | |
| 2062 | BaseExpr = LHSExp; |
| 2063 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2064 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2065 | } else if (RHSTy->isArrayType()) { |
| 2066 | // Same as previous, except for 123[f().a] case |
| 2067 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2068 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2069 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 2070 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2071 | RHSTy = RHSExp->getType(); |
| 2072 | |
| 2073 | BaseExpr = RHSExp; |
| 2074 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2075 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2077 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2078 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2079 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2080 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2081 | if (!(IndexExpr->getType()->isIntegerType() && |
| 2082 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2083 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2084 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2086 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2087 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2088 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2089 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2090 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2091 | // 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] | 2092 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2093 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2094 | // incomplete types are not object types. |
| 2095 | if (ResultType->isFunctionType()) { |
| 2096 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2097 | << ResultType << BaseExpr->getSourceRange(); |
| 2098 | return ExprError(); |
| 2099 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2101 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2103 | PDiag(diag::err_subscript_incomplete_type) |
| 2104 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2105 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2107 | // Diagnose bad cases where we step over interface counts. |
| 2108 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 2109 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2110 | << ResultType << BaseExpr->getSourceRange(); |
| 2111 | return ExprError(); |
| 2112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2114 | Base.release(); |
| 2115 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2116 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2117 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2120 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2121 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2123 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2124 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2125 | // see FIXME there. |
| 2126 | // |
| 2127 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2128 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2129 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2130 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2131 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2132 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2133 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2134 | // 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] | 2135 | // special names that indicate a subset of exactly half the elements are |
| 2136 | // to be selected. |
| 2137 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2138 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2139 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2140 | // 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] | 2141 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Check that we've found one of the special components, or that the component |
| 2144 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2145 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2146 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2147 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2148 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2149 | do |
| 2150 | compStr++; |
| 2151 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2152 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2153 | do |
| 2154 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2155 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2156 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2157 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2158 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2159 | // We didn't get to the end of the string. This means the component names |
| 2160 | // 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] | 2161 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 2162 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2163 | return QualType(); |
| 2164 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2165 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2166 | // Ensure no component accessor exceeds the width of the vector type it |
| 2167 | // operates on. |
| 2168 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2169 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2170 | |
| 2171 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2172 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2173 | |
| 2174 | while (*compStr) { |
| 2175 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2176 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2177 | << baseType << SourceRange(CompLoc); |
| 2178 | return QualType(); |
| 2179 | } |
| 2180 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2181 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2182 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2183 | // 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] | 2184 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2185 | // 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] | 2186 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2187 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2188 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2189 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2190 | if (HexSwizzle) |
| 2191 | CompSize--; |
| 2192 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2193 | if (CompSize == 1) |
| 2194 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2195 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2196 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2197 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2198 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2199 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2200 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2201 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2202 | } |
| 2203 | 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] | 2204 | } |
| 2205 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2206 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2207 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2208 | const Selector &Sel, |
| 2209 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2211 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2212 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2213 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2214 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2216 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2217 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2219 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2220 | return D; |
| 2221 | } |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2225 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2226 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2227 | const Selector &Sel, |
| 2228 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2229 | // Check protocols on qualified interfaces. |
| 2230 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2231 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2232 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2233 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2234 | GDecl = PD; |
| 2235 | break; |
| 2236 | } |
| 2237 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2238 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2239 | GDecl = OMD; |
| 2240 | break; |
| 2241 | } |
| 2242 | } |
| 2243 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2244 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2245 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2246 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2247 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2248 | if (GDecl) |
| 2249 | return GDecl; |
| 2250 | } |
| 2251 | } |
| 2252 | return GDecl; |
| 2253 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2254 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2255 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2256 | Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType, |
| 2257 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2258 | const CXXScopeSpec &SS, |
| 2259 | NamedDecl *FirstQualifierInScope, |
| 2260 | DeclarationName Name, SourceLocation NameLoc, |
| 2261 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2262 | Expr *BaseExpr = Base.takeAs<Expr>(); |
| 2263 | |
| 2264 | // Even in dependent contexts, try to diagnose base expressions with |
| 2265 | // obviously wrong types, e.g.: |
| 2266 | // |
| 2267 | // T* t; |
| 2268 | // t.f; |
| 2269 | // |
| 2270 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2271 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2272 | // allows this, while still reporting an error if T is a struct pointer. |
| 2273 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2274 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2275 | if (PT && (!getLangOptions().ObjC1 || |
| 2276 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2277 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2278 | Diag(NameLoc, diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2279 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2280 | return ExprError(); |
| 2281 | } |
| 2282 | } |
| 2283 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2284 | assert(BaseType->isDependentType()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2285 | |
| 2286 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2287 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2288 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2289 | IsArrow, OpLoc, |
| 2290 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 2291 | SS.getRange(), |
| 2292 | FirstQualifierInScope, |
| 2293 | Name, NameLoc, |
| 2294 | TemplateArgs)); |
| 2295 | } |
| 2296 | |
| 2297 | /// We know that the given qualified member reference points only to |
| 2298 | /// declarations which do not belong to the static type of the base |
| 2299 | /// expression. Diagnose the problem. |
| 2300 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2301 | Expr *BaseExpr, |
| 2302 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2303 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2304 | const LookupResult &R) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2305 | // If this is an implicit member access, use a different set of |
| 2306 | // diagnostics. |
| 2307 | if (!BaseExpr) |
| 2308 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2309 | |
| 2310 | // FIXME: this is an exceedingly lame diagnostic for some of the more |
| 2311 | // complicated cases here. |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2312 | DeclContext *DC = R.getRepresentativeDecl()->getDeclContext(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2313 | SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual) |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2314 | << SS.getRange() << DC << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | // Check whether the declarations we found through a nested-name |
| 2318 | // specifier in a member expression are actually members of the base |
| 2319 | // type. The restriction here is: |
| 2320 | // |
| 2321 | // C++ [expr.ref]p2: |
| 2322 | // ... In these cases, the id-expression shall name a |
| 2323 | // member of the class or of one of its base classes. |
| 2324 | // |
| 2325 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2326 | // an unrelated class, and for us to find an overload set including |
| 2327 | // decls from classes which are not superclasses, as long as the decl |
| 2328 | // we actually pick through overload resolution is from a superclass. |
| 2329 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2330 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2331 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2332 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2333 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2334 | if (!BaseRT) { |
| 2335 | // We can't check this yet because the base type is still |
| 2336 | // dependent. |
| 2337 | assert(BaseType->isDependentType()); |
| 2338 | return false; |
| 2339 | } |
| 2340 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2341 | |
| 2342 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2343 | // If this is an implicit member reference and we find a |
| 2344 | // non-instance member, it's not an error. |
| 2345 | if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl())) |
| 2346 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2347 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2348 | // Note that we use the DC of the decl, not the underlying decl. |
| 2349 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext()); |
| 2350 | while (RecordD->isAnonymousStructOrUnion()) |
| 2351 | RecordD = cast<CXXRecordDecl>(RecordD->getParent()); |
| 2352 | |
| 2353 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| 2354 | MemberRecord.insert(RecordD->getCanonicalDecl()); |
| 2355 | |
| 2356 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2360 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2361 | return true; |
| 2362 | } |
| 2363 | |
| 2364 | static bool |
| 2365 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2366 | SourceRange BaseRange, const RecordType *RTy, |
| 2367 | SourceLocation OpLoc, const CXXScopeSpec &SS) { |
| 2368 | RecordDecl *RDecl = RTy->getDecl(); |
| 2369 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 2370 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2371 | << BaseRange)) |
| 2372 | return true; |
| 2373 | |
| 2374 | DeclContext *DC = RDecl; |
| 2375 | if (SS.isSet()) { |
| 2376 | // If the member name was a qualified-id, look into the |
| 2377 | // nested-name-specifier. |
| 2378 | DC = SemaRef.computeDeclContext(SS, false); |
| 2379 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2380 | if (SemaRef.RequireCompleteDeclContext(SS)) { |
| 2381 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2382 | << SS.getRange() << DC; |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2386 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2387 | |
| 2388 | if (!isa<TypeDecl>(DC)) { |
| 2389 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2390 | << DC << SS.getRange(); |
| 2391 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2395 | // The record definition is complete, now look up the member. |
| 2396 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2397 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2398 | if (!R.empty()) |
| 2399 | return false; |
| 2400 | |
| 2401 | // We didn't find anything with the given name, so try to correct |
| 2402 | // for typos. |
| 2403 | DeclarationName Name = R.getLookupName(); |
| 2404 | if (SemaRef.CorrectTypo(R, 0, &SS, DC) && |
| 2405 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 2406 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 2407 | << Name << DC << R.getLookupName() << SS.getRange() |
| 2408 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2409 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2410 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 2411 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 2412 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2413 | return false; |
| 2414 | } else { |
| 2415 | R.clear(); |
| 2416 | } |
| 2417 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2418 | return false; |
| 2419 | } |
| 2420 | |
| 2421 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2422 | Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2423 | SourceLocation OpLoc, bool IsArrow, |
| 2424 | const CXXScopeSpec &SS, |
| 2425 | NamedDecl *FirstQualifierInScope, |
| 2426 | DeclarationName Name, SourceLocation NameLoc, |
| 2427 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2428 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2429 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2430 | if (BaseType->isDependentType() || |
| 2431 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2432 | return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2433 | IsArrow, OpLoc, |
| 2434 | SS, FirstQualifierInScope, |
| 2435 | Name, NameLoc, |
| 2436 | TemplateArgs); |
| 2437 | |
| 2438 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2439 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2440 | // Implicit member accesses. |
| 2441 | if (!Base) { |
| 2442 | QualType RecordTy = BaseType; |
| 2443 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2444 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2445 | RecordTy->getAs<RecordType>(), |
| 2446 | OpLoc, SS)) |
| 2447 | return ExprError(); |
| 2448 | |
| 2449 | // Explicit member accesses. |
| 2450 | } else { |
| 2451 | OwningExprResult Result = |
| 2452 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 2453 | SS, FirstQualifierInScope, |
| 2454 | /*ObjCImpDecl*/ DeclPtrTy()); |
| 2455 | |
| 2456 | if (Result.isInvalid()) { |
| 2457 | Owned(Base); |
| 2458 | return ExprError(); |
| 2459 | } |
| 2460 | |
| 2461 | if (Result.get()) |
| 2462 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2465 | return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType, |
| 2466 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2470 | Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType, |
| 2471 | SourceLocation OpLoc, bool IsArrow, |
| 2472 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2473 | LookupResult &R, |
| 2474 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2475 | Expr *BaseExpr = Base.takeAs<Expr>(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2476 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2477 | if (IsArrow) { |
| 2478 | assert(BaseType->isPointerType()); |
| 2479 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2480 | } |
| 2481 | |
| 2482 | NestedNameSpecifier *Qualifier = |
| 2483 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 2484 | DeclarationName MemberName = R.getLookupName(); |
| 2485 | SourceLocation MemberLoc = R.getNameLoc(); |
| 2486 | |
| 2487 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2488 | return ExprError(); |
| 2489 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2490 | if (R.empty()) { |
| 2491 | // Rederive where we looked up. |
| 2492 | DeclContext *DC = (SS.isSet() |
| 2493 | ? computeDeclContext(SS, false) |
| 2494 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2495 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2496 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2497 | << MemberName << DC |
| 2498 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2499 | return ExprError(); |
| 2500 | } |
| 2501 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2502 | // Diagnose qualified lookups that find only declarations from a |
| 2503 | // non-base type. Note that it's okay for lookup to find |
| 2504 | // declarations from a non-base type as long as those aren't the |
| 2505 | // ones picked by overload resolution. |
| 2506 | if (SS.isSet() && CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2507 | return ExprError(); |
| 2508 | |
| 2509 | // Construct an unresolved result if we in fact got an unresolved |
| 2510 | // result. |
| 2511 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2512 | bool Dependent = |
John McCall | 410a3f3 | 2009-12-19 02:05:44 +0000 | [diff] [blame] | 2513 | BaseExprType->isDependentType() || |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2514 | R.isUnresolvableResult() || |
| 2515 | UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2516 | |
| 2517 | UnresolvedMemberExpr *MemExpr |
| 2518 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2519 | R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2520 | BaseExpr, BaseExprType, |
| 2521 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2522 | Qualifier, SS.getRange(), |
| 2523 | MemberName, MemberLoc, |
| 2524 | TemplateArgs); |
| 2525 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2526 | MemExpr->addDecl(*I); |
| 2527 | |
| 2528 | return Owned(MemExpr); |
| 2529 | } |
| 2530 | |
| 2531 | assert(R.isSingleResult()); |
| 2532 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2533 | |
| 2534 | // FIXME: diagnose the presence of template arguments now. |
| 2535 | |
| 2536 | // If the decl being referenced had an error, return an error for this |
| 2537 | // sub-expr without emitting another error, in order to avoid cascading |
| 2538 | // error cases. |
| 2539 | if (MemberDecl->isInvalidDecl()) |
| 2540 | return ExprError(); |
| 2541 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2542 | // Handle the implicit-member-access case. |
| 2543 | if (!BaseExpr) { |
| 2544 | // If this is not an instance member, convert to a non-member access. |
| 2545 | if (!IsInstanceMember(MemberDecl)) |
| 2546 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl); |
| 2547 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2548 | SourceLocation Loc = R.getNameLoc(); |
| 2549 | if (SS.getRange().isValid()) |
| 2550 | Loc = SS.getRange().getBegin(); |
| 2551 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2554 | bool ShouldCheckUse = true; |
| 2555 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2556 | // Don't diagnose the use of a virtual member function unless it's |
| 2557 | // explicitly qualified. |
| 2558 | if (MD->isVirtual() && !SS.isSet()) |
| 2559 | ShouldCheckUse = false; |
| 2560 | } |
| 2561 | |
| 2562 | // Check the use of this member. |
| 2563 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2564 | Owned(BaseExpr); |
| 2565 | return ExprError(); |
| 2566 | } |
| 2567 | |
| 2568 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2569 | // We may have found a field within an anonymous union or struct |
| 2570 | // (C++ [class.union]). |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2571 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2572 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2573 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2574 | BaseExpr, OpLoc); |
| 2575 | |
| 2576 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2577 | QualType MemberType = FD->getType(); |
| 2578 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2579 | MemberType = Ref->getPointeeType(); |
| 2580 | else { |
| 2581 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2582 | BaseQuals.removeObjCGCAttr(); |
| 2583 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2584 | |
| 2585 | Qualifiers MemberQuals |
| 2586 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2587 | |
| 2588 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2589 | if (Combined != MemberQuals) |
| 2590 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2591 | } |
| 2592 | |
| 2593 | MarkDeclarationReferenced(MemberLoc, FD); |
| 2594 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2595 | return ExprError(); |
| 2596 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2597 | FD, MemberLoc, MemberType)); |
| 2598 | } |
| 2599 | |
| 2600 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2601 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2602 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2603 | Var, MemberLoc, |
| 2604 | Var->getType().getNonReferenceType())); |
| 2605 | } |
| 2606 | |
| 2607 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2608 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2609 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2610 | MemberFn, MemberLoc, |
| 2611 | MemberFn->getType())); |
| 2612 | } |
| 2613 | |
| 2614 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2615 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2616 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2617 | Enum, MemberLoc, Enum->getType())); |
| 2618 | } |
| 2619 | |
| 2620 | Owned(BaseExpr); |
| 2621 | |
| 2622 | if (isa<TypeDecl>(MemberDecl)) |
| 2623 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2624 | << MemberName << int(IsArrow)); |
| 2625 | |
| 2626 | // We found a declaration kind that we didn't expect. This is a |
| 2627 | // generic error message that tells the user that she can't refer |
| 2628 | // to this member with '.' or '->'. |
| 2629 | return ExprError(Diag(MemberLoc, |
| 2630 | diag::err_typecheck_member_reference_unknown) |
| 2631 | << MemberName << int(IsArrow)); |
| 2632 | } |
| 2633 | |
| 2634 | /// Look up the given member of the given non-type-dependent |
| 2635 | /// expression. This can return in one of two ways: |
| 2636 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2637 | /// assume that lookup was performed and the results written into |
| 2638 | /// the provided structure. It will take over from there. |
| 2639 | /// * Otherwise, the returned expression will be produced in place of |
| 2640 | /// an ordinary member expression. |
| 2641 | /// |
| 2642 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2643 | /// fixed for ObjC++. |
| 2644 | Sema::OwningExprResult |
| 2645 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2646 | bool &IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2647 | const CXXScopeSpec &SS, |
| 2648 | NamedDecl *FirstQualifierInScope, |
| 2649 | DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2650 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2652 | // Perform default conversions. |
| 2653 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2654 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2655 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2656 | assert(!BaseType->isDependentType()); |
| 2657 | |
| 2658 | DeclarationName MemberName = R.getLookupName(); |
| 2659 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2660 | |
| 2661 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2662 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2663 | // function. Suggest the addition of those parentheses, build the |
| 2664 | // call, and continue on. |
| 2665 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 2666 | if (const FunctionProtoType *Fun |
| 2667 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 2668 | QualType ResultTy = Fun->getResultType(); |
| 2669 | if (Fun->getNumArgs() == 0 && |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2670 | ((!IsArrow && ResultTy->isRecordType()) || |
| 2671 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2672 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 2673 | ->isRecordType()))) { |
| 2674 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2675 | Diag(Loc, diag::err_member_reference_needs_call) |
| 2676 | << QualType(Fun, 0) |
| 2677 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2678 | |
| 2679 | OwningExprResult NewBase |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2680 | = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc, |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2681 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 2682 | if (NewBase.isInvalid()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2683 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2684 | |
| 2685 | BaseExpr = NewBase.takeAs<Expr>(); |
| 2686 | DefaultFunctionArrayConversion(BaseExpr); |
| 2687 | BaseType = BaseExpr->getType(); |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2692 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2693 | // use that. |
| 2694 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2695 | if (IsArrow) { |
| 2696 | // Handle the following exceptional case PObj->isa. |
| 2697 | if (const ObjCObjectPointerType *OPT = |
| 2698 | BaseType->getAs<ObjCObjectPointerType>()) { |
| 2699 | if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && |
| 2700 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2701 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 2702 | Context.getObjCClassType())); |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2703 | } |
| 2704 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2705 | // We have an 'id' type. Rather than fall through, we check if this |
| 2706 | // is a reference to 'isa'. |
| 2707 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2708 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2709 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2710 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2711 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2712 | |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 2713 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2714 | // use that. |
| 2715 | if (Context.isObjCSelType(BaseType)) { |
| 2716 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 2717 | // is a reference to 'sel_id'. |
| 2718 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 2719 | BaseType = Context.ObjCSelRedefinitionType; |
| 2720 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
| 2721 | } |
| 2722 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2723 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2724 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2725 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2726 | // Handle properties on ObjC 'Class' types. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2727 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2728 | // Also must look for a getter name which uses property syntax. |
| 2729 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2730 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 2731 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2732 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2733 | ObjCMethodDecl *Getter; |
| 2734 | // FIXME: need to also look locally in the implementation. |
| 2735 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2736 | // Check the use of this method. |
| 2737 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2738 | return ExprError(); |
| 2739 | } |
| 2740 | // If we found a getter then this may be a valid dot-reference, we |
| 2741 | // will look for the matching setter, in case it is needed. |
| 2742 | Selector SetterSel = |
| 2743 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2744 | PP.getSelectorTable(), Member); |
| 2745 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2746 | if (!Setter) { |
| 2747 | // If this reference is in an @implementation, also check for 'private' |
| 2748 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2749 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2750 | } |
| 2751 | // Look through local category implementations associated with the class. |
| 2752 | if (!Setter) |
| 2753 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 2754 | |
| 2755 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2756 | return ExprError(); |
| 2757 | |
| 2758 | if (Getter || Setter) { |
| 2759 | QualType PType; |
| 2760 | |
| 2761 | if (Getter) |
| 2762 | PType = Getter->getResultType(); |
| 2763 | else |
| 2764 | // Get the expression type from Setter's incoming parameter. |
| 2765 | PType = (*(Setter->param_end() -1))->getType(); |
| 2766 | // FIXME: we must check that the setter has property type. |
| 2767 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 2768 | PType, |
| 2769 | Setter, MemberLoc, BaseExpr)); |
| 2770 | } |
| 2771 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2772 | << MemberName << BaseType); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | if (BaseType->isObjCClassType() && |
| 2777 | BaseType != Context.ObjCClassRedefinitionType) { |
| 2778 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2779 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2782 | if (IsArrow) { |
| 2783 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2784 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2785 | else if (BaseType->isObjCObjectPointerType()) |
| 2786 | ; |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2787 | else if (BaseType->isRecordType()) { |
| 2788 | // Recover from arrow accesses to records, e.g.: |
| 2789 | // struct MyRecord foo; |
| 2790 | // foo->bar |
| 2791 | // This is actually well-formed in C++ if MyRecord has an |
| 2792 | // overloaded operator->, but that should have been dealt with |
| 2793 | // by now. |
| 2794 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2795 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2796 | << CodeModificationHint::CreateReplacement(OpLoc, "."); |
| 2797 | IsArrow = false; |
| 2798 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2799 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 2800 | << BaseType << BaseExpr->getSourceRange(); |
| 2801 | return ExprError(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2802 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2803 | } else { |
| 2804 | // Recover from dot accesses to pointers, e.g.: |
| 2805 | // type *foo; |
| 2806 | // foo.bar |
| 2807 | // This is actually well-formed in two cases: |
| 2808 | // - 'type' is an Objective C type |
| 2809 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 2810 | // the appropriate pointer type |
| 2811 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 2812 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 2813 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 2814 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2815 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2816 | << CodeModificationHint::CreateReplacement(OpLoc, "->"); |
| 2817 | BaseType = PT->getPointeeType(); |
| 2818 | IsArrow = true; |
| 2819 | } |
| 2820 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2821 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2822 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2823 | // Handle field access to simple records. This also handles access |
| 2824 | // to fields of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2825 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2826 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 2827 | RTy, OpLoc, SS)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2828 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2829 | return Owned((Expr*) 0); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2830 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2832 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2833 | // into a record type was handled above, any destructor we see here is a |
| 2834 | // pseudo-destructor. |
| 2835 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2836 | // C++ [expr.pseudo]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | // The left hand side of the dot operator shall be of scalar type. The |
| 2838 | // 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] | 2839 | // type. |
| 2840 | if (!BaseType->isScalarType()) |
| 2841 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2842 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2844 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2845 | // same as the object type. |
| 2846 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2847 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2848 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2849 | << BaseType << MemberName.getCXXNameType() |
| 2850 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
| 2852 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2853 | // the form |
| 2854 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2856 | // |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2857 | // shall designate the same scalar type. |
| 2858 | // |
| 2859 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2860 | // isn't checked here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2862 | // FIXME: We've lost the precise spelling of the type by going through |
| 2863 | // DeclarationName. Can we do better? |
| 2864 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2865 | IsArrow, OpLoc, |
| 2866 | (NestedNameSpecifier *) SS.getScopeRep(), |
| 2867 | SS.getRange(), |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2868 | MemberName.getCXXNameType(), |
| 2869 | MemberLoc)); |
| 2870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2872 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2873 | // (*Obj).ivar. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2874 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2875 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2876 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | const ObjCInterfaceType *IFaceT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2878 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2879 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2880 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2881 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2882 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2883 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2884 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2886 | if (!IV) { |
| 2887 | // Attempt to correct for typos in ivar names. |
| 2888 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 2889 | LookupMemberName); |
| 2890 | if (CorrectTypo(Res, 0, 0, IDecl) && |
| 2891 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 2892 | Diag(R.getNameLoc(), |
| 2893 | diag::err_typecheck_member_reference_ivar_suggest) |
| 2894 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 2895 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2896 | IV->getNameAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2897 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 2898 | << IV->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2902 | if (IV) { |
| 2903 | // If the decl being referenced had an error, return an error for this |
| 2904 | // sub-expr without emitting another error, in order to avoid cascading |
| 2905 | // error cases. |
| 2906 | if (IV->isInvalidDecl()) |
| 2907 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2908 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2909 | // Check whether we can reference this field. |
| 2910 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2911 | return ExprError(); |
| 2912 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2913 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2914 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2915 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2916 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2917 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2918 | // Case of a c-function declared inside an objc implementation. |
| 2919 | // FIXME: For a c-style function nested inside an objc implementation |
| 2920 | // class, there is no implementation context available, so we pass |
| 2921 | // down the context as argument to this routine. Ideally, this context |
| 2922 | // need be passed down in the AST node and somehow calculated from the |
| 2923 | // AST for a function decl. |
| 2924 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2926 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2927 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2928 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2929 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2930 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
| 2933 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2934 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2935 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2937 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2938 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2939 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2941 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2942 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2943 | |
| 2944 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2945 | MemberLoc, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2946 | IsArrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2947 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2948 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2949 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2950 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2951 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2952 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2953 | // Handle properties on 'id' and qualified "id". |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2954 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 2955 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2956 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2957 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2959 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2960 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2961 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2962 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2963 | // Check the use of this declaration |
| 2964 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2965 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2967 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2968 | MemberLoc, BaseExpr)); |
| 2969 | } |
| 2970 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2971 | // Check the use of this method. |
| 2972 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2973 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2975 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | OMD->getResultType(), |
| 2977 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2978 | NULL, 0)); |
| 2979 | } |
| 2980 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2981 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2982 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2983 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2984 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2985 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2986 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2987 | const ObjCObjectPointerType *OPT; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2988 | if (!IsArrow && (OPT = BaseType->getAsObjCInterfacePointerType())) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2989 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2990 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2991 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2993 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2994 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2995 | // Check whether we can reference this property. |
| 2996 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2997 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2998 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2999 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3000 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 3001 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 3002 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 3003 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3004 | MemberLoc, BaseExpr)); |
| 3005 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3006 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3007 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3008 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3009 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3010 | // Check whether we can reference this property. |
| 3011 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3012 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3013 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3014 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3015 | MemberLoc, BaseExpr)); |
| 3016 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3017 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 3018 | // selector is implemented. |
| 3019 | |
| 3020 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 3021 | // shared with the code in ActOnInstanceMessage. |
| 3022 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3023 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3024 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3025 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3026 | // If this reference is in an @implementation, check for 'private' methods. |
| 3027 | if (!Getter) |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3028 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3029 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 3030 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3031 | if (!Getter) |
| 3032 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3033 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3034 | // Check if we can reference this property. |
| 3035 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3036 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3037 | } |
| 3038 | // If we found a getter then this may be a valid dot-reference, we |
| 3039 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | Selector SetterSel = |
| 3041 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3042 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3043 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3044 | if (!Setter) { |
| 3045 | // If this reference is in an @implementation, also check for 'private' |
| 3046 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3047 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3048 | } |
| 3049 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3050 | if (!Setter) |
| 3051 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3052 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3053 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3054 | return ExprError(); |
| 3055 | |
| 3056 | if (Getter || Setter) { |
| 3057 | QualType PType; |
| 3058 | |
| 3059 | if (Getter) |
| 3060 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 3061 | else |
| 3062 | // Get the expression type from Setter's incoming parameter. |
| 3063 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3064 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 3065 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3066 | Setter, MemberLoc, BaseExpr)); |
| 3067 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3068 | |
| 3069 | // Attempt to correct for typos in property names. |
| 3070 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3071 | LookupOrdinaryName); |
| 3072 | if (CorrectTypo(Res, 0, 0, IFace, false, OPT) && |
| 3073 | Res.getAsSingle<ObjCPropertyDecl>()) { |
| 3074 | Diag(R.getNameLoc(), diag::err_property_not_found_suggest) |
| 3075 | << MemberName << BaseType << Res.getLookupName() |
| 3076 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 3077 | Res.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3078 | ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>(); |
| 3079 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 3080 | << Property->getDeclName(); |
| 3081 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3082 | return LookupMemberExpr(Res, BaseExpr, IsArrow, OpLoc, SS, |
| 3083 | FirstQualifierInScope, ObjCImpDecl); |
| 3084 | } |
| 3085 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3086 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3087 | << MemberName << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 3088 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3090 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3091 | if (!IsArrow && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3092 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3093 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3094 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 3095 | Context.getObjCClassType())); |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3096 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3097 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3098 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3099 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3100 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 3101 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3102 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3103 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3104 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3105 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3107 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 3108 | << BaseType << BaseExpr->getSourceRange(); |
| 3109 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3110 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3113 | static Sema::OwningExprResult DiagnoseDtorReference(Sema &SemaRef, |
| 3114 | SourceLocation NameLoc, |
| 3115 | Sema::ExprArg MemExpr) { |
| 3116 | Expr *E = (Expr *) MemExpr.get(); |
| 3117 | SourceLocation ExpectedLParenLoc = SemaRef.PP.getLocForEndOfToken(NameLoc); |
| 3118 | SemaRef.Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 3119 | << isa<CXXPseudoDestructorExpr>(E) |
| 3120 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 3121 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3122 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, |
| 3123 | move(MemExpr), |
| 3124 | /*LPLoc*/ ExpectedLParenLoc, |
| 3125 | Sema::MultiExprArg(SemaRef, 0, 0), |
| 3126 | /*CommaLocs*/ 0, |
| 3127 | /*RPLoc*/ ExpectedLParenLoc); |
| 3128 | } |
| 3129 | |
| 3130 | /// The main callback when the parser finds something like |
| 3131 | /// expression . [nested-name-specifier] identifier |
| 3132 | /// expression -> [nested-name-specifier] identifier |
| 3133 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3134 | /// possibilities, including destructor and operator references. |
| 3135 | /// |
| 3136 | /// \param OpKind either tok::arrow or tok::period |
| 3137 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3138 | /// is used to diagnose mis-uses of special members that can |
| 3139 | /// only be called |
| 3140 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3141 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3142 | /// aren't properly put in the context chain |
| 3143 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg BaseArg, |
| 3144 | SourceLocation OpLoc, |
| 3145 | tok::TokenKind OpKind, |
| 3146 | const CXXScopeSpec &SS, |
| 3147 | UnqualifiedId &Id, |
| 3148 | DeclPtrTy ObjCImpDecl, |
| 3149 | bool HasTrailingLParen) { |
| 3150 | if (SS.isSet() && SS.isInvalid()) |
| 3151 | return ExprError(); |
| 3152 | |
| 3153 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3154 | |
| 3155 | // Decompose the name into its component parts. |
| 3156 | DeclarationName Name; |
| 3157 | SourceLocation NameLoc; |
| 3158 | const TemplateArgumentListInfo *TemplateArgs; |
| 3159 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 3160 | Name, NameLoc, TemplateArgs); |
| 3161 | |
| 3162 | bool IsArrow = (OpKind == tok::arrow); |
| 3163 | |
| 3164 | NamedDecl *FirstQualifierInScope |
| 3165 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3166 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3167 | |
| 3168 | // This is a postfix expression, so get rid of ParenListExprs. |
| 3169 | BaseArg = MaybeConvertParenListExprToParenExpr(S, move(BaseArg)); |
| 3170 | |
| 3171 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 3172 | OwningExprResult Result(*this); |
| 3173 | if (Base->getType()->isDependentType()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3174 | Result = ActOnDependentMemberExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3175 | IsArrow, OpLoc, |
| 3176 | SS, FirstQualifierInScope, |
| 3177 | Name, NameLoc, |
| 3178 | TemplateArgs); |
| 3179 | } else { |
| 3180 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
| 3181 | if (TemplateArgs) { |
| 3182 | // Re-use the lookup done for the template name. |
| 3183 | DecomposeTemplateName(R, Id); |
| 3184 | } else { |
| 3185 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 3186 | SS, FirstQualifierInScope, |
| 3187 | ObjCImpDecl); |
| 3188 | |
| 3189 | if (Result.isInvalid()) { |
| 3190 | Owned(Base); |
| 3191 | return ExprError(); |
| 3192 | } |
| 3193 | |
| 3194 | if (Result.get()) { |
| 3195 | // The only way a reference to a destructor can be used is to |
| 3196 | // immediately call it, which falls into this case. If the |
| 3197 | // next token is not a '(', produce a diagnostic and build the |
| 3198 | // call now. |
| 3199 | if (!HasTrailingLParen && |
| 3200 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| 3201 | return DiagnoseDtorReference(*this, NameLoc, move(Result)); |
| 3202 | |
| 3203 | return move(Result); |
| 3204 | } |
| 3205 | } |
| 3206 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3207 | Result = BuildMemberReferenceExpr(ExprArg(*this, Base), Base->getType(), |
| 3208 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3214 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 3215 | FunctionDecl *FD, |
| 3216 | ParmVarDecl *Param) { |
| 3217 | if (Param->hasUnparsedDefaultArg()) { |
| 3218 | Diag (CallLoc, |
| 3219 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3220 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3222 | diag::note_default_argument_declared_here); |
| 3223 | } else { |
| 3224 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3225 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 3226 | |
| 3227 | // Instantiate the expression. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3228 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3229 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3231 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3232 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3233 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3234 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3236 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3238 | // Check the expression as an initializer for the parameter. |
| 3239 | InitializedEntity Entity |
| 3240 | = InitializedEntity::InitializeParameter(Param); |
| 3241 | InitializationKind Kind |
| 3242 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3243 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3244 | Expr *ResultE = Result.takeAs<Expr>(); |
| 3245 | |
| 3246 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3247 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3248 | MultiExprArg(*this, (void**)&ResultE, 1)); |
| 3249 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3250 | return ExprError(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3251 | |
| 3252 | // Build the default argument expression. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3253 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3254 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3257 | // If the default expression creates temporaries, we need to |
| 3258 | // push them to the current stack of expression temporaries so they'll |
| 3259 | // be properly destroyed. |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3260 | // FIXME: We should really be rebuilding the default argument with new |
| 3261 | // bound temporaries; see the comment in PR5810. |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 3262 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3263 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3267 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3270 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3271 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3272 | /// function prototype Proto. Call is the call expression itself, and |
| 3273 | /// Fn is the function expression. For a C++ member function, this |
| 3274 | /// routine does not attempt to convert the object argument. Returns |
| 3275 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3276 | bool |
| 3277 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3278 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3279 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3280 | Expr **Args, unsigned NumArgs, |
| 3281 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3282 | // 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] | 3283 | // assignment, to the types of the corresponding parameter, ... |
| 3284 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3285 | bool Invalid = false; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3287 | // If too few arguments are available (and we don't have default |
| 3288 | // arguments for the remaining parameters), don't make the call. |
| 3289 | if (NumArgs < NumArgsInProto) { |
| 3290 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3291 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 3292 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3293 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | // If too many are passed and not variadic, error on the extras and drop |
| 3297 | // them. |
| 3298 | if (NumArgs > NumArgsInProto) { |
| 3299 | if (!Proto->isVariadic()) { |
| 3300 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3301 | diag::err_typecheck_call_too_many_args) |
| 3302 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 3303 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3304 | Args[NumArgs-1]->getLocEnd()); |
| 3305 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3306 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3307 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3308 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3309 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3310 | llvm::SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3311 | VariadicCallType CallType = |
| 3312 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3313 | if (Fn->getType()->isBlockPointerType()) |
| 3314 | CallType = VariadicBlock; // Block |
| 3315 | else if (isa<MemberExpr>(Fn)) |
| 3316 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3317 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3318 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3319 | if (Invalid) |
| 3320 | return true; |
| 3321 | unsigned TotalNumArgs = AllArgs.size(); |
| 3322 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3323 | Call->setArg(i, AllArgs[i]); |
| 3324 | |
| 3325 | return false; |
| 3326 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3327 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3328 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3329 | FunctionDecl *FDecl, |
| 3330 | const FunctionProtoType *Proto, |
| 3331 | unsigned FirstProtoArg, |
| 3332 | Expr **Args, unsigned NumArgs, |
| 3333 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3334 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3335 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3336 | unsigned NumArgsToCheck = NumArgs; |
| 3337 | bool Invalid = false; |
| 3338 | if (NumArgs != NumArgsInProto) |
| 3339 | // Use default arguments for missing arguments |
| 3340 | NumArgsToCheck = NumArgsInProto; |
| 3341 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3342 | // 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] | 3343 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3344 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3346 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3347 | if (ArgIx < NumArgs) { |
| 3348 | Arg = Args[ArgIx++]; |
| 3349 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3350 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3351 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3352 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3353 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3354 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3356 | // Pass the argument |
| 3357 | ParmVarDecl *Param = 0; |
| 3358 | if (FDecl && i < FDecl->getNumParams()) |
| 3359 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3360 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3361 | |
| 3362 | InitializedEntity Entity = |
| 3363 | Param? InitializedEntity::InitializeParameter(Param) |
| 3364 | : InitializedEntity::InitializeParameter(ProtoArgType); |
| 3365 | OwningExprResult ArgE = PerformCopyInitialization(Entity, |
| 3366 | SourceLocation(), |
| 3367 | Owned(Arg)); |
| 3368 | if (ArgE.isInvalid()) |
| 3369 | return true; |
| 3370 | |
| 3371 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3372 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3373 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3374 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3376 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3377 | if (ArgExpr.isInvalid()) |
| 3378 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3379 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3380 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3381 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3382 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3383 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3385 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3386 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3387 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3388 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3389 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 3390 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3391 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3394 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3397 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3398 | /// This provides the location of the left/right parens and a list of comma |
| 3399 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3400 | Action::OwningExprResult |
| 3401 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 3402 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3403 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3404 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3405 | |
| 3406 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 3407 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 3409 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3410 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 3411 | assert(Fn && "no function call expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3413 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3414 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3415 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3416 | if (NumArgs > 0) { |
| 3417 | // Pseudo-destructor calls should not have any arguments. |
| 3418 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 3419 | << CodeModificationHint::CreateRemoval( |
| 3420 | SourceRange(Args[0]->getLocStart(), |
| 3421 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3423 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3424 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3426 | NumArgs = 0; |
| 3427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3429 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3430 | RParenLoc)); |
| 3431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3433 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3434 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3435 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3436 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3437 | bool Dependent = false; |
| 3438 | if (Fn->isTypeDependent()) |
| 3439 | Dependent = true; |
| 3440 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3441 | Dependent = true; |
| 3442 | |
| 3443 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3444 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3445 | Context.DependentTy, RParenLoc)); |
| 3446 | |
| 3447 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3448 | if (Fn->getType()->isRecordType()) |
| 3449 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 3450 | CommaLocs, RParenLoc)); |
| 3451 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3452 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3453 | |
| 3454 | // Determine whether this is a call to an unresolved member function. |
| 3455 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3456 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3457 | // an unresolved using declaration), it has to be an overloaded |
| 3458 | // function set, which means it must contain either multiple |
| 3459 | // declarations (all methods or method templates) or a single |
| 3460 | // method template. |
| 3461 | assert((MemE->getNumDecls() > 1) || |
| 3462 | isa<FunctionTemplateDecl>(*MemE->decls_begin())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3463 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3464 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3465 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3466 | CommaLocs, RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3469 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3470 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3471 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3472 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3473 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3474 | CommaLocs, RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3475 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3478 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3479 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 3480 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3481 | if (const FunctionProtoType *FPT = |
| 3482 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 3483 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3484 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3485 | ExprOwningPtr<CXXMemberCallExpr> |
| 3486 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3487 | NumArgs, ResultTy, |
| 3488 | RParenLoc)); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3489 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3490 | if (CheckCallReturnType(FPT->getResultType(), |
| 3491 | BO->getRHS()->getSourceRange().getBegin(), |
| 3492 | TheCall.get(), 0)) |
| 3493 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3494 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3495 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 3496 | RParenLoc)) |
| 3497 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3498 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3499 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 3500 | } |
| 3501 | return ExprError(Diag(Fn->getLocStart(), |
| 3502 | diag::err_typecheck_call_not_function) |
| 3503 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3504 | } |
| 3505 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3508 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3510 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3511 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3512 | Expr *NakedFn = Fn->IgnoreParens(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3513 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 3514 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 3515 | return BuildOverloadedCallExpr(Fn, ULE, LParenLoc, Args, NumArgs, |
| 3516 | CommaLocs, RParenLoc); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3517 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3518 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3519 | NamedDecl *NDecl = 0; |
| 3520 | if (isa<DeclRefExpr>(NakedFn)) |
| 3521 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 3522 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3523 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3524 | } |
| 3525 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3526 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3527 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3528 | /// unary-convert to an expression of function-pointer or |
| 3529 | /// block-pointer type. |
| 3530 | /// |
| 3531 | /// \param NDecl the declaration being called, if available |
| 3532 | Sema::OwningExprResult |
| 3533 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3534 | SourceLocation LParenLoc, |
| 3535 | Expr **Args, unsigned NumArgs, |
| 3536 | SourceLocation RParenLoc) { |
| 3537 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3538 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3539 | // Promote the function operand. |
| 3540 | UsualUnaryConversions(Fn); |
| 3541 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3542 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3543 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3544 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 3545 | Args, NumArgs, |
| 3546 | Context.BoolTy, |
| 3547 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3548 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3549 | const FunctionType *FuncT; |
| 3550 | if (!Fn->getType()->isBlockPointerType()) { |
| 3551 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3552 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3553 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3554 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3555 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3556 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3557 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3558 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3559 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3560 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3561 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3562 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3563 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3564 | << Fn->getType() << Fn->getSourceRange()); |
| 3565 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3566 | // Check for a valid return type |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3567 | if (CheckCallReturnType(FuncT->getResultType(), |
| 3568 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 3569 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3570 | return ExprError(); |
| 3571 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3572 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3573 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3574 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3575 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3576 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3577 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3578 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3579 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3580 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3582 | if (FDecl) { |
| 3583 | // Check if we have too few/too many template arguments, based |
| 3584 | // on our knowledge of the function definition. |
| 3585 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3586 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3587 | const FunctionProtoType *Proto = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3588 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3589 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3590 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3591 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3592 | } |
| 3593 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3596 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3597 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3598 | Expr *Arg = Args[i]; |
| 3599 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3600 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3601 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3602 | PDiag(diag::err_call_incomplete_argument) |
| 3603 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3604 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3605 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3606 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3607 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3608 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3609 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3610 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3611 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3612 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3613 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3614 | // Check for sentinels |
| 3615 | if (NDecl) |
| 3616 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3618 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3619 | if (FDecl) { |
| 3620 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3621 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3623 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3624 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3625 | } else if (NDecl) { |
| 3626 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3627 | return ExprError(); |
| 3628 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3629 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3630 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3633 | Action::OwningExprResult |
| 3634 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3635 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3636 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3638 | QualType literalType = GetTypeFromParser(Ty); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3639 | |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3640 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3641 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3642 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3643 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3644 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3645 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3646 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3647 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3648 | } else if (!literalType->isDependentType() && |
| 3649 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3650 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3652 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3653 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3655 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3656 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3657 | InitializationKind Kind |
| 3658 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| 3659 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3660 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
| 3661 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 3662 | MultiExprArg(*this, (void**)&literalExpr, 1), |
| 3663 | &literalType); |
| 3664 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3665 | return ExprError(); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3666 | InitExpr.release(); |
| 3667 | literalExpr = static_cast<Expr*>(Result.get()); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3668 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3669 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3670 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3671 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3672 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3673 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3674 | |
| 3675 | Result.release(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3676 | |
| 3677 | // FIXME: Store the TInfo to preserve type information better. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3678 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3679 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3682 | Action::OwningExprResult |
| 3683 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3684 | SourceLocation RBraceLoc) { |
| 3685 | unsigned NumInit = initlist.size(); |
| 3686 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3687 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3688 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3689 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3690 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3691 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3692 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3693 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3694 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3697 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3698 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3699 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3700 | return CastExpr::CK_NoOp; |
| 3701 | |
| 3702 | if (SrcTy->hasPointerRepresentation()) { |
| 3703 | if (DestTy->hasPointerRepresentation()) |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 3704 | return DestTy->isObjCObjectPointerType() ? |
| 3705 | CastExpr::CK_AnyPointerToObjCPointerCast : |
| 3706 | CastExpr::CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3707 | if (DestTy->isIntegerType()) |
| 3708 | return CastExpr::CK_PointerToIntegral; |
| 3709 | } |
| 3710 | |
| 3711 | if (SrcTy->isIntegerType()) { |
| 3712 | if (DestTy->isIntegerType()) |
| 3713 | return CastExpr::CK_IntegralCast; |
| 3714 | if (DestTy->hasPointerRepresentation()) |
| 3715 | return CastExpr::CK_IntegralToPointer; |
| 3716 | if (DestTy->isRealFloatingType()) |
| 3717 | return CastExpr::CK_IntegralToFloating; |
| 3718 | } |
| 3719 | |
| 3720 | if (SrcTy->isRealFloatingType()) { |
| 3721 | if (DestTy->isRealFloatingType()) |
| 3722 | return CastExpr::CK_FloatingCast; |
| 3723 | if (DestTy->isIntegerType()) |
| 3724 | return CastExpr::CK_FloatingToIntegral; |
| 3725 | } |
| 3726 | |
| 3727 | // FIXME: Assert here. |
| 3728 | // assert(false && "Unhandled cast combination!"); |
| 3729 | return CastExpr::CK_Unknown; |
| 3730 | } |
| 3731 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3732 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3733 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3735 | CXXMethodDecl *& ConversionDecl, |
| 3736 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3737 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3738 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3739 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3740 | |
Eli Friedman | 199ea95 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3741 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3742 | |
| 3743 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3744 | // type needs to be scalar. |
| 3745 | if (castType->isVoidType()) { |
| 3746 | // Cast to void allows any expr type. |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3747 | Kind = CastExpr::CK_ToVoid; |
| 3748 | return false; |
| 3749 | } |
| 3750 | |
| 3751 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3752 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3753 | (castType->isStructureType() || castType->isUnionType())) { |
| 3754 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3755 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3756 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3757 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3758 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3759 | return false; |
| 3760 | } |
| 3761 | |
| 3762 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3763 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3764 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3765 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3766 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3767 | Field != FieldEnd; ++Field) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3768 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3769 | castExpr->getType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3770 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3771 | << castExpr->getSourceRange(); |
| 3772 | break; |
| 3773 | } |
| 3774 | } |
| 3775 | if (Field == FieldEnd) |
| 3776 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3777 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3778 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3779 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3780 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3781 | |
| 3782 | // Reject any other conversions to non-scalar types. |
| 3783 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3784 | << castType << castExpr->getSourceRange(); |
| 3785 | } |
| 3786 | |
| 3787 | if (!castExpr->getType()->isScalarType() && |
| 3788 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3789 | return Diag(castExpr->getLocStart(), |
| 3790 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3791 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3794 | if (castType->isExtVectorType()) |
| 3795 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3796 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3797 | if (castType->isVectorType()) |
| 3798 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3799 | if (castExpr->getType()->isVectorType()) |
| 3800 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3801 | |
| 3802 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3803 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3804 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3805 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3806 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3807 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3808 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3809 | QualType castExprType = castExpr->getType(); |
| 3810 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3811 | return Diag(castExpr->getLocStart(), |
| 3812 | diag::err_cast_pointer_from_non_pointer_int) |
| 3813 | << castExprType << castExpr->getSourceRange(); |
| 3814 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3815 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3816 | return Diag(castExpr->getLocStart(), |
| 3817 | diag::err_cast_pointer_to_non_pointer_int) |
| 3818 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3819 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3820 | |
| 3821 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3822 | return false; |
| 3823 | } |
| 3824 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3825 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3826 | CastExpr::CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3827 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3828 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3829 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3830 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3831 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3832 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3833 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3834 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3835 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3836 | } else |
| 3837 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3838 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3839 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3840 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3841 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3842 | return false; |
| 3843 | } |
| 3844 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3845 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3846 | CastExpr::CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3847 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3848 | |
| 3849 | QualType SrcTy = CastExpr->getType(); |
| 3850 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3851 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3852 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3853 | if (SrcTy->isVectorType()) { |
| 3854 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3855 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3856 | << DestTy << SrcTy << R; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3857 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3858 | return false; |
| 3859 | } |
| 3860 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3861 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3862 | // conversion will take place first from scalar to elt type, and then |
| 3863 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3864 | if (SrcTy->isPointerType()) |
| 3865 | return Diag(R.getBegin(), |
| 3866 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3867 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3868 | |
| 3869 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3870 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3871 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3872 | |
| 3873 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3874 | return false; |
| 3875 | } |
| 3876 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3877 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3878 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3879 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3880 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3882 | assert((Ty != 0) && (Op.get() != 0) && |
| 3883 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3884 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3885 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3886 | //FIXME: Preserve type source info. |
| 3887 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3889 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3890 | if (isa<ParenListExpr>(castExpr)) |
| 3891 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3892 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3894 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3895 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3896 | |
| 3897 | if (Method) { |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3898 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3899 | Method, move(Op)); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3900 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3901 | if (CastArg.isInvalid()) |
| 3902 | return ExprError(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3903 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3904 | castExpr = CastArg.takeAs<Expr>(); |
| 3905 | } else { |
| 3906 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3909 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | Kind, castExpr, castType, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3911 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3914 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3915 | /// of comma binary operators. |
| 3916 | Action::OwningExprResult |
| 3917 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3918 | Expr *expr = EA.takeAs<Expr>(); |
| 3919 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3920 | if (!E) |
| 3921 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3922 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3923 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3925 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3926 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3927 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3929 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3930 | } |
| 3931 | |
| 3932 | Action::OwningExprResult |
| 3933 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3934 | SourceLocation RParenLoc, ExprArg Op, |
| 3935 | QualType Ty) { |
| 3936 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3937 | |
| 3938 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3939 | // then handle it as such. |
| 3940 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3941 | if (PE->getNumExprs() == 0) { |
| 3942 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3943 | return ExprError(); |
| 3944 | } |
| 3945 | |
| 3946 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3947 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3948 | initExprs.push_back(PE->getExpr(i)); |
| 3949 | |
| 3950 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3951 | // braces instead of the original commas. |
| 3952 | Op.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3954 | initExprs.size(), RParenLoc); |
| 3955 | E->setType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3957 | Owned(E)); |
| 3958 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | // 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] | 3960 | // sequence of BinOp comma operators. |
| 3961 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3962 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3963 | } |
| 3964 | } |
| 3965 | |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3966 | Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3967 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3968 | MultiExprArg Val, |
| 3969 | TypeTy *TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3970 | unsigned nexprs = Val.size(); |
| 3971 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3972 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 3973 | Expr *expr; |
| 3974 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 3975 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 3976 | else |
| 3977 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3978 | return Owned(expr); |
| 3979 | } |
| 3980 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3981 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3982 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3983 | /// C99 6.5.15 |
| 3984 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3985 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3986 | // C++ is sufficiently different to merit its own checker. |
| 3987 | if (getLangOptions().CPlusPlus) |
| 3988 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3989 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 3990 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 3991 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3992 | UsualUnaryConversions(Cond); |
| 3993 | UsualUnaryConversions(LHS); |
| 3994 | UsualUnaryConversions(RHS); |
| 3995 | QualType CondTy = Cond->getType(); |
| 3996 | QualType LHSTy = LHS->getType(); |
| 3997 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3999 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4000 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 4001 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4002 | << CondTy; |
| 4003 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4004 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4005 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4006 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4007 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 4008 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4009 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4010 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4011 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4012 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4013 | UsualArithmeticConversions(LHS, RHS); |
| 4014 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4015 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4016 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4017 | // If both operands are the same structure or union type, the result is that |
| 4018 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4019 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4020 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4021 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4022 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4023 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4024 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4025 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4026 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4027 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4028 | // 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] | 4029 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4030 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4031 | if (!LHSTy->isVoidType()) |
| 4032 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4033 | << RHS->getSourceRange(); |
| 4034 | if (!RHSTy->isVoidType()) |
| 4035 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4036 | << LHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4037 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 4038 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4039 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4040 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4041 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4042 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4043 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4044 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4045 | // promote the null to a pointer. |
| 4046 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4047 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4048 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4049 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4050 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4051 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4052 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4053 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4054 | |
| 4055 | // All objective-c pointer type analysis is done here. |
| 4056 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4057 | QuestionLoc); |
| 4058 | if (!compositeType.isNull()) |
| 4059 | return compositeType; |
| 4060 | |
| 4061 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4062 | // Handle block pointer types. |
| 4063 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4064 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4065 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4066 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4067 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4068 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4069 | return destType; |
| 4070 | } |
| 4071 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4072 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4073 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4074 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4075 | // We have 2 block pointer types. |
| 4076 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4077 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4078 | return LHSTy; |
| 4079 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4080 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4081 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4082 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4083 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4084 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4085 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4086 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4087 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4088 | // In this situation, we assume void* type. No especially good |
| 4089 | // reason, but this is what gcc does, and we do have to pick |
| 4090 | // to get a consistent AST. |
| 4091 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4092 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4093 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4094 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4095 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4096 | // The block pointer types are compatible. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4097 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4098 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4099 | return LHSTy; |
| 4100 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4101 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4102 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4103 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4104 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4105 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4106 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4107 | |
| 4108 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4109 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4110 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4111 | QualType destPointee |
| 4112 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4113 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4114 | // Add qualifiers if necessary. |
| 4115 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4116 | // Promote to void*. |
| 4117 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4118 | return destType; |
| 4119 | } |
| 4120 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4121 | QualType destPointee |
| 4122 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4123 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4124 | // Add qualifiers if necessary. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4125 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4126 | // Promote to void*. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4127 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4128 | return destType; |
| 4129 | } |
| 4130 | |
| 4131 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4132 | // Two identical pointer types are always compatible. |
| 4133 | return LHSTy; |
| 4134 | } |
| 4135 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4136 | rhptee.getUnqualifiedType())) { |
| 4137 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4138 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4139 | // In this situation, we assume void* type. No especially good |
| 4140 | // reason, but this is what gcc does, and we do have to pick |
| 4141 | // to get a consistent AST. |
| 4142 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4143 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4144 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4145 | return incompatTy; |
| 4146 | } |
| 4147 | // The pointer types are compatible. |
| 4148 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4149 | // differently qualified versions of compatible types, the result type is |
| 4150 | // a pointer to an appropriately qualified version of the *composite* |
| 4151 | // type. |
| 4152 | // FIXME: Need to calculate the composite type. |
| 4153 | // FIXME: Need to add qualifiers |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4154 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4155 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4156 | return LHSTy; |
| 4157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4158 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4159 | // GCC compatibility: soften pointer/integer mismatch. |
| 4160 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4161 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4162 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4163 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4164 | return RHSTy; |
| 4165 | } |
| 4166 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4167 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4168 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4169 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4170 | return LHSTy; |
| 4171 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4172 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4173 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4174 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4175 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4176 | return QualType(); |
| 4177 | } |
| 4178 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4179 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4180 | /// two objective-c pointer types of the two input expressions. |
| 4181 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4182 | SourceLocation QuestionLoc) { |
| 4183 | QualType LHSTy = LHS->getType(); |
| 4184 | QualType RHSTy = RHS->getType(); |
| 4185 | |
| 4186 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4187 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4188 | // redefinition type if an attempt is made to access its fields. |
| 4189 | if (LHSTy->isObjCClassType() && |
| 4190 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4191 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4192 | return LHSTy; |
| 4193 | } |
| 4194 | if (RHSTy->isObjCClassType() && |
| 4195 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4196 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4197 | return RHSTy; |
| 4198 | } |
| 4199 | // And the same for struct objc_object* / id |
| 4200 | if (LHSTy->isObjCIdType() && |
| 4201 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4202 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4203 | return LHSTy; |
| 4204 | } |
| 4205 | if (RHSTy->isObjCIdType() && |
| 4206 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4207 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4208 | return RHSTy; |
| 4209 | } |
| 4210 | // And the same for struct objc_selector* / SEL |
| 4211 | if (Context.isObjCSelType(LHSTy) && |
| 4212 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4213 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4214 | return LHSTy; |
| 4215 | } |
| 4216 | if (Context.isObjCSelType(RHSTy) && |
| 4217 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4218 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4219 | return RHSTy; |
| 4220 | } |
| 4221 | // Check constraints for Objective-C object pointers types. |
| 4222 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4223 | |
| 4224 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4225 | // Two identical object pointer types are always compatible. |
| 4226 | return LHSTy; |
| 4227 | } |
| 4228 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4229 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4230 | QualType compositeType = LHSTy; |
| 4231 | |
| 4232 | // If both operands are interfaces and either operand can be |
| 4233 | // assigned to the other, use that type as the composite |
| 4234 | // type. This allows |
| 4235 | // xxx ? (A*) a : (B*) b |
| 4236 | // where B is a subclass of A. |
| 4237 | // |
| 4238 | // Additionally, as for assignment, if either type is 'id' |
| 4239 | // allow silent coercion. Finally, if the types are |
| 4240 | // incompatible then make sure to use 'id' as the composite |
| 4241 | // type so the result is acceptable for sending messages to. |
| 4242 | |
| 4243 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4244 | // It could return the composite type. |
| 4245 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4246 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4247 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4248 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4249 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4250 | RHSTy->isObjCQualifiedIdType()) && |
| 4251 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4252 | // Need to handle "id<xx>" explicitly. |
| 4253 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4254 | // id. Currently localizing to here until clear this should be |
| 4255 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4256 | compositeType = Context.getObjCIdType(); |
| 4257 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4258 | compositeType = Context.getObjCIdType(); |
| 4259 | } else if (!(compositeType = |
| 4260 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4261 | ; |
| 4262 | else { |
| 4263 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4264 | << LHSTy << RHSTy |
| 4265 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4266 | QualType incompatTy = Context.getObjCIdType(); |
| 4267 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4268 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
| 4269 | return incompatTy; |
| 4270 | } |
| 4271 | // The object pointer types are compatible. |
| 4272 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 4273 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
| 4274 | return compositeType; |
| 4275 | } |
| 4276 | // Check Objective-C object pointer types and 'void *' |
| 4277 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4278 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4279 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4280 | QualType destPointee |
| 4281 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4282 | QualType destType = Context.getPointerType(destPointee); |
| 4283 | // Add qualifiers if necessary. |
| 4284 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4285 | // Promote to void*. |
| 4286 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
| 4287 | return destType; |
| 4288 | } |
| 4289 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4290 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4291 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4292 | QualType destPointee |
| 4293 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4294 | QualType destType = Context.getPointerType(destPointee); |
| 4295 | // Add qualifiers if necessary. |
| 4296 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 4297 | // Promote to void*. |
| 4298 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4299 | return destType; |
| 4300 | } |
| 4301 | return QualType(); |
| 4302 | } |
| 4303 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4304 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4305 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4306 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 4307 | SourceLocation ColonLoc, |
| 4308 | ExprArg Cond, ExprArg LHS, |
| 4309 | ExprArg RHS) { |
| 4310 | Expr *CondExpr = (Expr *) Cond.get(); |
| 4311 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4312 | |
| 4313 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4314 | // was the condition. |
| 4315 | bool isLHSNull = LHSExpr == 0; |
| 4316 | if (isLHSNull) |
| 4317 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4318 | |
| 4319 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4320 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4321 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4322 | return ExprError(); |
| 4323 | |
| 4324 | Cond.release(); |
| 4325 | LHS.release(); |
| 4326 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4327 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4328 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4329 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4332 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4333 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4334 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4335 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4336 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4337 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4338 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4339 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4340 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4341 | if ((lhsType->isObjCClassType() && |
| 4342 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4343 | (rhsType->isObjCClassType() && |
| 4344 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4345 | return Compatible; |
| 4346 | } |
| 4347 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4348 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4349 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4350 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4352 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4353 | lhptee = Context.getCanonicalType(lhptee); |
| 4354 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4355 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4356 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4357 | |
| 4358 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4359 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4360 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4361 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4362 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4363 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4364 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4365 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4366 | // 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] | 4367 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4368 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4369 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4370 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4371 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4372 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4373 | assert(rhptee->isFunctionType()); |
| 4374 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4375 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4376 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4377 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4378 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4379 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4380 | |
| 4381 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4382 | assert(lhptee->isFunctionType()); |
| 4383 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4384 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4385 | // 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] | 4386 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4387 | lhptee = lhptee.getUnqualifiedType(); |
| 4388 | rhptee = rhptee.getUnqualifiedType(); |
| 4389 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4390 | // Check if the pointee types are compatible ignoring the sign. |
| 4391 | // We explicitly check for char so that we catch "char" vs |
| 4392 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4393 | if (lhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4394 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4395 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4396 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4397 | |
| 4398 | if (rhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4399 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4400 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4401 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4402 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4403 | if (lhptee == rhptee) { |
| 4404 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4405 | // takes priority over sign incompatibility because the sign |
| 4406 | // warning can be disabled. |
| 4407 | if (ConvTy != Compatible) |
| 4408 | return ConvTy; |
| 4409 | return IncompatiblePointerSign; |
| 4410 | } |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4411 | |
| 4412 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4413 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4414 | // the eventual target type is the same and the pointers have the same |
| 4415 | // level of indirection, this must be the issue. |
| 4416 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4417 | do { |
| 4418 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4419 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 4420 | |
| 4421 | lhptee = Context.getCanonicalType(lhptee); |
| 4422 | rhptee = Context.getCanonicalType(rhptee); |
| 4423 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 4424 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4425 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4426 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4429 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4430 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4431 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4432 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4435 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4436 | /// block pointer types are compatible or whether a block and normal pointer |
| 4437 | /// are compatible. It is more restrict than comparing two function pointer |
| 4438 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4439 | Sema::AssignConvertType |
| 4440 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4441 | QualType rhsType) { |
| 4442 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4443 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4444 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4445 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4446 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4447 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4448 | // make sure we operate on the canonical type |
| 4449 | lhptee = Context.getCanonicalType(lhptee); |
| 4450 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4451 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4452 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4453 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4454 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4455 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4456 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4457 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4458 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4459 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4460 | return ConvTy; |
| 4461 | } |
| 4462 | |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4463 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4464 | /// for assignment compatibility. |
| 4465 | Sema::AssignConvertType |
| 4466 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4467 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 4468 | return Compatible; |
| 4469 | QualType lhptee = |
| 4470 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4471 | QualType rhptee = |
| 4472 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4473 | // make sure we operate on the canonical type |
| 4474 | lhptee = Context.getCanonicalType(lhptee); |
| 4475 | rhptee = Context.getCanonicalType(rhptee); |
| 4476 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4477 | return CompatiblePointerDiscardsQualifiers; |
| 4478 | |
| 4479 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4480 | return Compatible; |
| 4481 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4482 | return IncompatibleObjCQualifiedId; |
| 4483 | return IncompatiblePointer; |
| 4484 | } |
| 4485 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4486 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4487 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4488 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4489 | /// |
| 4490 | /// int a, *pint; |
| 4491 | /// short *pshort; |
| 4492 | /// struct foo *pfoo; |
| 4493 | /// |
| 4494 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4495 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4496 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4497 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4498 | /// |
| 4499 | /// 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] | 4500 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4501 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4502 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4503 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4504 | // Get canonical types. We're not formatting these types, just comparing |
| 4505 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4506 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4507 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4508 | |
| 4509 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4510 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4511 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4512 | if ((lhsType->isObjCClassType() && |
| 4513 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4514 | (rhsType->isObjCClassType() && |
| 4515 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4516 | return Compatible; |
| 4517 | } |
| 4518 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4519 | // If the left-hand side is a reference type, then we are in a |
| 4520 | // (rare!) case where we've allowed the use of references in C, |
| 4521 | // e.g., as a parameter type in a built-in function. In this case, |
| 4522 | // just make sure that the type referenced is compatible with the |
| 4523 | // right-hand side type. The caller is responsible for adjusting |
| 4524 | // lhsType so that the resulting expression does not have reference |
| 4525 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4526 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4527 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4528 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4529 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4530 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4531 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4532 | // to the same ExtVector type. |
| 4533 | if (lhsType->isExtVectorType()) { |
| 4534 | if (rhsType->isExtVectorType()) |
| 4535 | return lhsType == rhsType ? Compatible : Incompatible; |
| 4536 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 4537 | return Compatible; |
| 4538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4540 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4541 | // 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] | 4542 | // 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] | 4543 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4544 | if (getLangOptions().LaxVectorConversions && |
| 4545 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4546 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4547 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4548 | } |
| 4549 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4550 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4551 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4552 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4553 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4554 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4555 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4556 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4557 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4558 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4559 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4560 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4561 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4562 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4563 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4564 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4565 | return Compatible; |
| 4566 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4567 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4568 | if (rhsType->getAs<BlockPointerType>()) { |
| 4569 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4570 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4571 | |
| 4572 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4573 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4574 | return Compatible; |
| 4575 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4576 | return Incompatible; |
| 4577 | } |
| 4578 | |
| 4579 | if (isa<BlockPointerType>(lhsType)) { |
| 4580 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4581 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4582 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4583 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4584 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4585 | return Compatible; |
| 4586 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4587 | if (rhsType->isBlockPointerType()) |
| 4588 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4589 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4590 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4591 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4592 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4593 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4594 | return Incompatible; |
| 4595 | } |
| 4596 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4597 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4598 | if (rhsType->isIntegerType()) |
| 4599 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4601 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4602 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4603 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4604 | return Compatible; |
| 4605 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4606 | } |
| 4607 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4608 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4609 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4610 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4611 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4612 | return Compatible; |
| 4613 | } |
| 4614 | // Treat block pointers as objects. |
| 4615 | if (rhsType->isBlockPointerType()) |
| 4616 | return Compatible; |
| 4617 | return Incompatible; |
| 4618 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4619 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4620 | // 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] | 4621 | if (lhsType == Context.BoolTy) |
| 4622 | return Compatible; |
| 4623 | |
| 4624 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4625 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4626 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4627 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4628 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4629 | |
| 4630 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4631 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4632 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4633 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4634 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4635 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4636 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4637 | if (lhsType == Context.BoolTy) |
| 4638 | return Compatible; |
| 4639 | |
| 4640 | if (lhsType->isIntegerType()) |
| 4641 | return PointerToInt; |
| 4642 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4643 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4644 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4645 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4646 | return Compatible; |
| 4647 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4648 | } |
| 4649 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4650 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4651 | return Compatible; |
| 4652 | return Incompatible; |
| 4653 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4654 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4655 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4656 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4657 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4658 | } |
| 4659 | return Incompatible; |
| 4660 | } |
| 4661 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4662 | /// \brief Constructs a transparent union from an expression that is |
| 4663 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4665 | QualType UnionType, FieldDecl *Field) { |
| 4666 | // Build an initializer list that designates the appropriate member |
| 4667 | // of the transparent union. |
| 4668 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 4669 | &E, 1, |
| 4670 | SourceLocation()); |
| 4671 | Initializer->setType(UnionType); |
| 4672 | Initializer->setInitializedFieldInUnion(Field); |
| 4673 | |
| 4674 | // Build a compound literal constructing a value of the transparent |
| 4675 | // union type from this initializer list. |
| 4676 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 4677 | false); |
| 4678 | } |
| 4679 | |
| 4680 | Sema::AssignConvertType |
| 4681 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4682 | QualType FromType = rExpr->getType(); |
| 4683 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | // 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] | 4685 | // transparent_union GCC extension. |
| 4686 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4687 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4688 | return Incompatible; |
| 4689 | |
| 4690 | // The field to initialize within the transparent union. |
| 4691 | RecordDecl *UD = UT->getDecl(); |
| 4692 | FieldDecl *InitField = 0; |
| 4693 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4694 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4695 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4696 | it != itend; ++it) { |
| 4697 | if (it->getType()->isPointerType()) { |
| 4698 | // If the transparent union contains a pointer type, we allow: |
| 4699 | // 1) void pointer |
| 4700 | // 2) null pointer constant |
| 4701 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4702 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4703 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4704 | InitField = *it; |
| 4705 | break; |
| 4706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4708 | if (rExpr->isNullPointerConstant(Context, |
| 4709 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4710 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4711 | InitField = *it; |
| 4712 | break; |
| 4713 | } |
| 4714 | } |
| 4715 | |
| 4716 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4717 | == Compatible) { |
| 4718 | InitField = *it; |
| 4719 | break; |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | if (!InitField) |
| 4724 | return Incompatible; |
| 4725 | |
| 4726 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4727 | return Compatible; |
| 4728 | } |
| 4729 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4730 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4731 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4732 | if (getLangOptions().CPlusPlus) { |
| 4733 | if (!lhsType->isRecordType()) { |
| 4734 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4735 | // expression is implicitly converted (C++ 4) to the |
| 4736 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4737 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4738 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4739 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4740 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4744 | // structures. |
| 4745 | } |
| 4746 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4747 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4748 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | if ((lhsType->isPointerType() || |
| 4750 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4751 | lhsType->isBlockPointerType()) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4752 | && rExpr->isNullPointerConstant(Context, |
| 4753 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4754 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4755 | return Compatible; |
| 4756 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4757 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4758 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4759 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4760 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4761 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4762 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4763 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4764 | if (!lhsType->isReferenceType()) |
| 4765 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4766 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4767 | Sema::AssignConvertType result = |
| 4768 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4769 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4770 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4771 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4772 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4773 | // so that we can use references in built-in functions even in C. |
| 4774 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4775 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4776 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4777 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4778 | CastExpr::CK_Unknown); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4779 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4782 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4783 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4784 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4785 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4786 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4789 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4790 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4791 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4792 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4793 | QualType lhsType = |
| 4794 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4795 | QualType rhsType = |
| 4796 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4797 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4798 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4799 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4800 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4801 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4802 | // Handle the case of a vector & extvector type of the same size and element |
| 4803 | // 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] | 4804 | if (getLangOptions().LaxVectorConversions) { |
| 4805 | // FIXME: Should we warn here? |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4806 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4807 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4808 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4809 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4810 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4814 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4815 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4816 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4817 | bool swapped = false; |
| 4818 | if (rhsType->isExtVectorType()) { |
| 4819 | swapped = true; |
| 4820 | std::swap(rex, lex); |
| 4821 | std::swap(rhsType, lhsType); |
| 4822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4823 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4824 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4825 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4826 | QualType EltTy = LV->getElementType(); |
| 4827 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4828 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4829 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4830 | if (swapped) std::swap(rex, lex); |
| 4831 | return lhsType; |
| 4832 | } |
| 4833 | } |
| 4834 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4835 | rhsType->isRealFloatingType()) { |
| 4836 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4837 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4838 | if (swapped) std::swap(rex, lex); |
| 4839 | return lhsType; |
| 4840 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4841 | } |
| 4842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4843 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4844 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4845 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4846 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4847 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4848 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4851 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4852 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4853 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4854 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4855 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4856 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4857 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4858 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4859 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4860 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4865 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4866 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4867 | return CheckVectorOperands(Loc, lex, rex); |
| 4868 | return InvalidOperands(Loc, lex, rex); |
| 4869 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4870 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4871 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4872 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4873 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4874 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4875 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4880 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4881 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4882 | if (CompLHSTy) *CompLHSTy = compType; |
| 4883 | return compType; |
| 4884 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4885 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4886 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4888 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4889 | if (lex->getType()->isArithmeticType() && |
| 4890 | rex->getType()->isArithmeticType()) { |
| 4891 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4892 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4893 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4894 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4895 | // Put any potential pointer into PExp |
| 4896 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4897 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4898 | std::swap(PExp, IExp); |
| 4899 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4900 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4901 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4902 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4903 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4905 | // Check for arithmetic on pointers to incomplete types. |
| 4906 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4907 | if (getLangOptions().CPlusPlus) { |
| 4908 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4909 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4910 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4911 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4912 | |
| 4913 | // GNU extension: arithmetic on pointer to void |
| 4914 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4915 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4916 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4917 | if (getLangOptions().CPlusPlus) { |
| 4918 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4919 | << lex->getType() << lex->getSourceRange(); |
| 4920 | return QualType(); |
| 4921 | } |
| 4922 | |
| 4923 | // GNU extension: arithmetic on pointer to function |
| 4924 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4925 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4926 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4927 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4929 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4930 | PExp->getType()->isObjCObjectPointerType()) && |
| 4931 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4932 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4933 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4934 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4935 | return QualType(); |
| 4936 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4937 | // Diagnose bad cases where we step over interface counts. |
| 4938 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4939 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4940 | << PointeeTy << PExp->getSourceRange(); |
| 4941 | return QualType(); |
| 4942 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4944 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4945 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4946 | if (LHSTy.isNull()) { |
| 4947 | LHSTy = lex->getType(); |
| 4948 | if (LHSTy->isPromotableIntegerType()) |
| 4949 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4950 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4951 | *CompLHSTy = LHSTy; |
| 4952 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4953 | return PExp->getType(); |
| 4954 | } |
| 4955 | } |
| 4956 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4957 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4960 | // C99 6.5.6 |
| 4961 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4962 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4963 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4964 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4965 | if (CompLHSTy) *CompLHSTy = compType; |
| 4966 | return compType; |
| 4967 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4968 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4969 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4970 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4971 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4972 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4973 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4974 | if (lex->getType()->isArithmeticType() |
| 4975 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4976 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4977 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4979 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4980 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4981 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4982 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4983 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4984 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4986 | bool ComplainAboutVoid = false; |
| 4987 | Expr *ComplainAboutFunc = 0; |
| 4988 | if (lpointee->isVoidType()) { |
| 4989 | if (getLangOptions().CPlusPlus) { |
| 4990 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4991 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4992 | return QualType(); |
| 4993 | } |
| 4994 | |
| 4995 | // GNU C extension: arithmetic on pointer to void |
| 4996 | ComplainAboutVoid = true; |
| 4997 | } else if (lpointee->isFunctionType()) { |
| 4998 | if (getLangOptions().CPlusPlus) { |
| 4999 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5000 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5001 | return QualType(); |
| 5002 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5003 | |
| 5004 | // GNU C extension: arithmetic on pointer to function |
| 5005 | ComplainAboutFunc = lex; |
| 5006 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5007 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5008 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5009 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5010 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5011 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5012 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5013 | // Diagnose bad cases where we step over interface counts. |
| 5014 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5015 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5016 | << lpointee << lex->getSourceRange(); |
| 5017 | return QualType(); |
| 5018 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5020 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5021 | if (rex->getType()->isIntegerType()) { |
| 5022 | if (ComplainAboutVoid) |
| 5023 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5024 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5025 | if (ComplainAboutFunc) |
| 5026 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5028 | << ComplainAboutFunc->getSourceRange(); |
| 5029 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5030 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5031 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5032 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5033 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5034 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5035 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 5036 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5037 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5038 | // RHS must be a completely-type object type. |
| 5039 | // Handle the GNU void* extension. |
| 5040 | if (rpointee->isVoidType()) { |
| 5041 | if (getLangOptions().CPlusPlus) { |
| 5042 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5043 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5044 | return QualType(); |
| 5045 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5046 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5047 | ComplainAboutVoid = true; |
| 5048 | } else if (rpointee->isFunctionType()) { |
| 5049 | if (getLangOptions().CPlusPlus) { |
| 5050 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5051 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5052 | return QualType(); |
| 5053 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5054 | |
| 5055 | // GNU extension: arithmetic on pointer to function |
| 5056 | if (!ComplainAboutFunc) |
| 5057 | ComplainAboutFunc = rex; |
| 5058 | } else if (!rpointee->isDependentType() && |
| 5059 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5060 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 5061 | << rex->getSourceRange() |
| 5062 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5063 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5064 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5065 | if (getLangOptions().CPlusPlus) { |
| 5066 | // Pointee types must be the same: C++ [expr.add] |
| 5067 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 5068 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5069 | << lex->getType() << rex->getType() |
| 5070 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5071 | return QualType(); |
| 5072 | } |
| 5073 | } else { |
| 5074 | // Pointee types must be compatible C99 6.5.6p3 |
| 5075 | if (!Context.typesAreCompatible( |
| 5076 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 5077 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 5078 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5079 | << lex->getType() << rex->getType() |
| 5080 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5081 | return QualType(); |
| 5082 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5083 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5085 | if (ComplainAboutVoid) |
| 5086 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5087 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5088 | if (ComplainAboutFunc) |
| 5089 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5090 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5091 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5092 | |
| 5093 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5094 | return Context.getPointerDiffType(); |
| 5095 | } |
| 5096 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5097 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5098 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5101 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5102 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5103 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5104 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 5105 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5106 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5107 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 5108 | // Vector shifts promote their scalar inputs to vector type. |
| 5109 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 5110 | return CheckVectorOperands(Loc, lex, rex); |
| 5111 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5112 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 5113 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5114 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5115 | if (LHSTy.isNull()) { |
| 5116 | LHSTy = lex->getType(); |
| 5117 | if (LHSTy->isPromotableIntegerType()) |
| 5118 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5119 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 5120 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5121 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5122 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5123 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5124 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5125 | // Sanity-check shift operands |
| 5126 | llvm::APSInt Right; |
| 5127 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 5128 | if (!rex->isValueDependent() && |
| 5129 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 5130 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5131 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 5132 | else { |
| 5133 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 5134 | Context.getTypeSize(lex->getType())); |
| 5135 | if (Right.uge(LeftBits)) |
| 5136 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 5137 | } |
| 5138 | } |
| 5139 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5140 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5141 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5144 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5145 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5146 | unsigned OpaqueOpc, bool isRelational) { |
| 5147 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 5148 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5149 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5150 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5151 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5152 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5153 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 5154 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5155 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5156 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 5157 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5158 | UsualArithmeticConversions(lex, rex); |
| 5159 | else { |
| 5160 | UsualUnaryConversions(lex); |
| 5161 | UsualUnaryConversions(rex); |
| 5162 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5163 | QualType lType = lex->getType(); |
| 5164 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5165 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5166 | if (!lType->isFloatingType() |
| 5167 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5168 | // For non-floating point types, check for self-comparisons of the form |
| 5169 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5170 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5171 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 5172 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5173 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5174 | Expr *RHSStripped = rex->IgnoreParens(); |
| 5175 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 5176 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 5177 | if (DRL->getDecl() == DRR->getDecl() && |
| 5178 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5179 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5181 | if (isa<CastExpr>(LHSStripped)) |
| 5182 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5183 | if (isa<CastExpr>(RHSStripped)) |
| 5184 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5185 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5186 | // Warn about comparisons against a string constant (unless the other |
| 5187 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5188 | Expr *literalString = 0; |
| 5189 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5190 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5191 | !RHSStripped->isNullPointerConstant(Context, |
| 5192 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5193 | literalString = lex; |
| 5194 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5195 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5196 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5197 | !LHSStripped->isNullPointerConstant(Context, |
| 5198 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5199 | literalString = rex; |
| 5200 | literalStringStripped = RHSStripped; |
| 5201 | } |
| 5202 | |
| 5203 | if (literalString) { |
| 5204 | std::string resultComparison; |
| 5205 | switch (Opc) { |
| 5206 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 5207 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 5208 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 5209 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 5210 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 5211 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 5212 | default: assert(false && "Invalid comparison operator"); |
| 5213 | } |
| 5214 | Diag(Loc, diag::warn_stringcompare) |
| 5215 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 5216 | << literalString->getSourceRange() |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5217 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 5218 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 5219 | "strcmp(") |
| 5220 | << CodeModificationHint::CreateInsertion( |
| 5221 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5222 | resultComparison); |
| 5223 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5224 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5225 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5226 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5227 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5228 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5229 | if (isRelational) { |
| 5230 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5231 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5232 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5233 | // Check for comparisons of floating point operands using != and ==. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5234 | if (lType->isFloatingType() && rType->isFloatingType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5235 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5236 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5237 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5238 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5239 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5240 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5241 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 5242 | Expr::NPC_ValueDependentIsNull); |
| 5243 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 5244 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5245 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5246 | // All of the following pointer related warnings are GCC extensions, except |
| 5247 | // when handling null pointer constants. One day, we can consider making them |
| 5248 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5249 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5250 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5251 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5252 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5253 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5254 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5255 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5256 | if (LCanPointeeTy == RCanPointeeTy) |
| 5257 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5258 | if (!isRelational && |
| 5259 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5260 | // Valid unless comparison between non-null pointer and function pointer |
| 5261 | // This is a gcc extension compatibility comparison. |
| 5262 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5263 | && !LHSIsNull && !RHSIsNull) { |
| 5264 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5265 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5266 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
| 5267 | return ResultTy; |
| 5268 | } |
| 5269 | } |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5270 | // C++ [expr.rel]p2: |
| 5271 | // [...] Pointer conversions (4.10) and qualification |
| 5272 | // conversions (4.4) are performed on pointer operands (or on |
| 5273 | // a pointer operand and a null pointer constant) to bring |
| 5274 | // them to their composite pointer type. [...] |
| 5275 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5276 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5277 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 5278 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5279 | if (T.isNull()) { |
| 5280 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5281 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5282 | return QualType(); |
| 5283 | } |
| 5284 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5285 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5286 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5287 | return ResultTy; |
| 5288 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5289 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5290 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5291 | RCanPointeeTy.getUnqualifiedType())) { |
| 5292 | // Valid unless a relational comparison of function pointers |
| 5293 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5294 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5295 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5296 | } |
| 5297 | } else if (!isRelational && |
| 5298 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5299 | // Valid unless comparison between non-null pointer and function pointer |
| 5300 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5301 | && !LHSIsNull && !RHSIsNull) { |
| 5302 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5303 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5304 | } |
| 5305 | } else { |
| 5306 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5307 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5308 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5309 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5310 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5311 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5312 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5315 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5317 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5318 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5319 | (lType->isPointerType() || |
| 5320 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5321 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5322 | return ResultTy; |
| 5323 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5324 | if (LHSIsNull && |
| 5325 | (rType->isPointerType() || |
| 5326 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5327 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5328 | return ResultTy; |
| 5329 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5330 | |
| 5331 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5333 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5334 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | // In addition, pointers to members can be compared, or a pointer to |
| 5336 | // member and a null pointer constant. Pointer to member conversions |
| 5337 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5338 | // them to a common type. If one operand is a null pointer constant, |
| 5339 | // the common type is the type of the other operand. Otherwise, the |
| 5340 | // common type is a pointer to member type similar (4.4) to the type |
| 5341 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5342 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5343 | // types. |
| 5344 | QualType T = FindCompositePointerType(lex, rex); |
| 5345 | if (T.isNull()) { |
| 5346 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5347 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5348 | return QualType(); |
| 5349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5351 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5352 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5353 | return ResultTy; |
| 5354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5355 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5356 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5357 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5358 | return ResultTy; |
| 5359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5360 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5361 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5362 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5363 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5364 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5365 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5366 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5367 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5368 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5369 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5370 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5371 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5372 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5373 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5374 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5375 | if (!isRelational |
| 5376 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5377 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5378 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5379 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5380 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5381 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5382 | ->getPointeeType()->isVoidType()))) |
| 5383 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5384 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5385 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5386 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5387 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5388 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5389 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5390 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5391 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5392 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5393 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5394 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5395 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5396 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5397 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5398 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5399 | if (!LPtrToVoid && !RPtrToVoid && |
| 5400 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5401 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5402 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5403 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5404 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5405 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5406 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5407 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5408 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5409 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5410 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5411 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5412 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5413 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5414 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5415 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5416 | unsigned DiagID = 0; |
| 5417 | if (RHSIsNull) { |
| 5418 | if (isRelational) |
| 5419 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5420 | } else if (isRelational) |
| 5421 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5422 | else |
| 5423 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5425 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5426 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5427 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5428 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5429 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5430 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5431 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5432 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5433 | unsigned DiagID = 0; |
| 5434 | if (LHSIsNull) { |
| 5435 | if (isRelational) |
| 5436 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5437 | } else if (isRelational) |
| 5438 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5439 | else |
| 5440 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5442 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5443 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5444 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5445 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5446 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5447 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5448 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5449 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5450 | if (!isRelational && RHSIsNull |
| 5451 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5452 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5453 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5454 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5455 | if (!isRelational && LHSIsNull |
| 5456 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5457 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5458 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5459 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5460 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5463 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5464 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5465 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5466 | /// types. |
| 5467 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5468 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5469 | bool isRelational) { |
| 5470 | // Check to make sure we're operating on vectors of the same type and width, |
| 5471 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5472 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5473 | if (vType.isNull()) |
| 5474 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5475 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5476 | QualType lType = lex->getType(); |
| 5477 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5478 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5479 | // For non-floating point types, check for self-comparisons of the form |
| 5480 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5481 | // often indicate logic errors in the program. |
| 5482 | if (!lType->isFloatingType()) { |
| 5483 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5484 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5485 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5486 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5487 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5488 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5489 | // Check for comparisons of floating point operands using != and ==. |
| 5490 | if (!isRelational && lType->isFloatingType()) { |
| 5491 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5492 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5493 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5494 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5495 | // Return the type for the comparison, which is the same as vector type for |
| 5496 | // integer vectors, or an integer type of identical size and number of |
| 5497 | // elements for floating point vectors. |
| 5498 | if (lType->isIntegerType()) |
| 5499 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5500 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5501 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5502 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5503 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5504 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5505 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5506 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5507 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5508 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5509 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5510 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5511 | } |
| 5512 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5513 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5514 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 5515 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5516 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5517 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5518 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5519 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5520 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5521 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5522 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5527 | if (!Context.getLangOptions().CPlusPlus) { |
| 5528 | UsualUnaryConversions(lex); |
| 5529 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5530 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5531 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5532 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5533 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5534 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5535 | } |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5536 | |
| 5537 | // C++ [expr.log.and]p1 |
| 5538 | // C++ [expr.log.or]p1 |
| 5539 | // The operands are both implicitly converted to type bool (clause 4). |
| 5540 | StandardConversionSequence LHS; |
| 5541 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 5542 | /*InOverloadResolution=*/false, LHS)) |
| 5543 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5544 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5545 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5546 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5547 | return InvalidOperands(Loc, lex, rex); |
| 5548 | |
| 5549 | StandardConversionSequence RHS; |
| 5550 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 5551 | /*InOverloadResolution=*/false, RHS)) |
| 5552 | return InvalidOperands(Loc, lex, rex); |
| 5553 | |
| 5554 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5555 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5556 | return InvalidOperands(Loc, lex, rex); |
| 5557 | |
| 5558 | // C++ [expr.log.and]p2 |
| 5559 | // C++ [expr.log.or]p2 |
| 5560 | // The result is a bool. |
| 5561 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5564 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5565 | /// is a read-only property; return true if so. A readonly property expression |
| 5566 | /// depends on various declarations and thus must be treated specially. |
| 5567 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5568 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5569 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5570 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5571 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5572 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5573 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5574 | BaseType->getAsObjCInterfacePointerType()) |
| 5575 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5576 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5577 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5578 | } |
| 5579 | } |
| 5580 | return false; |
| 5581 | } |
| 5582 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5583 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5584 | /// emit an error and return true. If so, return false. |
| 5585 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5586 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5588 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5589 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5590 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5591 | if (IsLV == Expr::MLV_Valid) |
| 5592 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5593 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5594 | unsigned Diag = 0; |
| 5595 | bool NeedType = false; |
| 5596 | switch (IsLV) { // C99 6.5.16p2 |
| 5597 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 5598 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5599 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5600 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5601 | NeedType = true; |
| 5602 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5603 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5604 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5605 | NeedType = true; |
| 5606 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5607 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5608 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5609 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5610 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5611 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5612 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5613 | case Expr::MLV_IncompleteType: |
| 5614 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5615 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5616 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 5617 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5618 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5619 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5620 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5621 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5622 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5623 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5624 | case Expr::MLV_ReadonlyProperty: |
| 5625 | Diag = diag::error_readonly_property_assignment; |
| 5626 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5627 | case Expr::MLV_NoSetterProperty: |
| 5628 | Diag = diag::error_nosetter_property_assignment; |
| 5629 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5630 | case Expr::MLV_SubObjCPropertySetting: |
| 5631 | Diag = diag::error_no_subobject_property_setting; |
| 5632 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5633 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5634 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5635 | SourceRange Assign; |
| 5636 | if (Loc != OrigLoc) |
| 5637 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5638 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5639 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5640 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5642 | return true; |
| 5643 | } |
| 5644 | |
| 5645 | |
| 5646 | |
| 5647 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5648 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5649 | SourceLocation Loc, |
| 5650 | QualType CompoundType) { |
| 5651 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5652 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5653 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5654 | |
| 5655 | QualType LHSType = LHS->getType(); |
| 5656 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5657 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5658 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5659 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5660 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5661 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5662 | // Special case of NSObject attributes on c-style pointer types. |
| 5663 | if (ConvTy == IncompatiblePointer && |
| 5664 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5665 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5666 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5667 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5668 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5669 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5670 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5671 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5672 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5673 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5674 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5675 | RHSCheck = ICE->getSubExpr(); |
| 5676 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5677 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5678 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5679 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5680 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5681 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5682 | // And there is a space or other character before the subexpr of the |
| 5683 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5684 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5685 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5686 | Diag(Loc, diag::warn_not_compound_assign) |
| 5687 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5688 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5689 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5690 | } |
| 5691 | } else { |
| 5692 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5693 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5694 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5695 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5696 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5697 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5698 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5700 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5701 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5702 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5703 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5704 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5705 | // 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] | 5706 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5707 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5710 | // C99 6.5.17 |
| 5711 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5712 | // 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] | 5713 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5714 | |
| 5715 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5716 | // incomplete in C++). |
| 5717 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5718 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5721 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5722 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5723 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5724 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5725 | if (Op->isTypeDependent()) |
| 5726 | return Context.DependentTy; |
| 5727 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5728 | QualType ResType = Op->getType(); |
| 5729 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5730 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5731 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5732 | // Decrement of bool is not allowed. |
| 5733 | if (!isInc) { |
| 5734 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5735 | return QualType(); |
| 5736 | } |
| 5737 | // Increment of bool sets it to true, but is deprecated. |
| 5738 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5739 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5740 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5741 | } else if (ResType->isAnyPointerType()) { |
| 5742 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5743 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5744 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5745 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5746 | if (getLangOptions().CPlusPlus) { |
| 5747 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5748 | << Op->getSourceRange(); |
| 5749 | return QualType(); |
| 5750 | } |
| 5751 | |
| 5752 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5753 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5754 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5755 | if (getLangOptions().CPlusPlus) { |
| 5756 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5757 | << Op->getType() << Op->getSourceRange(); |
| 5758 | return QualType(); |
| 5759 | } |
| 5760 | |
| 5761 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5762 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5763 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5764 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5766 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5767 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5768 | // Diagnose bad cases where we step over interface counts. |
| 5769 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5770 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5771 | << PointeeTy << Op->getSourceRange(); |
| 5772 | return QualType(); |
| 5773 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 5774 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5775 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5776 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5777 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5778 | } else { |
| 5779 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 5780 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5781 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5782 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5783 | // 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] | 5784 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5785 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5786 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5787 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5790 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5791 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5792 | /// where the declaration is needed for type checking. We only need to |
| 5793 | /// handle cases when the expression references a function designator |
| 5794 | /// or is an lvalue. Here are some examples: |
| 5795 | /// - &(x) => x |
| 5796 | /// - &*****f => f for f a function designator. |
| 5797 | /// - &s.xx => s |
| 5798 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5799 | /// - *(x + 1) -> x, if x is an array |
| 5800 | /// - &"123"[2] -> 0 |
| 5801 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5802 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5803 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5804 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5805 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5806 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5807 | // If this is an arrow operator, the address is an offset from |
| 5808 | // the base's value, so the object the base refers to is |
| 5809 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5810 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5811 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5812 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5813 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5814 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5815 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5816 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5817 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5818 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5819 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5820 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5821 | } |
| 5822 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5823 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5824 | case Stmt::UnaryOperatorClass: { |
| 5825 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5826 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5827 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5828 | case UnaryOperator::Real: |
| 5829 | case UnaryOperator::Imag: |
| 5830 | case UnaryOperator::Extension: |
| 5831 | return getPrimaryDecl(UO->getSubExpr()); |
| 5832 | default: |
| 5833 | return 0; |
| 5834 | } |
| 5835 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5836 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5837 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5838 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5839 | // If the result of an implicit cast is an l-value, we care about |
| 5840 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5841 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5842 | default: |
| 5843 | return 0; |
| 5844 | } |
| 5845 | } |
| 5846 | |
| 5847 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5848 | /// 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] | 5849 | /// 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] | 5850 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5851 | /// 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] | 5852 | /// 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] | 5853 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5854 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5855 | // Make sure to ignore parentheses in subsequent checks |
| 5856 | op = op->IgnoreParens(); |
| 5857 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5858 | if (op->isTypeDependent()) |
| 5859 | return Context.DependentTy; |
| 5860 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5861 | if (getLangOptions().C99) { |
| 5862 | // Implement C99-only parts of addressof rules. |
| 5863 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5864 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5865 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5866 | // (assuming the deref expression is valid). |
| 5867 | return uOp->getSubExpr()->getType(); |
| 5868 | } |
| 5869 | // Technically, there should be a check for array subscript |
| 5870 | // expressions here, but the result of one is always an lvalue anyway. |
| 5871 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5872 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5873 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5874 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5875 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5876 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5877 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5878 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5879 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5880 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5881 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5882 | return QualType(); |
| 5883 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5884 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5885 | // The operand cannot be a bit-field |
| 5886 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5887 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5888 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5889 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5890 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5891 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5892 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5893 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5894 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5895 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5896 | // cannot take address of a property expression. |
| 5897 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5898 | << "property expression" << op->getSourceRange(); |
| 5899 | return QualType(); |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5900 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5901 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 474e102 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5902 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5903 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5904 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5905 | return Context.OverloadTy; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5906 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5907 | // 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] | 5908 | // with the register storage-class specifier. |
| 5909 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5910 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5911 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5912 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5913 | return QualType(); |
| 5914 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5915 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5916 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5917 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5918 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5919 | // Could be a pointer to member, though, if there is an explicit |
| 5920 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5921 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5922 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5923 | if (Ctx && Ctx->isRecord()) { |
| 5924 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5925 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5926 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5927 | << FD->getDeclName() << FD->getType(); |
| 5928 | return QualType(); |
| 5929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5930 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5931 | return Context.getMemberPointerType(op->getType(), |
| 5932 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5933 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5934 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5935 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5936 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5937 | // As above. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5938 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 5939 | MD->isInstance()) |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5940 | return Context.getMemberPointerType(op->getType(), |
| 5941 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5942 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5943 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5944 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5945 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5946 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5947 | // Taking the address of a void variable is technically illegal, but we |
| 5948 | // allow it in cases which are otherwise valid. |
| 5949 | // Example: "extern void x; void* y = &x;". |
| 5950 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5951 | } |
| 5952 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5953 | // If the operand has type "type", the result has type "pointer to type". |
| 5954 | return Context.getPointerType(op->getType()); |
| 5955 | } |
| 5956 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5957 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5958 | if (Op->isTypeDependent()) |
| 5959 | return Context.DependentTy; |
| 5960 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5961 | UsualUnaryConversions(Op); |
| 5962 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5963 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5964 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5965 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5966 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5967 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5968 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5969 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5970 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5971 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5972 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5973 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5974 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5975 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5976 | return QualType(); |
| 5977 | } |
| 5978 | |
| 5979 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5980 | tok::TokenKind Kind) { |
| 5981 | BinaryOperator::Opcode Opc; |
| 5982 | switch (Kind) { |
| 5983 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5984 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5985 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5986 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5987 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5988 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5989 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5990 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5991 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5992 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5993 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5994 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5995 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5996 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5997 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5998 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5999 | case tok::amp: Opc = BinaryOperator::And; break; |
| 6000 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 6001 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 6002 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 6003 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 6004 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 6005 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 6006 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 6007 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 6008 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 6009 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 6010 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 6011 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 6012 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 6013 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 6014 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 6015 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 6016 | } |
| 6017 | return Opc; |
| 6018 | } |
| 6019 | |
| 6020 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 6021 | tok::TokenKind Kind) { |
| 6022 | UnaryOperator::Opcode Opc; |
| 6023 | switch (Kind) { |
| 6024 | default: assert(0 && "Unknown unary op!"); |
| 6025 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 6026 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 6027 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 6028 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 6029 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 6030 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 6031 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 6032 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6033 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 6034 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 6035 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 6036 | } |
| 6037 | return Opc; |
| 6038 | } |
| 6039 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6040 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 6041 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 6042 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6043 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 6044 | unsigned Op, |
| 6045 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6046 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6047 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6048 | // The following two variables are used for compound assignment operators |
| 6049 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 6050 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6051 | |
| 6052 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6053 | case BinaryOperator::Assign: |
| 6054 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 6055 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6056 | case BinaryOperator::PtrMemD: |
| 6057 | case BinaryOperator::PtrMemI: |
| 6058 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 6059 | Opc == BinaryOperator::PtrMemI); |
| 6060 | break; |
| 6061 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6062 | case BinaryOperator::Div: |
| 6063 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 6064 | break; |
| 6065 | case BinaryOperator::Rem: |
| 6066 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 6067 | break; |
| 6068 | case BinaryOperator::Add: |
| 6069 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 6070 | break; |
| 6071 | case BinaryOperator::Sub: |
| 6072 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 6073 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6074 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6075 | case BinaryOperator::Shr: |
| 6076 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 6077 | break; |
| 6078 | case BinaryOperator::LE: |
| 6079 | case BinaryOperator::LT: |
| 6080 | case BinaryOperator::GE: |
| 6081 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6082 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6083 | break; |
| 6084 | case BinaryOperator::EQ: |
| 6085 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6086 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6087 | break; |
| 6088 | case BinaryOperator::And: |
| 6089 | case BinaryOperator::Xor: |
| 6090 | case BinaryOperator::Or: |
| 6091 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 6092 | break; |
| 6093 | case BinaryOperator::LAnd: |
| 6094 | case BinaryOperator::LOr: |
| 6095 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 6096 | break; |
| 6097 | case BinaryOperator::MulAssign: |
| 6098 | case BinaryOperator::DivAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6099 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 6100 | CompLHSTy = CompResultTy; |
| 6101 | if (!CompResultTy.isNull()) |
| 6102 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6103 | break; |
| 6104 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6105 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6106 | CompLHSTy = CompResultTy; |
| 6107 | if (!CompResultTy.isNull()) |
| 6108 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6109 | break; |
| 6110 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6111 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6112 | if (!CompResultTy.isNull()) |
| 6113 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6114 | break; |
| 6115 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6116 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6117 | if (!CompResultTy.isNull()) |
| 6118 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6119 | break; |
| 6120 | case BinaryOperator::ShlAssign: |
| 6121 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6122 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6123 | CompLHSTy = CompResultTy; |
| 6124 | if (!CompResultTy.isNull()) |
| 6125 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6126 | break; |
| 6127 | case BinaryOperator::AndAssign: |
| 6128 | case BinaryOperator::XorAssign: |
| 6129 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6130 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6131 | CompLHSTy = CompResultTy; |
| 6132 | if (!CompResultTy.isNull()) |
| 6133 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6134 | break; |
| 6135 | case BinaryOperator::Comma: |
| 6136 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6137 | break; |
| 6138 | } |
| 6139 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6140 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6141 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6142 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6143 | else |
| 6144 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6145 | CompLHSTy, CompResultTy, |
| 6146 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6149 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6150 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6151 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6152 | const PartialDiagnostic &PD, |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6153 | SourceRange ParenRange, |
| 6154 | const PartialDiagnostic &SecondPD = PartialDiagnostic(0), |
| 6155 | SourceRange SecondParenRange = SourceRange()) { |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6156 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6157 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6158 | // We can't display the parentheses, so just dig the |
| 6159 | // warning/error and return. |
| 6160 | Self.Diag(Loc, PD); |
| 6161 | return; |
| 6162 | } |
| 6163 | |
| 6164 | Self.Diag(Loc, PD) |
| 6165 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6166 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6167 | |
| 6168 | if (!SecondPD.getDiagID()) |
| 6169 | return; |
| 6170 | |
| 6171 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6172 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6173 | // We can't display the parentheses, so just dig the |
| 6174 | // warning/error and return. |
| 6175 | Self.Diag(Loc, SecondPD); |
| 6176 | return; |
| 6177 | } |
| 6178 | |
| 6179 | Self.Diag(Loc, SecondPD) |
| 6180 | << CodeModificationHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6181 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6184 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6185 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6186 | /// comparison operators have higher precedence. The most typical example of |
| 6187 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6188 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6189 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6190 | typedef BinaryOperator BinOp; |
| 6191 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6192 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6193 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6194 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6195 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6196 | rhsopc = BO->getOpcode(); |
| 6197 | |
| 6198 | // Subs are not binary operators. |
| 6199 | if (lhsopc == -1 && rhsopc == -1) |
| 6200 | return; |
| 6201 | |
| 6202 | // Bitwise operations are sometimes used as eager logical ops. |
| 6203 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6204 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6205 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6206 | return; |
| 6207 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6208 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6209 | SuggestParentheses(Self, OpLoc, |
| 6210 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6211 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6212 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6213 | lhs->getSourceRange(), |
| 6214 | PDiag(diag::note_precedence_bitwise_first) |
| 6215 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6216 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 6217 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6218 | SuggestParentheses(Self, OpLoc, |
| 6219 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6220 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6221 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6222 | rhs->getSourceRange(), |
| 6223 | PDiag(diag::note_precedence_bitwise_first) |
| 6224 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6225 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6229 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6230 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 6231 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6232 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6233 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6234 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6235 | } |
| 6236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6237 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6238 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 6239 | tok::TokenKind Kind, |
| 6240 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6241 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6242 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6243 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6244 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6245 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6246 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6247 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6248 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6249 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6250 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6251 | } |
| 6252 | |
| 6253 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 6254 | BinaryOperator::Opcode Opc, |
| 6255 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6256 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6257 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6258 | rhs->getType()->isOverloadableType())) { |
| 6259 | // Find all of the overloaded operators visible from this |
| 6260 | // point. We perform both an operator-name lookup from the local |
| 6261 | // scope and an argument-dependent lookup based on the types of |
| 6262 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6263 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6264 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 6265 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6266 | if (S) |
| 6267 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6268 | Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6269 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6270 | DeclarationName OpName |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6271 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6272 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6273 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6274 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6275 | // Build the (potentially-overloaded, potentially-dependent) |
| 6276 | // binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6277 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6278 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6279 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6280 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6281 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6284 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6285 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6286 | ExprArg InputArg) { |
| 6287 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6288 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6289 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6290 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6291 | QualType resultType; |
| 6292 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6293 | case UnaryOperator::OffsetOf: |
| 6294 | assert(false && "Invalid unary operator"); |
| 6295 | break; |
| 6296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6297 | case UnaryOperator::PreInc: |
| 6298 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6299 | case UnaryOperator::PostInc: |
| 6300 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6301 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6302 | Opc == UnaryOperator::PreInc || |
| 6303 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6304 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6305 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6306 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 6307 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6308 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 6309 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6310 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 6311 | break; |
| 6312 | case UnaryOperator::Plus: |
| 6313 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6314 | UsualUnaryConversions(Input); |
| 6315 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6316 | if (resultType->isDependentType()) |
| 6317 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6318 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 6319 | break; |
| 6320 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6321 | resultType->isEnumeralType()) |
| 6322 | break; |
| 6323 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 6324 | Opc == UnaryOperator::Plus && |
| 6325 | resultType->isPointerType()) |
| 6326 | break; |
| 6327 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6328 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6329 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6330 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6331 | UsualUnaryConversions(Input); |
| 6332 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6333 | if (resultType->isDependentType()) |
| 6334 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6335 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6336 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6337 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6338 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6339 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6340 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6341 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6342 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6343 | break; |
| 6344 | case UnaryOperator::LNot: // logical negation |
| 6345 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6346 | DefaultFunctionArrayConversion(Input); |
| 6347 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6348 | if (resultType->isDependentType()) |
| 6349 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6350 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6351 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6352 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6353 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6354 | // In C++, it's bool. C++ 5.3.1p8 |
| 6355 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6356 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6357 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6358 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 6359 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6360 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6361 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6362 | resultType = Input->getType(); |
| 6363 | break; |
| 6364 | } |
| 6365 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6366 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6367 | |
| 6368 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6369 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6372 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6373 | UnaryOperator::Opcode Opc, |
| 6374 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6375 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6376 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 6377 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6378 | // Find all of the overloaded operators visible from this |
| 6379 | // point. We perform both an operator-name lookup from the local |
| 6380 | // scope and an argument-dependent lookup based on the types of |
| 6381 | // the arguments. |
| 6382 | FunctionSet Functions; |
| 6383 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 6384 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6385 | if (S) |
| 6386 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 6387 | Functions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6388 | DeclarationName OpName |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6389 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6390 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6391 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6392 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6393 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 6394 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6396 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 6397 | } |
| 6398 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6399 | // Unary Operators. 'Tok' is the token for the operator. |
| 6400 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6401 | tok::TokenKind Op, ExprArg input) { |
| 6402 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 6403 | } |
| 6404 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6405 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6406 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 6407 | SourceLocation LabLoc, |
| 6408 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6409 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 6410 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6411 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6412 | // If we haven't seen this label yet, create a forward reference. It |
| 6413 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6414 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6415 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6417 | // 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] | 6418 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6419 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6420 | } |
| 6421 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6422 | Sema::OwningExprResult |
| 6423 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 6424 | SourceLocation RPLoc) { // "({..})" |
| 6425 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6426 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6427 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6428 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6429 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6430 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6431 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6432 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6433 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6434 | // example, it is not possible to goto into a stmt expression apparently. |
| 6435 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6436 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6437 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6438 | // as the type of the stmtexpr. |
| 6439 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6440 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6441 | if (!Compound->body_empty()) { |
| 6442 | Stmt *LastStmt = Compound->body_back(); |
| 6443 | // If LastStmt is a label, skip down through into the body. |
| 6444 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6445 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6446 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6447 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6448 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6449 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6450 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6451 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6452 | // expressions are not lvalues. |
| 6453 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6454 | substmt.release(); |
| 6455 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6456 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6457 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6458 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 6459 | SourceLocation BuiltinLoc, |
| 6460 | SourceLocation TypeLoc, |
| 6461 | TypeTy *argty, |
| 6462 | OffsetOfComponent *CompPtr, |
| 6463 | unsigned NumComponents, |
| 6464 | SourceLocation RPLoc) { |
| 6465 | // FIXME: This function leaks all expressions in the offset components on |
| 6466 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6467 | // FIXME: Preserve type source info. |
| 6468 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6469 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6470 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6471 | bool Dependent = ArgTy->isDependentType(); |
| 6472 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6473 | // We must have at least one component that refers to the type, and the first |
| 6474 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6475 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6476 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6477 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6478 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6479 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 6480 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 6481 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6482 | // Otherwise, create a null pointer as the base, and iteratively process |
| 6483 | // the offsetof designators. |
| 6484 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 6485 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6486 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6487 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 6488 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6489 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6490 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6491 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6492 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6493 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6494 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6495 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6496 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6497 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 6498 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 6500 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 6501 | diag::err_offsetof_incomplete_type)) |
| 6502 | return ExprError(); |
| 6503 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6504 | // FIXME: Dependent case loses a lot of information here. And probably |
| 6505 | // leaks like a sieve. |
| 6506 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6507 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6508 | if (OC.isBrackets) { |
| 6509 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6510 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 6511 | if (!AT) { |
| 6512 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6513 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6514 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 6518 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6519 | // Promote the array so it looks more like a normal array subscript |
| 6520 | // expression. |
| 6521 | DefaultFunctionArrayConversion(Res); |
| 6522 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6523 | // C99 6.5.2.1p1 |
| 6524 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6525 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6526 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6527 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 6528 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6529 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6530 | |
| 6531 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 6532 | OC.LocEnd); |
| 6533 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6534 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6535 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6536 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6537 | if (!RC) { |
| 6538 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6539 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6540 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6541 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 6542 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6543 | // Get the decl corresponding to this. |
| 6544 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6545 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 6546 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| 6547 | DiagRuntimeBehavior(BuiltinLoc, |
| 6548 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6549 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6550 | << Res->getType())) |
| 6551 | DidWarnAboutNonPOD = true; |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6553 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6554 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6555 | LookupQualifiedName(R, RD); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6556 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6557 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6558 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6559 | if (!MemberDecl) |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6560 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6561 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6562 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6563 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 6564 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6565 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 6566 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 09b6d0e | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 6567 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6568 | } else { |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 6569 | PerformObjectMemberConversion(Res, MemberDecl); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6570 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 6571 | // doesn't matter here. |
| 6572 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 6573 | MemberDecl->getType().getNonReferenceType()); |
| 6574 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6575 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6576 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6577 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6578 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 6579 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
| 6582 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6583 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 6584 | TypeTy *arg1,TypeTy *arg2, |
| 6585 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6586 | // FIXME: Preserve type source info. |
| 6587 | QualType argT1 = GetTypeFromParser(arg1); |
| 6588 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6589 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6590 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6591 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 6592 | if (getLangOptions().CPlusPlus) { |
| 6593 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 6594 | << SourceRange(BuiltinLoc, RPLoc); |
| 6595 | return ExprError(); |
| 6596 | } |
| 6597 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6598 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 6599 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6602 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 6603 | ExprArg cond, |
| 6604 | ExprArg expr1, ExprArg expr2, |
| 6605 | SourceLocation RPLoc) { |
| 6606 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 6607 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 6608 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6609 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6610 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 6611 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6612 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6613 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 6614 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6615 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6616 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6617 | } else { |
| 6618 | // The conditional expression is required to be a constant expression. |
| 6619 | llvm::APSInt condEval(32); |
| 6620 | SourceLocation ExpLoc; |
| 6621 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6622 | return ExprError(Diag(ExpLoc, |
| 6623 | diag::err_typecheck_choose_expr_requires_constant) |
| 6624 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6625 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6626 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 6627 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6628 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 6629 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6630 | } |
| 6631 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6632 | cond.release(); expr1.release(); expr2.release(); |
| 6633 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6634 | resType, RPLoc, |
| 6635 | resType->isDependentType(), |
| 6636 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6639 | //===----------------------------------------------------------------------===// |
| 6640 | // Clang Extensions. |
| 6641 | //===----------------------------------------------------------------------===// |
| 6642 | |
| 6643 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6644 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6645 | // Analyze block parameters. |
| 6646 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6647 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6648 | // Add BSI to CurBlock. |
| 6649 | BSI->PrevBlockInfo = CurBlock; |
| 6650 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6651 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6652 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6653 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 6654 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 6655 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6656 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 6657 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6658 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6659 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Ted Kremenek | 3cdff23 | 2009-12-07 22:01:30 +0000 | [diff] [blame] | 6660 | CurContext->addDecl(BSI->TheDecl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6661 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6664 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6665 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6666 | |
| 6667 | if (ParamInfo.getNumTypeObjects() == 0 |
| 6668 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6669 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6670 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 6671 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6672 | if (T->isArrayType()) { |
| 6673 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6674 | diag::err_block_returns_array); |
| 6675 | return; |
| 6676 | } |
| 6677 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6678 | // The parameter list is optional, if there was none, assume (). |
| 6679 | if (!T->isFunctionType()) |
| 6680 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6681 | |
| 6682 | CurBlock->hasPrototype = true; |
| 6683 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6684 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6685 | if (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 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6690 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6691 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6692 | // Do not allow returning a objc interface by-value. |
| 6693 | if (RetTy->isObjCInterfaceType()) { |
| 6694 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6695 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6696 | return; |
| 6697 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6698 | return; |
| 6699 | } |
| 6700 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6701 | // Analyze arguments to block. |
| 6702 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6703 | "Not a function declarator!"); |
| 6704 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6705 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6706 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6707 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6708 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6709 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6710 | // no arguments, not a function that takes a single void argument. |
| 6711 | if (FTI.hasPrototype && |
| 6712 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6713 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6714 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6715 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6716 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6717 | } else if (FTI.hasPrototype) { |
| 6718 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6719 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6720 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6721 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6722 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6723 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6724 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6725 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6726 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6727 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6728 | // If this has an identifier, add it to the scope stack. |
| 6729 | if ((*AI)->getIdentifier()) |
| 6730 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6731 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6732 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6733 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6734 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6735 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6736 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6737 | // FIXME: remove the attribute. |
| 6738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6739 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6740 | // Analyze the return type. |
| 6741 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6742 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6743 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6744 | // Do not allow returning a objc interface by-value. |
| 6745 | if (RetTy->isObjCInterfaceType()) { |
| 6746 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6747 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6748 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6749 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6753 | /// is invoked to pop the information about the block from the action impl. |
| 6754 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6755 | // Ensure that CurBlock is deleted. |
| 6756 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6757 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6758 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6759 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6760 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6761 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6762 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6763 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6767 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6768 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6769 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6770 | // If blocks are disabled, emit an error. |
| 6771 | if (!LangOpts.Blocks) |
| 6772 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6773 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6774 | // Ensure that CurBlock is deleted. |
| 6775 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6776 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6777 | PopDeclContext(); |
| 6778 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6779 | // Pop off CurBlock, handle nested blocks. |
| 6780 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6781 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6782 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6783 | if (!BSI->ReturnType.isNull()) |
| 6784 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6785 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6786 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6787 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6788 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6789 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6790 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6791 | QualType BlockTy; |
| 6792 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6793 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6794 | NoReturn); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6795 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6796 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6797 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6798 | NoReturn); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6799 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6800 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6801 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6802 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6803 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6804 | // If needed, diagnose invalid gotos and switches in the block. |
| 6805 | if (CurFunctionNeedsScopeChecking) |
| 6806 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6807 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6808 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6809 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6810 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6811 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6812 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6813 | } |
| 6814 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6815 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6816 | ExprArg expr, TypeTy *type, |
| 6817 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6818 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6819 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6820 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6821 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6822 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6823 | |
| 6824 | // Get the va_list type |
| 6825 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6826 | if (VaListType->isArrayType()) { |
| 6827 | // Deal with implicit array decay; for example, on x86-64, |
| 6828 | // va_list is an array, but it's supposed to decay to |
| 6829 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6830 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6831 | // Make sure the input expression also decays appropriately. |
| 6832 | UsualUnaryConversions(E); |
| 6833 | } else { |
| 6834 | // Otherwise, the va_list argument must be an l-value because |
| 6835 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6836 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6837 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6838 | return ExprError(); |
| 6839 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6840 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6841 | if (!E->isTypeDependent() && |
| 6842 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6843 | return ExprError(Diag(E->getLocStart(), |
| 6844 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6845 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6846 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6847 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6848 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6849 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6850 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6851 | expr.release(); |
| 6852 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6853 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6856 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6857 | // The type of __null will be int or long, depending on the size of |
| 6858 | // pointers on the target. |
| 6859 | QualType Ty; |
| 6860 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6861 | Ty = Context.IntTy; |
| 6862 | else |
| 6863 | Ty = Context.LongTy; |
| 6864 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6865 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6868 | static void |
| 6869 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6870 | QualType DstType, |
| 6871 | Expr *SrcExpr, |
| 6872 | CodeModificationHint &Hint) { |
| 6873 | if (!SemaRef.getLangOptions().ObjC1) |
| 6874 | return; |
| 6875 | |
| 6876 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6877 | if (!PT) |
| 6878 | return; |
| 6879 | |
| 6880 | // Check if the destination is of type 'id'. |
| 6881 | if (!PT->isObjCIdType()) { |
| 6882 | // Check if the destination is the 'NSString' interface. |
| 6883 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6884 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6885 | return; |
| 6886 | } |
| 6887 | |
| 6888 | // Strip off any parens and casts. |
| 6889 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6890 | if (!SL || SL->isWide()) |
| 6891 | return; |
| 6892 | |
| 6893 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6894 | } |
| 6895 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6896 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6897 | SourceLocation Loc, |
| 6898 | QualType DstType, QualType SrcType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6899 | Expr *SrcExpr, AssignmentAction Action) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6900 | // Decode the result (notice that AST's are still created for extensions). |
| 6901 | bool isInvalid = false; |
| 6902 | unsigned DiagKind; |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6903 | CodeModificationHint Hint; |
| 6904 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6905 | switch (ConvTy) { |
| 6906 | default: assert(0 && "Unknown conversion type"); |
| 6907 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6908 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6909 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6910 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6911 | case IntToPointer: |
| 6912 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6913 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6914 | case IncompatiblePointer: |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6915 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6916 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6917 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6918 | case IncompatiblePointerSign: |
| 6919 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6920 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6921 | case FunctionVoidPointer: |
| 6922 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6923 | break; |
| 6924 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 6925 | // If the qualifiers lost were because we were applying the |
| 6926 | // (deprecated) C++ conversion from a string literal to a char* |
| 6927 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 6928 | // Ideally, this check would be performed in |
| 6929 | // CheckPointerTypesForAssignment. However, that would require a |
| 6930 | // bit of refactoring (so that the second argument is an |
| 6931 | // expression, rather than a type), which should be done as part |
| 6932 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 6933 | // C++ semantics. |
| 6934 | if (getLangOptions().CPlusPlus && |
| 6935 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 6936 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6937 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 6938 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 6939 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 6940 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6941 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6942 | case IntToBlockPointer: |
| 6943 | DiagKind = diag::err_int_to_block_pointer; |
| 6944 | break; |
| 6945 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 6946 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6947 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6948 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6949 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6950 | // it can give a more specific diagnostic. |
| 6951 | DiagKind = diag::warn_incompatible_qualified_id; |
| 6952 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6953 | case IncompatibleVectors: |
| 6954 | DiagKind = diag::warn_incompatible_vectors; |
| 6955 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6956 | case Incompatible: |
| 6957 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 6958 | isInvalid = true; |
| 6959 | break; |
| 6960 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6962 | Diag(Loc, DiagKind) << DstType << SrcType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6963 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6964 | return isInvalid; |
| 6965 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6966 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 6967 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6968 | llvm::APSInt ICEResult; |
| 6969 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 6970 | if (Result) |
| 6971 | *Result = ICEResult; |
| 6972 | return false; |
| 6973 | } |
| 6974 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6975 | Expr::EvalResult EvalResult; |
| 6976 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6977 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6978 | EvalResult.HasSideEffects) { |
| 6979 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 6980 | |
| 6981 | if (EvalResult.Diag) { |
| 6982 | // We only show the note if it's not the usual "invalid subexpression" |
| 6983 | // or if it's actually in a subexpression. |
| 6984 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 6985 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 6986 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 6987 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6988 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6989 | return true; |
| 6990 | } |
| 6991 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6992 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 6993 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6994 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6995 | if (EvalResult.Diag && |
| 6996 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 6997 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6998 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6999 | if (Result) |
| 7000 | *Result = EvalResult.Val.getInt(); |
| 7001 | return false; |
| 7002 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7003 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7004 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7005 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7006 | ExprEvalContexts.push_back( |
| 7007 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7010 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7011 | Sema::PopExpressionEvaluationContext() { |
| 7012 | // Pop the current expression evaluation context off the stack. |
| 7013 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 7014 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7015 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 7016 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 7017 | if (Rec.PotentiallyReferenced) { |
| 7018 | // Mark any remaining declarations in the current position of the stack |
| 7019 | // as "referenced". If they were not meant to be referenced, semantic |
| 7020 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 7021 | for (PotentiallyReferencedDecls::iterator |
| 7022 | I = Rec.PotentiallyReferenced->begin(), |
| 7023 | IEnd = Rec.PotentiallyReferenced->end(); |
| 7024 | I != IEnd; ++I) |
| 7025 | MarkDeclarationReferenced(I->first, I->second); |
| 7026 | } |
| 7027 | |
| 7028 | if (Rec.PotentiallyDiagnosed) { |
| 7029 | // Emit any pending diagnostics. |
| 7030 | for (PotentiallyEmittedDiagnostics::iterator |
| 7031 | I = Rec.PotentiallyDiagnosed->begin(), |
| 7032 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 7033 | I != IEnd; ++I) |
| 7034 | Diag(I->first, I->second); |
| 7035 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | // When are coming out of an unevaluated context, clear out any |
| 7039 | // temporaries that we may have created as part of the evaluation of |
| 7040 | // the expression in that context: they aren't relevant because they |
| 7041 | // will never be constructed. |
| 7042 | if (Rec.Context == Unevaluated && |
| 7043 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 7044 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 7045 | ExprTemporaries.end()); |
| 7046 | |
| 7047 | // Destroy the popped expression evaluation record. |
| 7048 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7049 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7050 | |
| 7051 | /// \brief Note that the given declaration was referenced in the source code. |
| 7052 | /// |
| 7053 | /// This routine should be invoke whenever a given declaration is referenced |
| 7054 | /// in the source code, and where that reference occurred. If this declaration |
| 7055 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 7056 | /// C99 6.9p3), then the declaration will be marked as used. |
| 7057 | /// |
| 7058 | /// \param Loc the location where the declaration was referenced. |
| 7059 | /// |
| 7060 | /// \param D the declaration that has been referenced by the source code. |
| 7061 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 7062 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7064 | if (D->isUsed()) |
| 7065 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7066 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 7067 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 7068 | // template or not. The reason for this is that unevaluated expressions |
| 7069 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 7070 | // -Wunused-parameters) |
| 7071 | if (isa<ParmVarDecl>(D) || |
| 7072 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7073 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7074 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7075 | // Do not mark anything as "used" within a dependent context; wait for |
| 7076 | // an instantiation. |
| 7077 | if (CurContext->isDependentContext()) |
| 7078 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7079 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7080 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7081 | case Unevaluated: |
| 7082 | // We are in an expression that is not potentially evaluated; do nothing. |
| 7083 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7085 | case PotentiallyEvaluated: |
| 7086 | // We are in a potentially-evaluated expression, so this declaration is |
| 7087 | // "used"; handle this below. |
| 7088 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7089 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7090 | case PotentiallyPotentiallyEvaluated: |
| 7091 | // We are in an expression that may be potentially evaluated; queue this |
| 7092 | // declaration reference until we know whether the expression is |
| 7093 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7094 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7095 | return; |
| 7096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7097 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7098 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 7099 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7100 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7101 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 7102 | if (!Constructor->isUsed()) |
| 7103 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7104 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 7105 | Constructor->isCopyConstructor(TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7106 | if (!Constructor->isUsed()) |
| 7107 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7108 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7109 | |
| 7110 | MaybeMarkVirtualMembersReferenced(Loc, Constructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7111 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 7112 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 7113 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7114 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7115 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7116 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7117 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 7118 | if (!MethodDecl->isUsed()) |
| 7119 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 7120 | } |
| 7121 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7122 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7123 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7124 | // class templates. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7125 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7126 | bool AlreadyInstantiated = false; |
| 7127 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7128 | = Function->getTemplateSpecializationInfo()) { |
| 7129 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7130 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7131 | else if (SpecInfo->getTemplateSpecializationKind() |
| 7132 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7133 | AlreadyInstantiated = true; |
| 7134 | } else if (MemberSpecializationInfo *MSInfo |
| 7135 | = Function->getMemberSpecializationInfo()) { |
| 7136 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7137 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7138 | else if (MSInfo->getTemplateSpecializationKind() |
| 7139 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7140 | AlreadyInstantiated = true; |
| 7141 | } |
| 7142 | |
| 7143 | if (!AlreadyInstantiated) |
| 7144 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
| 7145 | } |
| 7146 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7147 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7148 | Function->setUsed(true); |
| 7149 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7150 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7151 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7152 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7153 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7154 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7155 | Var->getInstantiatedFromStaticDataMember()) { |
| 7156 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7157 | assert(MSInfo && "Missing member specialization information?"); |
| 7158 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7159 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7160 | MSInfo->setPointOfInstantiation(Loc); |
| 7161 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 7162 | } |
| 7163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7164 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7165 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7166 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7167 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7168 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7169 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7170 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7171 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7172 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 7173 | /// of the program being compiled. |
| 7174 | /// |
| 7175 | /// This routine emits the given diagnostic when the code currently being |
| 7176 | /// type-checked is "potentially evaluated", meaning that there is a |
| 7177 | /// possibility that the code will actually be executable. Code in sizeof() |
| 7178 | /// expressions, code used only during overload resolution, etc., are not |
| 7179 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 7180 | /// in the absolutely nutty case of potentially potentially evaluated |
| 7181 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
| 7182 | /// later. |
| 7183 | /// |
| 7184 | /// This routine should be used for all diagnostics that describe the run-time |
| 7185 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 7186 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 7187 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
| 7188 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, |
| 7189 | const PartialDiagnostic &PD) { |
| 7190 | switch (ExprEvalContexts.back().Context ) { |
| 7191 | case Unevaluated: |
| 7192 | // The argument will never be evaluated, so don't complain. |
| 7193 | break; |
| 7194 | |
| 7195 | case PotentiallyEvaluated: |
| 7196 | Diag(Loc, PD); |
| 7197 | return true; |
| 7198 | |
| 7199 | case PotentiallyPotentiallyEvaluated: |
| 7200 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 7201 | break; |
| 7202 | } |
| 7203 | |
| 7204 | return false; |
| 7205 | } |
| 7206 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7207 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7208 | CallExpr *CE, FunctionDecl *FD) { |
| 7209 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7210 | return false; |
| 7211 | |
| 7212 | PartialDiagnostic Note = |
| 7213 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7214 | << FD->getDeclName() : PDiag(); |
| 7215 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 7216 | |
| 7217 | if (RequireCompleteType(Loc, ReturnType, |
| 7218 | FD ? |
| 7219 | PDiag(diag::err_call_function_incomplete_return) |
| 7220 | << CE->getSourceRange() << FD->getDeclName() : |
| 7221 | PDiag(diag::err_call_incomplete_return) |
| 7222 | << CE->getSourceRange(), |
| 7223 | std::make_pair(NoteLoc, Note))) |
| 7224 | return true; |
| 7225 | |
| 7226 | return false; |
| 7227 | } |
| 7228 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7229 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7230 | // will prevent this condition from triggering, which is what we want. |
| 7231 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7232 | SourceLocation Loc; |
| 7233 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7234 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7235 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7236 | if (isa<BinaryOperator>(E)) { |
| 7237 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 7238 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 7239 | return; |
| 7240 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7241 | // Greylist some idioms by putting them into a warning subcategory. |
| 7242 | if (ObjCMessageExpr *ME |
| 7243 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7244 | Selector Sel = ME->getSelector(); |
| 7245 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7246 | // self = [<foo> init...] |
| 7247 | if (isSelfExpr(Op->getLHS()) |
| 7248 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7249 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7250 | |
| 7251 | // <foo> = [<bar> nextObject] |
| 7252 | else if (Sel.isUnarySelector() && |
| 7253 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7254 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7255 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7256 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7257 | Loc = Op->getOperatorLoc(); |
| 7258 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7259 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7260 | if (Op->getOperator() != OO_Equal) |
| 7261 | return; |
| 7262 | |
| 7263 | Loc = Op->getOperatorLoc(); |
| 7264 | } else { |
| 7265 | // Not an assignment. |
| 7266 | return; |
| 7267 | } |
| 7268 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7269 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7270 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7271 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7272 | Diag(Loc, diagnostic) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7273 | << E->getSourceRange() |
| 7274 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 7275 | << CodeModificationHint::CreateInsertion(Close, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7276 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 7277 | << CodeModificationHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7278 | } |
| 7279 | |
| 7280 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7281 | DiagnoseAssignmentAsCondition(E); |
| 7282 | |
| 7283 | if (!E->isTypeDependent()) { |
| 7284 | DefaultFunctionArrayConversion(E); |
| 7285 | |
| 7286 | QualType T = E->getType(); |
| 7287 | |
| 7288 | if (getLangOptions().CPlusPlus) { |
| 7289 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7290 | return true; |
| 7291 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7292 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7293 | << T << E->getSourceRange(); |
| 7294 | return true; |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | return false; |
| 7299 | } |