Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/Lex/LiteralSupport.h" |
| 26 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 27 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 28 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 29 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 30 | #include "clang/Parse/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 34 | /// \brief Determine whether the use of this declaration is valid, and |
| 35 | /// emit any corresponding diagnostics. |
| 36 | /// |
| 37 | /// This routine diagnoses various problems with referencing |
| 38 | /// declarations that can occur when using a declaration. For example, |
| 39 | /// it might warn if a deprecated or unavailable declaration is being |
| 40 | /// used, or produce an error (and return true) if a C++0x deleted |
| 41 | /// function is being used. |
| 42 | /// |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 43 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 44 | /// decls. |
| 45 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 46 | /// \returns true if there was an error (this declaration cannot be |
| 47 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 48 | /// |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 49 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 50 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 51 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 52 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 55 | // See if the decl is unavailable |
| 56 | if (D->getAttr<UnavailableAttr>()) { |
| 57 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 58 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 59 | } |
| 60 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 61 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 62 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 63 | if (FD->isDeleted()) { |
| 64 | Diag(Loc, diag::err_deleted_function_use); |
| 65 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 66 | return true; |
| 67 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 68 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 70 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 73 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 75 | /// attribute. It warns if call does not have the sentinel argument. |
| 76 | /// |
| 77 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 79 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 81 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 82 | int sentinelPos = attr->getSentinel(); |
| 83 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 85 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 86 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 87 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 88 | bool warnNotEnoughArgs = false; |
| 89 | int isMethod = 0; |
| 90 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 91 | // skip over named parameters. |
| 92 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 93 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 94 | if (nullPos) |
| 95 | --nullPos; |
| 96 | else |
| 97 | ++i; |
| 98 | } |
| 99 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 100 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 101 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 102 | // skip over named parameters. |
| 103 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 104 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 105 | if (nullPos) |
| 106 | --nullPos; |
| 107 | else |
| 108 | ++i; |
| 109 | } |
| 110 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 111 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 112 | // block or function pointer call. |
| 113 | QualType Ty = V->getType(); |
| 114 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 116 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 117 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 118 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 119 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 120 | unsigned k; |
| 121 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 122 | if (nullPos) |
| 123 | --nullPos; |
| 124 | else |
| 125 | ++i; |
| 126 | } |
| 127 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 128 | } |
| 129 | if (Ty->isBlockPointerType()) |
| 130 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 131 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 132 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 133 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
| 136 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 137 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 138 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | int sentinel = i; |
| 142 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 143 | --sentinelPos; |
| 144 | ++i; |
| 145 | } |
| 146 | if (sentinelPos > 0) { |
| 147 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 148 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | while (i < NumArgs-1) { |
| 152 | ++i; |
| 153 | ++sentinel; |
| 154 | } |
| 155 | Expr *sentinelExpr = Args[sentinel]; |
Anders Carlsson | e4d2bdd | 2009-11-24 17:24:21 +0000 | [diff] [blame] | 156 | if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) && |
| 157 | (!sentinelExpr->getType()->isPointerType() || |
| 158 | !sentinelExpr->isNullPointerConstant(Context, |
| 159 | Expr::NPC_ValueDependentIsNull)))) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 160 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 161 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | } |
| 163 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 166 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 167 | Expr *Ex = (Expr *)E; |
| 168 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 169 | } |
| 170 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 171 | //===----------------------------------------------------------------------===// |
| 172 | // Standard Promotions and Conversions |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 175 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 176 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 177 | QualType Ty = E->getType(); |
| 178 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 179 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 180 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 182 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 183 | else if (Ty->isArrayType()) { |
| 184 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 185 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 186 | // type 'array of type' is converted to an expression that has type 'pointer |
| 187 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 188 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 189 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 190 | // |
| 191 | // C++ 4.2p1: |
| 192 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 193 | // T" can be converted to an rvalue of type "pointer to T". |
| 194 | // |
| 195 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 196 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 197 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 198 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 204 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 205 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 206 | /// In these instances, this routine should *not* be called. |
| 207 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 208 | QualType Ty = Expr->getType(); |
| 209 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 211 | // C99 6.3.1.1p2: |
| 212 | // |
| 213 | // The following may be used in an expression wherever an int or |
| 214 | // unsigned int may be used: |
| 215 | // - an object or expression with an integer type whose integer |
| 216 | // conversion rank is less than or equal to the rank of int |
| 217 | // and unsigned int. |
| 218 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 219 | // |
| 220 | // If an int can represent all values of the original type, the |
| 221 | // value is converted to an int; otherwise, it is converted to an |
| 222 | // unsigned int. These are called the integer promotions. All |
| 223 | // other types are unchanged by the integer promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 224 | QualType PTy = Context.isPromotableBitField(Expr); |
| 225 | if (!PTy.isNull()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 226 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 227 | return Expr; |
| 228 | } |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 229 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 230 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 231 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 232 | return Expr; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 235 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 236 | return Expr; |
| 237 | } |
| 238 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 239 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 241 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 242 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 243 | QualType Ty = Expr->getType(); |
| 244 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 246 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 247 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 248 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 249 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 250 | CastExpr::CK_FloatingCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 252 | UsualUnaryConversions(Expr); |
| 253 | } |
| 254 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 255 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 256 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 257 | /// interfaces passed by value. This returns true if the argument type is |
| 258 | /// completely illegal. |
| 259 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 260 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 262 | if (Expr->getType()->isObjCInterfaceType()) { |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 263 | switch (ExprEvalContexts.back().Context ) { |
| 264 | case Unevaluated: |
| 265 | // The argument will never be evaluated, so don't complain. |
| 266 | break; |
| 267 | |
| 268 | case PotentiallyEvaluated: |
| 269 | Diag(Expr->getLocStart(), |
| 270 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 271 | << Expr->getType() << CT; |
| 272 | return true; |
| 273 | |
| 274 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 275 | ExprEvalContexts.back().addDiagnostic(Expr->getLocStart(), |
| 276 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 277 | << Expr->getType() << CT); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 282 | if (!Expr->getType()->isPODType()) { |
| 283 | switch (ExprEvalContexts.back().Context ) { |
| 284 | case Unevaluated: |
| 285 | // The argument will never be evaluated, so don't complain. |
| 286 | break; |
| 287 | |
| 288 | case PotentiallyEvaluated: |
| 289 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 290 | << Expr->getType() << CT; |
| 291 | break; |
| 292 | |
| 293 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 294 | ExprEvalContexts.back().addDiagnostic(Expr->getLocStart(), |
| 295 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 296 | << Expr->getType() << CT); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | } |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 300 | |
| 301 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 305 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 306 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 308 | /// responsible for emitting appropriate error diagnostics. |
| 309 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 310 | /// GCC. |
| 311 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 312 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 313 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 314 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 315 | |
| 316 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 319 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 320 | QualType lhs = |
| 321 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 323 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 324 | |
| 325 | // If both types are identical, no conversion is needed. |
| 326 | if (lhs == rhs) |
| 327 | return lhs; |
| 328 | |
| 329 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 330 | // The caller can deal with this (e.g. pointer + int). |
| 331 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 332 | return lhs; |
| 333 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 334 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 335 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 336 | if (!LHSBitfieldPromoteTy.isNull()) |
| 337 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 338 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 339 | if (!RHSBitfieldPromoteTy.isNull()) |
| 340 | rhs = RHSBitfieldPromoteTy; |
| 341 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 342 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 343 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 344 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 345 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 346 | return destType; |
| 347 | } |
| 348 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 349 | //===----------------------------------------------------------------------===// |
| 350 | // Semantic Analysis for various Expression Types |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | |
| 353 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 354 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 356 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 357 | /// multiple tokens. However, the common case is that StringToks points to one |
| 358 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 359 | /// |
| 360 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 361 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | assert(NumStringToks && "Must have at least one string!"); |
| 363 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 364 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 366 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | |
| 368 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 369 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 370 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 371 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 372 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 373 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 374 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 375 | |
| 376 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 377 | if (getLangOptions().CPlusPlus) |
| 378 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 379 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 380 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 381 | // the nul terminator character as well as the string length for pascal |
| 382 | // strings. |
| 383 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 384 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 385 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 387 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 389 | Literal.GetStringLength(), |
| 390 | Literal.AnyWide, StrTy, |
| 391 | &StringTokLocs[0], |
| 392 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 395 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 396 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 397 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 398 | /// for values inside the block or for globals). |
| 399 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 400 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 401 | /// up-to-date. |
| 402 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 403 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 404 | ValueDecl *VD) { |
| 405 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 406 | // we wanted to. |
| 407 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 408 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 410 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 411 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 412 | return false; |
| 413 | |
| 414 | // If this is a reference to an extern, static, or global variable, no need to |
| 415 | // snapshot it. |
| 416 | // FIXME: What about 'const' variables in C++? |
| 417 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 418 | if (!Var->hasLocalStorage()) |
| 419 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 421 | // Blocks that have these can't be constant. |
| 422 | CurBlock->hasBlockDeclRefExprs = true; |
| 423 | |
| 424 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 425 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 426 | // be defined outside all of the current blocks (in which case the blocks do |
| 427 | // all get the bit). Walk the nesting chain. |
| 428 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 429 | NextBlock = NextBlock->PrevBlockInfo) { |
| 430 | // If we found the defining block for the variable, don't mark the block as |
| 431 | // having a reference outside it. |
| 432 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 433 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 435 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 436 | // a snapshot as well. |
| 437 | NextBlock->hasBlockDeclRefExprs = true; |
| 438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 440 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 443 | |
| 444 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 445 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 446 | Sema::OwningExprResult |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 447 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 448 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 449 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 450 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 452 | << D->getDeclName(); |
| 453 | return ExprError(); |
| 454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 456 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 457 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 458 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 459 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 461 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 463 | << D->getIdentifier(); |
| 464 | return ExprError(); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 470 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 472 | return Owned(DeclRefExpr::Create(Context, |
| 473 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 474 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 475 | D, Loc, Ty)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 478 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 479 | /// variable corresponding to the anonymous union or struct whose type |
| 480 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 481 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 482 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 484 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 486 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 487 | // operation rather than a slow walk through DeclContext's vector (which |
| 488 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 489 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 491 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 492 | D != DEnd; ++D) { |
| 493 | if (*D == Record) { |
| 494 | // The object for the anonymous struct/union directly |
| 495 | // follows its type in the list of declarations. |
| 496 | ++D; |
| 497 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 498 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 499 | return *D; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | assert(false && "Missing object for anonymous record"); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 507 | /// \brief Given a field that represents a member of an anonymous |
| 508 | /// struct/union, build the path from that field's context to the |
| 509 | /// actual member. |
| 510 | /// |
| 511 | /// Construct the sequence of field member references we'll have to |
| 512 | /// perform to get to the field in the anonymous union/struct. The |
| 513 | /// list of members is built from the field outward, so traverse it |
| 514 | /// backwards to go from an object in the current context to the field |
| 515 | /// we found. |
| 516 | /// |
| 517 | /// \returns The variable from which the field access should begin, |
| 518 | /// for an anonymous struct/union that is not a member of another |
| 519 | /// class. Otherwise, returns NULL. |
| 520 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 521 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 522 | assert(Field->getDeclContext()->isRecord() && |
| 523 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 524 | && "Field must be stored inside an anonymous struct or union"); |
| 525 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 526 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 527 | VarDecl *BaseObject = 0; |
| 528 | DeclContext *Ctx = Field->getDeclContext(); |
| 529 | do { |
| 530 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 531 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 532 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 533 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 534 | else { |
| 535 | BaseObject = cast<VarDecl>(AnonObject); |
| 536 | break; |
| 537 | } |
| 538 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 540 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 541 | |
| 542 | return BaseObject; |
| 543 | } |
| 544 | |
| 545 | Sema::OwningExprResult |
| 546 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 547 | FieldDecl *Field, |
| 548 | Expr *BaseObjectExpr, |
| 549 | SourceLocation OpLoc) { |
| 550 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 552 | AnonFields); |
| 553 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 554 | // Build the expression that refers to the base object, from |
| 555 | // which we will build a sequence of member references to each |
| 556 | // of the anonymous union objects and, eventually, the field we |
| 557 | // found via name lookup. |
| 558 | bool BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 559 | Qualifiers BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 560 | if (BaseObject) { |
| 561 | // BaseObject is an anonymous struct/union variable (and is, |
| 562 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 563 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 564 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 565 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 566 | SourceLocation()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 567 | BaseQuals |
| 568 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 569 | } else if (BaseObjectExpr) { |
| 570 | // The caller provided the base object expression. Determine |
| 571 | // whether its a pointer and whether it adds any qualifiers to the |
| 572 | // anonymous struct/union fields we're looking into. |
| 573 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 574 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 575 | BaseObjectIsPointer = true; |
| 576 | ObjectType = ObjectPtr->getPointeeType(); |
| 577 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 578 | BaseQuals |
| 579 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 580 | } else { |
| 581 | // We've found a member of an anonymous struct/union that is |
| 582 | // inside a non-anonymous struct/union, so in a well-formed |
| 583 | // program our base object expression is "this". |
| 584 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 585 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 587 | = Context.getTagDeclType( |
| 588 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 589 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 591 | == Context.getCanonicalType(ThisType)) || |
| 592 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 593 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 594 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 595 | MD->getThisType(Context)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 596 | BaseObjectIsPointer = true; |
| 597 | } |
| 598 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 599 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 600 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 601 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 602 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 606 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 607 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // Build the implicit member references to the field of the |
| 611 | // anonymous struct/union. |
| 612 | Expr *Result = BaseObjectExpr; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 613 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 614 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 615 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 616 | FI != FIEnd; ++FI) { |
| 617 | QualType MemberType = (*FI)->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 618 | Qualifiers MemberTypeQuals = |
| 619 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 620 | |
| 621 | // CVR attributes from the base are picked up by members, |
| 622 | // except that 'mutable' members don't pick up 'const'. |
| 623 | if ((*FI)->isMutable()) |
| 624 | ResultQuals.removeConst(); |
| 625 | |
| 626 | // GC attributes are never picked up by members. |
| 627 | ResultQuals.removeObjCGCAttr(); |
| 628 | |
| 629 | // TR 18037 does not allow fields to be declared with address spaces. |
| 630 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 631 | |
| 632 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 633 | if (NewQuals != MemberTypeQuals) |
| 634 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 635 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 636 | MarkDeclarationReferenced(Loc, *FI); |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 637 | PerformObjectMemberConversion(Result, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 638 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 639 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 640 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 641 | BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 642 | ResultQuals = NewQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 645 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 646 | } |
| 647 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 648 | /// Decomposes the given name into a DeclarationName, its location, and |
| 649 | /// possibly a list of template arguments. |
| 650 | /// |
| 651 | /// If this produces template arguments, it is permitted to call |
| 652 | /// DecomposeTemplateName. |
| 653 | /// |
| 654 | /// This actually loses a lot of source location information for |
| 655 | /// non-standard name kinds; we should consider preserving that in |
| 656 | /// some way. |
| 657 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 658 | const UnqualifiedId &Id, |
| 659 | TemplateArgumentListInfo &Buffer, |
| 660 | DeclarationName &Name, |
| 661 | SourceLocation &NameLoc, |
| 662 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 663 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 664 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 665 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 666 | |
| 667 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 668 | Id.TemplateId->getTemplateArgs(), |
| 669 | Id.TemplateId->NumArgs); |
| 670 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 671 | TemplateArgsPtr.release(); |
| 672 | |
| 673 | TemplateName TName = |
| 674 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 675 | |
| 676 | Name = SemaRef.Context.getNameForTemplate(TName); |
| 677 | NameLoc = Id.TemplateId->TemplateNameLoc; |
| 678 | TemplateArgs = &Buffer; |
| 679 | } else { |
| 680 | Name = SemaRef.GetNameFromUnqualifiedId(Id); |
| 681 | NameLoc = Id.StartLocation; |
| 682 | TemplateArgs = 0; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// Decompose the given template name into a list of lookup results. |
| 687 | /// |
| 688 | /// The unqualified ID must name a non-dependent template, which can |
| 689 | /// be more easily tested by checking whether DecomposeUnqualifiedId |
| 690 | /// found template arguments. |
| 691 | static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) { |
| 692 | assert(Id.getKind() == UnqualifiedId::IK_TemplateId); |
| 693 | TemplateName TName = |
| 694 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 695 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 696 | if (TemplateDecl *TD = TName.getAsTemplateDecl()) |
| 697 | R.addDecl(TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 698 | else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate()) |
| 699 | for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end(); |
| 700 | I != E; ++I) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 701 | R.addDecl(*I); |
John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 702 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 703 | R.resolveKind(); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 704 | } |
| 705 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 706 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
| 707 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 708 | E = Record->bases_end(); I != E; ++I) { |
| 709 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 710 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 711 | if (!BaseRT) return false; |
| 712 | |
| 713 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 714 | if (!BaseRecord->isDefinition() || |
| 715 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | return true; |
| 720 | } |
| 721 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 722 | /// Determines whether we can lookup this id-expression now or whether |
| 723 | /// we have to wait until template instantiation is complete. |
| 724 | static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 725 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 726 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 727 | // If the qualifier scope isn't computable, it's definitely dependent. |
| 728 | if (!DC) return true; |
| 729 | |
| 730 | // If the qualifier scope doesn't name a record, we can always look into it. |
| 731 | if (!isa<CXXRecordDecl>(DC)) return false; |
| 732 | |
| 733 | // We can't look into record types unless they're fully-formed. |
| 734 | if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true; |
| 735 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 738 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 739 | /// Determines if the given class is provably not derived from all of |
| 740 | /// the prospective base classes. |
| 741 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 742 | CXXRecordDecl *Record, |
| 743 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 744 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 745 | return false; |
| 746 | |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 747 | RecordDecl *RD = Record->getDefinition(SemaRef.Context); |
| 748 | if (!RD) return false; |
| 749 | Record = cast<CXXRecordDecl>(RD); |
| 750 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 751 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 752 | E = Record->bases_end(); I != E; ++I) { |
| 753 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 754 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 755 | if (!BaseRT) return false; |
| 756 | |
| 757 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 758 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | return true; |
| 763 | } |
| 764 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 765 | /// Determines if this is an instance member of a class. |
| 766 | static bool IsInstanceMember(NamedDecl *D) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 767 | assert(D->isCXXClassMember() && |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 768 | "checking whether non-member is instance member"); |
| 769 | |
| 770 | if (isa<FieldDecl>(D)) return true; |
| 771 | |
| 772 | if (isa<CXXMethodDecl>(D)) |
| 773 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 774 | |
| 775 | if (isa<FunctionTemplateDecl>(D)) { |
| 776 | D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 777 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 778 | } |
| 779 | |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | enum IMAKind { |
| 784 | /// The reference is definitely not an instance member access. |
| 785 | IMA_Static, |
| 786 | |
| 787 | /// The reference may be an implicit instance member access. |
| 788 | IMA_Mixed, |
| 789 | |
| 790 | /// The reference may be to an instance member, but it is invalid if |
| 791 | /// so, because the context is not an instance method. |
| 792 | IMA_Mixed_StaticContext, |
| 793 | |
| 794 | /// The reference may be to an instance member, but it is invalid if |
| 795 | /// so, because the context is from an unrelated class. |
| 796 | IMA_Mixed_Unrelated, |
| 797 | |
| 798 | /// The reference is definitely an implicit instance member access. |
| 799 | IMA_Instance, |
| 800 | |
| 801 | /// The reference may be to an unresolved using declaration. |
| 802 | IMA_Unresolved, |
| 803 | |
| 804 | /// The reference may be to an unresolved using declaration and the |
| 805 | /// context is not an instance method. |
| 806 | IMA_Unresolved_StaticContext, |
| 807 | |
| 808 | /// The reference is to a member of an anonymous structure in a |
| 809 | /// non-class context. |
| 810 | IMA_AnonymousMember, |
| 811 | |
| 812 | /// All possible referrents are instance members and the current |
| 813 | /// context is not an instance method. |
| 814 | IMA_Error_StaticContext, |
| 815 | |
| 816 | /// All possible referrents are instance members of an unrelated |
| 817 | /// class. |
| 818 | IMA_Error_Unrelated |
| 819 | }; |
| 820 | |
| 821 | /// The given lookup names class member(s) and is not being used for |
| 822 | /// an address-of-member expression. Classify the type of access |
| 823 | /// according to whether it's possible that this reference names an |
| 824 | /// instance member. This is best-effort; it is okay to |
| 825 | /// conservatively answer "yes", in which case some errors will simply |
| 826 | /// not be caught until template-instantiation. |
| 827 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 828 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 829 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 830 | |
| 831 | bool isStaticContext = |
| 832 | (!isa<CXXMethodDecl>(SemaRef.CurContext) || |
| 833 | cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic()); |
| 834 | |
| 835 | if (R.isUnresolvableResult()) |
| 836 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 837 | |
| 838 | // Collect all the declaring classes of instance members we find. |
| 839 | bool hasNonInstance = false; |
| 840 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 841 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 842 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 843 | if (IsInstanceMember(D)) { |
| 844 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 845 | |
| 846 | // If this is a member of an anonymous record, move out to the |
| 847 | // innermost non-anonymous struct or union. If there isn't one, |
| 848 | // that's a special case. |
| 849 | while (R->isAnonymousStructOrUnion()) { |
| 850 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 851 | if (!R) return IMA_AnonymousMember; |
| 852 | } |
| 853 | Classes.insert(R->getCanonicalDecl()); |
| 854 | } |
| 855 | else |
| 856 | hasNonInstance = true; |
| 857 | } |
| 858 | |
| 859 | // If we didn't find any instance members, it can't be an implicit |
| 860 | // member reference. |
| 861 | if (Classes.empty()) |
| 862 | return IMA_Static; |
| 863 | |
| 864 | // If the current context is not an instance method, it can't be |
| 865 | // an implicit member reference. |
| 866 | if (isStaticContext) |
| 867 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 868 | |
| 869 | // If we can prove that the current context is unrelated to all the |
| 870 | // declaring classes, it can't be an implicit member reference (in |
| 871 | // which case it's an error if any of those members are selected). |
| 872 | if (IsProvablyNotDerivedFrom(SemaRef, |
| 873 | cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(), |
| 874 | Classes)) |
| 875 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 876 | |
| 877 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 878 | } |
| 879 | |
| 880 | /// Diagnose a reference to a field with no object available. |
| 881 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 882 | const CXXScopeSpec &SS, |
| 883 | const LookupResult &R) { |
| 884 | SourceLocation Loc = R.getNameLoc(); |
| 885 | SourceRange Range(Loc); |
| 886 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 887 | |
| 888 | if (R.getAsSingle<FieldDecl>()) { |
| 889 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 890 | if (MD->isStatic()) { |
| 891 | // "invalid use of member 'x' in static member function" |
| 892 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 893 | << Range << R.getLookupName(); |
| 894 | return; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 899 | << R.getLookupName() << Range; |
| 900 | return; |
| 901 | } |
| 902 | |
| 903 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 904 | } |
| 905 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 906 | /// Diagnose an empty lookup. |
| 907 | /// |
| 908 | /// \return false if new lookup candidates were found |
| 909 | bool Sema::DiagnoseEmptyLookup(const CXXScopeSpec &SS, |
| 910 | LookupResult &R) { |
| 911 | DeclarationName Name = R.getLookupName(); |
| 912 | |
| 913 | // We don't know how to recover from bad qualified lookups. |
| 914 | if (!SS.isEmpty()) { |
| 915 | Diag(R.getNameLoc(), diag::err_no_member) |
| 916 | << Name << computeDeclContext(SS, false) |
| 917 | << SS.getRange(); |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | unsigned diagnostic = diag::err_undeclared_var_use; |
| 922 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 923 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
| 924 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
| 925 | diagnostic = diag::err_undeclared_use; |
| 926 | |
| 927 | // Fake an unqualified lookup. This is useful when (for example) |
| 928 | // the original lookup would not have found something because it was |
| 929 | // a dependent name. |
| 930 | for (DeclContext *DC = CurContext; DC; DC = DC->getParent()) { |
| 931 | if (isa<CXXRecordDecl>(DC)) { |
| 932 | LookupQualifiedName(R, DC); |
| 933 | |
| 934 | if (!R.empty()) { |
| 935 | // Don't give errors about ambiguities in this lookup. |
| 936 | R.suppressDiagnostics(); |
| 937 | |
| 938 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 939 | bool isInstance = CurMethod && |
| 940 | CurMethod->isInstance() && |
| 941 | DC == CurMethod->getParent(); |
| 942 | |
| 943 | // Give a code modification hint to insert 'this->'. |
| 944 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 945 | // Actually quite difficult! |
| 946 | if (isInstance) |
| 947 | Diag(R.getNameLoc(), diagnostic) << Name |
| 948 | << CodeModificationHint::CreateInsertion(R.getNameLoc(), |
| 949 | "this->"); |
| 950 | else |
| 951 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 952 | |
| 953 | // Do we really want to note all of these? |
| 954 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 955 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 956 | |
| 957 | // Tell the callee to try to recover. |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // Give up, we can't recover. |
| 964 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 965 | return true; |
| 966 | } |
| 967 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 968 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 969 | const CXXScopeSpec &SS, |
| 970 | UnqualifiedId &Id, |
| 971 | bool HasTrailingLParen, |
| 972 | bool isAddressOfOperand) { |
| 973 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 974 | "cannot be direct & operand and have a trailing lparen"); |
| 975 | |
| 976 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 977 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 978 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 979 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 980 | |
| 981 | // Decompose the UnqualifiedId into the following data. |
| 982 | DeclarationName Name; |
| 983 | SourceLocation NameLoc; |
| 984 | const TemplateArgumentListInfo *TemplateArgs; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 985 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 986 | Name, NameLoc, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 988 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 989 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 990 | // C++ [temp.dep.expr]p3: |
| 991 | // An id-expression is type-dependent if it contains: |
| 992 | // -- a nested-name-specifier that contains a class-name that |
| 993 | // names a dependent type. |
| 994 | // Determine whether this is a member of an unknown specialization; |
| 995 | // we need to handle these differently. |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 996 | if (SS.isSet() && IsDependentIdExpression(*this, SS)) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 997 | return ActOnDependentIdExpression(SS, Name, NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 998 | isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 999 | TemplateArgs); |
| 1000 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1001 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1002 | // Perform the required lookup. |
| 1003 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1004 | if (TemplateArgs) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1005 | // Just re-use the lookup done by isTemplateName. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1006 | DecomposeTemplateName(R, Id); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1007 | } else { |
| 1008 | LookupParsedName(R, S, &SS, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1010 | // If this reference is in an Objective-C method, then we need to do |
| 1011 | // some special Objective-C lookup, too. |
| 1012 | if (!SS.isSet() && II && getCurMethodDecl()) { |
| 1013 | OwningExprResult E(LookupInObjCMethod(R, S, II)); |
| 1014 | if (E.isInvalid()) |
| 1015 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1017 | Expr *Ex = E.takeAs<Expr>(); |
| 1018 | if (Ex) return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1019 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1020 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1021 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1022 | if (R.isAmbiguous()) |
| 1023 | return ExprError(); |
| 1024 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1025 | // Determine whether this name might be a candidate for |
| 1026 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1027 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1028 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1029 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1030 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1031 | // in C90, extension in C99, forbidden in C++). |
| 1032 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1033 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1034 | if (D) R.addDecl(D); |
| 1035 | } |
| 1036 | |
| 1037 | // If this name wasn't predeclared and if this is not a function |
| 1038 | // call, diagnose the problem. |
| 1039 | if (R.empty()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1040 | if (DiagnoseEmptyLookup(SS, R)) |
| 1041 | return ExprError(); |
| 1042 | |
| 1043 | assert(!R.empty() && |
| 1044 | "DiagnoseEmptyLookup returned false but added no results"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1048 | // This is guaranteed from this point on. |
| 1049 | assert(!R.empty() || ADL); |
| 1050 | |
| 1051 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1052 | // Warn about constructs like: |
| 1053 | // if (void *X = foo()) { ... } else { X }. |
| 1054 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1056 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1057 | Scope *CheckS = S; |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1058 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1060 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1061 | ExprError(Diag(NameLoc, diag::warn_value_always_zero) |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1062 | << Var->getDeclName() |
| 1063 | << (Var->getType()->isPointerType()? 2 : |
| 1064 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1068 | // Move to the parent of this scope. |
| 1069 | CheckS = CheckS->getParent(); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1072 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1073 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1074 | // C99 DR 316 says that, if a function type comes from a |
| 1075 | // function definition (without a prototype), that type is only |
| 1076 | // used for checking compatibility. Therefore, when referencing |
| 1077 | // the function, we pretend that we don't have the full function |
| 1078 | // type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1079 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1080 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1082 | QualType T = Func->getType(); |
| 1083 | QualType NoProtoType = T; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1084 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1085 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1086 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1090 | // Check whether this might be a C++ implicit instance member access. |
| 1091 | // C++ [expr.prim.general]p6: |
| 1092 | // Within the definition of a non-static member function, an |
| 1093 | // identifier that names a non-static member is transformed to a |
| 1094 | // class member access expression. |
| 1095 | // But note that &SomeClass::foo is grammatically distinct, even |
| 1096 | // though we don't parse it that way. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1097 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1098 | bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1099 | if (!isAbstractMemberPointer) |
| 1100 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1103 | if (TemplateArgs) |
| 1104 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1105 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1106 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1107 | } |
| 1108 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1109 | /// Builds an expression which might be an implicit member expression. |
| 1110 | Sema::OwningExprResult |
| 1111 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1112 | LookupResult &R, |
| 1113 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1114 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1115 | case IMA_Instance: |
| 1116 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1117 | |
| 1118 | case IMA_AnonymousMember: |
| 1119 | assert(R.isSingleResult()); |
| 1120 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1121 | R.getAsSingle<FieldDecl>()); |
| 1122 | |
| 1123 | case IMA_Mixed: |
| 1124 | case IMA_Mixed_Unrelated: |
| 1125 | case IMA_Unresolved: |
| 1126 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1127 | |
| 1128 | case IMA_Static: |
| 1129 | case IMA_Mixed_StaticContext: |
| 1130 | case IMA_Unresolved_StaticContext: |
| 1131 | if (TemplateArgs) |
| 1132 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1133 | return BuildDeclarationNameExpr(SS, R, false); |
| 1134 | |
| 1135 | case IMA_Error_StaticContext: |
| 1136 | case IMA_Error_Unrelated: |
| 1137 | DiagnoseInstanceReference(*this, SS, R); |
| 1138 | return ExprError(); |
| 1139 | } |
| 1140 | |
| 1141 | llvm_unreachable("unexpected instance member access kind"); |
| 1142 | return ExprError(); |
| 1143 | } |
| 1144 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1145 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1146 | /// declaration name, generally during template instantiation. |
| 1147 | /// There's a large number of things which don't need to be done along |
| 1148 | /// this path. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1149 | Sema::OwningExprResult |
| 1150 | Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS, |
| 1151 | DeclarationName Name, |
| 1152 | SourceLocation NameLoc) { |
| 1153 | DeclContext *DC; |
| 1154 | if (!(DC = computeDeclContext(SS, false)) || |
| 1155 | DC->isDependentContext() || |
| 1156 | RequireCompleteDeclContext(SS)) |
| 1157 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0); |
| 1158 | |
| 1159 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1160 | LookupQualifiedName(R, DC); |
| 1161 | |
| 1162 | if (R.isAmbiguous()) |
| 1163 | return ExprError(); |
| 1164 | |
| 1165 | if (R.empty()) { |
| 1166 | Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange(); |
| 1167 | return ExprError(); |
| 1168 | } |
| 1169 | |
| 1170 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1171 | } |
| 1172 | |
| 1173 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1174 | /// detected that we're currently inside an ObjC method. Perform some |
| 1175 | /// additional lookup. |
| 1176 | /// |
| 1177 | /// Ideally, most of this would be done by lookup, but there's |
| 1178 | /// actually quite a lot of extra work involved. |
| 1179 | /// |
| 1180 | /// Returns a null sentinel to indicate trivial success. |
| 1181 | Sema::OwningExprResult |
| 1182 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
| 1183 | IdentifierInfo *II) { |
| 1184 | SourceLocation Loc = Lookup.getNameLoc(); |
| 1185 | |
| 1186 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1187 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1188 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1189 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1190 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1191 | |
| 1192 | // If we're in a class method, we don't normally want to look for |
| 1193 | // ivars. But if we don't find anything else, and there's an |
| 1194 | // ivar, that's an error. |
| 1195 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 1196 | |
| 1197 | bool LookForIvars; |
| 1198 | if (Lookup.empty()) |
| 1199 | LookForIvars = true; |
| 1200 | else if (IsClassMethod) |
| 1201 | LookForIvars = false; |
| 1202 | else |
| 1203 | LookForIvars = (Lookup.isSingleResult() && |
| 1204 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1205 | |
| 1206 | if (LookForIvars) { |
| 1207 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1208 | ObjCInterfaceDecl *ClassDeclared; |
| 1209 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1210 | // Diagnose using an ivar in a class method. |
| 1211 | if (IsClassMethod) |
| 1212 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1213 | << IV->getDeclName()); |
| 1214 | |
| 1215 | // If we're referencing an invalid decl, just return this as a silent |
| 1216 | // error node. The error diagnostic was already emitted on the decl. |
| 1217 | if (IV->isInvalidDecl()) |
| 1218 | return ExprError(); |
| 1219 | |
| 1220 | // Check if referencing a field with __attribute__((deprecated)). |
| 1221 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1222 | return ExprError(); |
| 1223 | |
| 1224 | // Diagnose the use of an ivar outside of the declaring class. |
| 1225 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1226 | ClassDeclared != IFace) |
| 1227 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1228 | |
| 1229 | // FIXME: This should use a new expr for a direct reference, don't |
| 1230 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1231 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1232 | UnqualifiedId SelfName; |
| 1233 | SelfName.setIdentifier(&II, SourceLocation()); |
| 1234 | CXXScopeSpec SelfScopeSpec; |
| 1235 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 1236 | SelfName, false, false); |
| 1237 | MarkDeclarationReferenced(Loc, IV); |
| 1238 | return Owned(new (Context) |
| 1239 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1240 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1241 | } |
| 1242 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
| 1243 | // We should warn if a local variable hides an ivar. |
| 1244 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1245 | ObjCInterfaceDecl *ClassDeclared; |
| 1246 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1247 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1248 | IFace == ClassDeclared) |
| 1249 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | // Needed to implement property "super.method" notation. |
| 1254 | if (Lookup.empty() && II->isStr("super")) { |
| 1255 | QualType T; |
| 1256 | |
| 1257 | if (getCurMethodDecl()->isInstanceMethod()) |
| 1258 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 1259 | getCurMethodDecl()->getClassInterface())); |
| 1260 | else |
| 1261 | T = Context.getObjCClassType(); |
| 1262 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
| 1263 | } |
| 1264 | |
| 1265 | // Sentinel value saying that we didn't do anything special. |
| 1266 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1267 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1268 | |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1269 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1270 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1271 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 1272 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1274 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1276 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1277 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 1278 | return false; |
| 1279 | QualType FromRecordType = From->getType(); |
| 1280 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1281 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1282 | DestType = Context.getPointerType(DestType); |
| 1283 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1284 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1285 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 1286 | CheckDerivedToBaseConversion(FromRecordType, |
| 1287 | DestRecordType, |
| 1288 | From->getSourceRange().getBegin(), |
| 1289 | From->getSourceRange())) |
| 1290 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 1291 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 1292 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1293 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1294 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1295 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1297 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1299 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1300 | SourceLocation Loc, QualType Ty, |
| 1301 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1302 | NestedNameSpecifier *Qualifier = 0; |
| 1303 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1304 | if (SS.isSet()) { |
| 1305 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1306 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1309 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
| 1310 | Member, Loc, TemplateArgs, Ty); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1313 | /// Builds an implicit member access expression. The current context |
| 1314 | /// is known to be an instance method, and the given unqualified lookup |
| 1315 | /// set is known to contain only instance members, at least one of which |
| 1316 | /// is from an appropriate type. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1317 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1318 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1319 | LookupResult &R, |
| 1320 | const TemplateArgumentListInfo *TemplateArgs, |
| 1321 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1322 | assert(!R.empty() && !R.isAmbiguous()); |
| 1323 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1324 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1325 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1326 | // We may have found a field within an anonymous union or struct |
| 1327 | // (C++ [class.union]). |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1328 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1329 | // FIXME: template-ids inside anonymous structs? |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1330 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1331 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1332 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1333 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1334 | // If this is known to be an instance access, go ahead and build a |
| 1335 | // 'this' expression now. |
| 1336 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 1337 | Expr *This = 0; // null signifies implicit access |
| 1338 | if (IsKnownInstance) { |
| 1339 | This = new (Context) CXXThisExpr(SourceLocation(), ThisType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1342 | return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType, |
| 1343 | /*OpLoc*/ SourceLocation(), |
| 1344 | /*IsArrow*/ true, |
| 1345 | SS, R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1348 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1349 | const LookupResult &R, |
| 1350 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1351 | // Only when used directly as the postfix-expression of a call. |
| 1352 | if (!HasTrailingLParen) |
| 1353 | return false; |
| 1354 | |
| 1355 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1356 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1357 | return false; |
| 1358 | |
| 1359 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1360 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1361 | return false; |
| 1362 | |
| 1363 | // Turn off ADL when we find certain kinds of declarations during |
| 1364 | // normal lookup: |
| 1365 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1366 | NamedDecl *D = *I; |
| 1367 | |
| 1368 | // C++0x [basic.lookup.argdep]p3: |
| 1369 | // -- a declaration of a class member |
| 1370 | // Since using decls preserve this property, we check this on the |
| 1371 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1372 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1373 | return false; |
| 1374 | |
| 1375 | // C++0x [basic.lookup.argdep]p3: |
| 1376 | // -- a block-scope function declaration that is not a |
| 1377 | // using-declaration |
| 1378 | // NOTE: we also trigger this for function templates (in fact, we |
| 1379 | // don't check the decl type at all, since all other decl types |
| 1380 | // turn off ADL anyway). |
| 1381 | if (isa<UsingShadowDecl>(D)) |
| 1382 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1383 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1384 | return false; |
| 1385 | |
| 1386 | // C++0x [basic.lookup.argdep]p3: |
| 1387 | // -- a declaration that is neither a function or a function |
| 1388 | // template |
| 1389 | // And also for builtin functions. |
| 1390 | if (isa<FunctionDecl>(D)) { |
| 1391 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1392 | |
| 1393 | // But also builtin functions. |
| 1394 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1395 | return false; |
| 1396 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | return true; |
| 1401 | } |
| 1402 | |
| 1403 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1404 | /// Diagnoses obvious problems with the use of the given declaration |
| 1405 | /// as an expression. This is only actually called for lookups that |
| 1406 | /// were not overloaded, and it doesn't promise that the declaration |
| 1407 | /// will in fact be used. |
| 1408 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1409 | if (isa<TypedefDecl>(D)) { |
| 1410 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1411 | return true; |
| 1412 | } |
| 1413 | |
| 1414 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1415 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1416 | return true; |
| 1417 | } |
| 1418 | |
| 1419 | if (isa<NamespaceDecl>(D)) { |
| 1420 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
| 1424 | return false; |
| 1425 | } |
| 1426 | |
| 1427 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1428 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1429 | LookupResult &R, |
| 1430 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1431 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1432 | // just build an ordinary singleton decl ref. |
| 1433 | if (!NeedsADL && R.isSingleResult()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1434 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1435 | |
| 1436 | // We only need to check the declaration if there's exactly one |
| 1437 | // result, because in the overloaded case the results can only be |
| 1438 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1439 | if (R.isSingleResult() && |
| 1440 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1441 | return ExprError(); |
| 1442 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1443 | bool Dependent |
| 1444 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1445 | UnresolvedLookupExpr *ULE |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1446 | = UnresolvedLookupExpr::Create(Context, Dependent, |
| 1447 | (NestedNameSpecifier*) SS.getScopeRep(), |
| 1448 | SS.getRange(), |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1449 | R.getLookupName(), R.getNameLoc(), |
| 1450 | NeedsADL, R.isOverloadedResult()); |
| 1451 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1452 | ULE->addDecl(*I); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1453 | |
| 1454 | return Owned(ULE); |
| 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1459 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1460 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1461 | SourceLocation Loc, NamedDecl *D) { |
| 1462 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1463 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1464 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1465 | DeclarationName Name = D->getDeclName(); |
| 1466 | |
| 1467 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1468 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1469 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1470 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1471 | // Specifically diagnose references to class templates that are missing |
| 1472 | // a template argument list. |
| 1473 | Diag(Loc, diag::err_template_decl_ref) |
| 1474 | << Template << SS.getRange(); |
| 1475 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1476 | return ExprError(); |
| 1477 | } |
| 1478 | |
| 1479 | // Make sure that we're referring to a value. |
| 1480 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1481 | if (!VD) { |
| 1482 | Diag(Loc, diag::err_ref_non_value) |
| 1483 | << D << SS.getRange(); |
Daniel Dunbar | ec3455f | 2009-12-15 04:24:24 +0000 | [diff] [blame] | 1484 | Diag(D->getLocation(), diag::note_previous_declaration); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1485 | return ExprError(); |
| 1486 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1488 | // Check whether this declaration can be used. Note that we suppress |
| 1489 | // this check when we're going to perform argument-dependent lookup |
| 1490 | // on this function name, because this might not be the function |
| 1491 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1492 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1493 | return ExprError(); |
| 1494 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1495 | // Only create DeclRefExpr's for valid Decl's. |
| 1496 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1497 | return ExprError(); |
| 1498 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1499 | // If the identifier reference is inside a block, and it refers to a value |
| 1500 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1501 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1502 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1503 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1504 | // We do not do this for things like enum constants, global variables, etc, |
| 1505 | // as they do not get snapshotted. |
| 1506 | // |
| 1507 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1508 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1509 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1510 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1511 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1512 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1513 | // This is to record that a 'const' was actually synthesize and added. |
| 1514 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1515 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1517 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1519 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1520 | } |
| 1521 | // If this reference is not in a block or if the referenced variable is |
| 1522 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1523 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1524 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1527 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1528 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1529 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1530 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1532 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1533 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1534 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1535 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1538 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1539 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1541 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1542 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1543 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1544 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1545 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1546 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1547 | QualType ResTy; |
| 1548 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1549 | ResTy = Context.DependentTy; |
| 1550 | } else { |
| 1551 | unsigned Length = |
| 1552 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1553 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1554 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1555 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1556 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1557 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1558 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1561 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1562 | llvm::SmallString<16> CharBuffer; |
| 1563 | CharBuffer.resize(Tok.getLength()); |
| 1564 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1565 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1566 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1567 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1568 | Tok.getLocation(), PP); |
| 1569 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1570 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1571 | |
| 1572 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1573 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1574 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1575 | Literal.isWide(), |
| 1576 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1579 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1580 | // 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] | 1581 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1582 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1583 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1584 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1585 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1586 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1588 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1589 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1590 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1591 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1592 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1595 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1596 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1598 | Tok.getLocation(), PP); |
| 1599 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1600 | return ExprError(); |
| 1601 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1602 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1603 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1604 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1605 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1606 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1607 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1608 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1609 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1610 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1611 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1612 | |
| 1613 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1614 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1615 | // isExact will be set by GetFloatValue(). |
| 1616 | bool isExact = false; |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1617 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1618 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1619 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1620 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1621 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1622 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1623 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1624 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1625 | // long long is a C99 feature. |
| 1626 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1627 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1628 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1629 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1631 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1632 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1633 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1634 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1635 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1636 | Ty = Context.UnsignedLongLongTy; |
| 1637 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1638 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1639 | } else { |
| 1640 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1641 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1644 | // be an unsigned int. |
| 1645 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1646 | |
| 1647 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1648 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1649 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1650 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1651 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | // Does it fit in a unsigned int? |
| 1654 | if (ResultVal.isIntN(IntSize)) { |
| 1655 | // Does it fit in a signed int? |
| 1656 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1657 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1658 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1659 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1660 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1662 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1663 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1664 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1665 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1666 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | // Does it fit in a unsigned long? |
| 1669 | if (ResultVal.isIntN(LongSize)) { |
| 1670 | // Does it fit in a signed long? |
| 1671 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1672 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1673 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1674 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1675 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1676 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1679 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1680 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1681 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1682 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1683 | // Does it fit in a unsigned long long? |
| 1684 | if (ResultVal.isIntN(LongLongSize)) { |
| 1685 | // Does it fit in a signed long long? |
| 1686 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1687 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1688 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1689 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1690 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1693 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1694 | // If we still couldn't decide a type, we probably have something that |
| 1695 | // 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] | 1696 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1698 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1699 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1702 | if (ResultVal.getBitWidth() != Width) |
| 1703 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1704 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1705 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1706 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1708 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1709 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1711 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1712 | |
| 1713 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1716 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1717 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1718 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1719 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1720 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1724 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1725 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1726 | SourceLocation OpLoc, |
| 1727 | const SourceRange &ExprRange, |
| 1728 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1729 | if (exprType->isDependentType()) |
| 1730 | return false; |
| 1731 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1732 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1733 | // the result is the size of the referenced type." |
| 1734 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1735 | // result shall be the alignment of the referenced type." |
| 1736 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1737 | exprType = Ref->getPointeeType(); |
| 1738 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1740 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1741 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1742 | if (isSizeof) |
| 1743 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1744 | return false; |
| 1745 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1747 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1748 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1749 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1750 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1754 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 1755 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 1756 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1757 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1759 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1760 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1761 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1762 | << exprType << isSizeof << ExprRange; |
| 1763 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1766 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1769 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1770 | const SourceRange &ExprRange) { |
| 1771 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1772 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1774 | if (isa<DeclRefExpr>(E)) |
| 1775 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Cannot know anything else if the expression is dependent. |
| 1778 | if (E->isTypeDependent()) |
| 1779 | return false; |
| 1780 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1781 | if (E->getBitField()) { |
| 1782 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1783 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1784 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Alignment of a field access is always okay, so long as it isn't a |
| 1787 | // bit-field. |
| 1788 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1789 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1790 | return false; |
| 1791 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1792 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1793 | } |
| 1794 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1795 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | Action::OwningExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1797 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1798 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1799 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1800 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1801 | return ExprError(); |
| 1802 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1803 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1805 | if (!T->isDependentType() && |
| 1806 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1807 | return ExprError(); |
| 1808 | |
| 1809 | // 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] | 1810 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1811 | Context.getSizeType(), OpLoc, |
| 1812 | R.getEnd())); |
| 1813 | } |
| 1814 | |
| 1815 | /// \brief Build a sizeof or alignof expression given an expression |
| 1816 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | Action::OwningExprResult |
| 1818 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1819 | bool isSizeOf, SourceRange R) { |
| 1820 | // Verify that the operand is valid. |
| 1821 | bool isInvalid = false; |
| 1822 | if (E->isTypeDependent()) { |
| 1823 | // Delay type-checking for type-dependent expressions. |
| 1824 | } else if (!isSizeOf) { |
| 1825 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1826 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1827 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1828 | isInvalid = true; |
| 1829 | } else { |
| 1830 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1831 | } |
| 1832 | |
| 1833 | if (isInvalid) |
| 1834 | return ExprError(); |
| 1835 | |
| 1836 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1837 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1838 | Context.getSizeType(), OpLoc, |
| 1839 | R.getEnd())); |
| 1840 | } |
| 1841 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1842 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1843 | /// the same for @c alignof and @c __alignof |
| 1844 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1845 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1846 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1847 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1848 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1849 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1850 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1851 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1852 | TypeSourceInfo *TInfo; |
| 1853 | (void) GetTypeFromParser(TyOrEx, &TInfo); |
| 1854 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1857 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1858 | Action::OwningExprResult Result |
| 1859 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1860 | |
| 1861 | if (Result.isInvalid()) |
| 1862 | DeleteExpr(ArgEx); |
| 1863 | |
| 1864 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1867 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1868 | if (V->isTypeDependent()) |
| 1869 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1871 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1872 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1873 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1875 | // Otherwise they pass through real integer and floating point types here. |
| 1876 | if (V->getType()->isArithmeticType()) |
| 1877 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1879 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1880 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1881 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1882 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1886 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1887 | Action::OwningExprResult |
| 1888 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1889 | tok::TokenKind Kind, ExprArg Input) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1890 | UnaryOperator::Opcode Opc; |
| 1891 | switch (Kind) { |
| 1892 | default: assert(0 && "Unknown unary op!"); |
| 1893 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1894 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1895 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1896 | |
Eli Friedman | e4216e9 | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 1897 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1900 | Action::OwningExprResult |
| 1901 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1902 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1903 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1904 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1905 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1906 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1907 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1909 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1910 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1911 | Base.release(); |
| 1912 | Idx.release(); |
| 1913 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1914 | Context.DependentTy, RLoc)); |
| 1915 | } |
| 1916 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1918 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1919 | LHSExp->getType()->isEnumeralType() || |
| 1920 | RHSExp->getType()->isRecordType() || |
| 1921 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1922 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1925 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 1926 | } |
| 1927 | |
| 1928 | |
| 1929 | Action::OwningExprResult |
| 1930 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 1931 | ExprArg Idx, SourceLocation RLoc) { |
| 1932 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 1933 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 1934 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1935 | // Perform default conversions. |
| 1936 | DefaultFunctionArrayConversion(LHSExp); |
| 1937 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1939 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1940 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1941 | // 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] | 1942 | // 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] | 1943 | // 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] | 1944 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1945 | Expr *BaseExpr, *IndexExpr; |
| 1946 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1947 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1948 | BaseExpr = LHSExp; |
| 1949 | IndexExpr = RHSExp; |
| 1950 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1951 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1952 | BaseExpr = LHSExp; |
| 1953 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1954 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1955 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1956 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1957 | BaseExpr = RHSExp; |
| 1958 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1959 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1961 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1962 | BaseExpr = LHSExp; |
| 1963 | IndexExpr = RHSExp; |
| 1964 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1966 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1967 | // Handle the uncommon case of "123[Ptr]". |
| 1968 | BaseExpr = RHSExp; |
| 1969 | IndexExpr = LHSExp; |
| 1970 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1971 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1972 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1973 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1975 | // FIXME: need to deal with const... |
| 1976 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1977 | } else if (LHSTy->isArrayType()) { |
| 1978 | // If we see an array that wasn't promoted by |
| 1979 | // DefaultFunctionArrayConversion, it must be an array that |
| 1980 | // wasn't promoted because of the C90 rule that doesn't |
| 1981 | // allow promoting non-lvalue arrays. Warn, then |
| 1982 | // force the promotion here. |
| 1983 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1984 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1985 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 1986 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1987 | LHSTy = LHSExp->getType(); |
| 1988 | |
| 1989 | BaseExpr = LHSExp; |
| 1990 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1991 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1992 | } else if (RHSTy->isArrayType()) { |
| 1993 | // Same as previous, except for 123[f().a] case |
| 1994 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1995 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1996 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 1997 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1998 | RHSTy = RHSExp->getType(); |
| 1999 | |
| 2000 | BaseExpr = RHSExp; |
| 2001 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2002 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2004 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2005 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2006 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2007 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2008 | if (!(IndexExpr->getType()->isIntegerType() && |
| 2009 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2010 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2011 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2013 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2014 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2015 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2016 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2017 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2018 | // 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] | 2019 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2020 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2021 | // incomplete types are not object types. |
| 2022 | if (ResultType->isFunctionType()) { |
| 2023 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2024 | << ResultType << BaseExpr->getSourceRange(); |
| 2025 | return ExprError(); |
| 2026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2028 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2030 | PDiag(diag::err_subscript_incomplete_type) |
| 2031 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2032 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2034 | // Diagnose bad cases where we step over interface counts. |
| 2035 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 2036 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2037 | << ResultType << BaseExpr->getSourceRange(); |
| 2038 | return ExprError(); |
| 2039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2041 | Base.release(); |
| 2042 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2043 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2044 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2047 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2048 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2050 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2051 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2052 | // see FIXME there. |
| 2053 | // |
| 2054 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2055 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2056 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2057 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2058 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2059 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2060 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2061 | // 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] | 2062 | // special names that indicate a subset of exactly half the elements are |
| 2063 | // to be selected. |
| 2064 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2065 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2066 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2067 | // 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] | 2068 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2069 | |
| 2070 | // Check that we've found one of the special components, or that the component |
| 2071 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2072 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2073 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2074 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2075 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2076 | do |
| 2077 | compStr++; |
| 2078 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2079 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2080 | do |
| 2081 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2082 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2083 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2084 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2085 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2086 | // We didn't get to the end of the string. This means the component names |
| 2087 | // 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] | 2088 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 2089 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2090 | return QualType(); |
| 2091 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2092 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2093 | // Ensure no component accessor exceeds the width of the vector type it |
| 2094 | // operates on. |
| 2095 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2096 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2097 | |
| 2098 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2099 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2100 | |
| 2101 | while (*compStr) { |
| 2102 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2103 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2104 | << baseType << SourceRange(CompLoc); |
| 2105 | return QualType(); |
| 2106 | } |
| 2107 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2108 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2109 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2110 | // 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] | 2111 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2112 | // 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] | 2113 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2114 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2115 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2116 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2117 | if (HexSwizzle) |
| 2118 | CompSize--; |
| 2119 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2120 | if (CompSize == 1) |
| 2121 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2122 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2123 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2124 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2125 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2126 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2127 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2128 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2129 | } |
| 2130 | 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] | 2131 | } |
| 2132 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2133 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2134 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2135 | const Selector &Sel, |
| 2136 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2138 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2139 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2140 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2141 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2143 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2144 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2146 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2147 | return D; |
| 2148 | } |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2152 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2153 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2154 | const Selector &Sel, |
| 2155 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2156 | // Check protocols on qualified interfaces. |
| 2157 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2158 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2159 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2160 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2161 | GDecl = PD; |
| 2162 | break; |
| 2163 | } |
| 2164 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2165 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2166 | GDecl = OMD; |
| 2167 | break; |
| 2168 | } |
| 2169 | } |
| 2170 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2171 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2172 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2173 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2174 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2175 | if (GDecl) |
| 2176 | return GDecl; |
| 2177 | } |
| 2178 | } |
| 2179 | return GDecl; |
| 2180 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2181 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2182 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2183 | Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType, |
| 2184 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2185 | const CXXScopeSpec &SS, |
| 2186 | NamedDecl *FirstQualifierInScope, |
| 2187 | DeclarationName Name, SourceLocation NameLoc, |
| 2188 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2189 | Expr *BaseExpr = Base.takeAs<Expr>(); |
| 2190 | |
| 2191 | // Even in dependent contexts, try to diagnose base expressions with |
| 2192 | // obviously wrong types, e.g.: |
| 2193 | // |
| 2194 | // T* t; |
| 2195 | // t.f; |
| 2196 | // |
| 2197 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2198 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2199 | // allows this, while still reporting an error if T is a struct pointer. |
| 2200 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2201 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2202 | if (PT && (!getLangOptions().ObjC1 || |
| 2203 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2204 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2205 | Diag(NameLoc, diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2206 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2207 | return ExprError(); |
| 2208 | } |
| 2209 | } |
| 2210 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2211 | assert(BaseType->isDependentType()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2212 | |
| 2213 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2214 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2215 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2216 | IsArrow, OpLoc, |
| 2217 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 2218 | SS.getRange(), |
| 2219 | FirstQualifierInScope, |
| 2220 | Name, NameLoc, |
| 2221 | TemplateArgs)); |
| 2222 | } |
| 2223 | |
| 2224 | /// We know that the given qualified member reference points only to |
| 2225 | /// declarations which do not belong to the static type of the base |
| 2226 | /// expression. Diagnose the problem. |
| 2227 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2228 | Expr *BaseExpr, |
| 2229 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2230 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2231 | const LookupResult &R) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2232 | // If this is an implicit member access, use a different set of |
| 2233 | // diagnostics. |
| 2234 | if (!BaseExpr) |
| 2235 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2236 | |
| 2237 | // FIXME: this is an exceedingly lame diagnostic for some of the more |
| 2238 | // complicated cases here. |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2239 | DeclContext *DC = R.getRepresentativeDecl()->getDeclContext(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2240 | SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual) |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2241 | << SS.getRange() << DC << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | // Check whether the declarations we found through a nested-name |
| 2245 | // specifier in a member expression are actually members of the base |
| 2246 | // type. The restriction here is: |
| 2247 | // |
| 2248 | // C++ [expr.ref]p2: |
| 2249 | // ... In these cases, the id-expression shall name a |
| 2250 | // member of the class or of one of its base classes. |
| 2251 | // |
| 2252 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2253 | // an unrelated class, and for us to find an overload set including |
| 2254 | // decls from classes which are not superclasses, as long as the decl |
| 2255 | // we actually pick through overload resolution is from a superclass. |
| 2256 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2257 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2258 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2259 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2260 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2261 | if (!BaseRT) { |
| 2262 | // We can't check this yet because the base type is still |
| 2263 | // dependent. |
| 2264 | assert(BaseType->isDependentType()); |
| 2265 | return false; |
| 2266 | } |
| 2267 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2268 | |
| 2269 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2270 | // If this is an implicit member reference and we find a |
| 2271 | // non-instance member, it's not an error. |
| 2272 | if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl())) |
| 2273 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2274 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2275 | // Note that we use the DC of the decl, not the underlying decl. |
| 2276 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext()); |
| 2277 | while (RecordD->isAnonymousStructOrUnion()) |
| 2278 | RecordD = cast<CXXRecordDecl>(RecordD->getParent()); |
| 2279 | |
| 2280 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| 2281 | MemberRecord.insert(RecordD->getCanonicalDecl()); |
| 2282 | |
| 2283 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2284 | return false; |
| 2285 | } |
| 2286 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2287 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2288 | return true; |
| 2289 | } |
| 2290 | |
| 2291 | static bool |
| 2292 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2293 | SourceRange BaseRange, const RecordType *RTy, |
| 2294 | SourceLocation OpLoc, const CXXScopeSpec &SS) { |
| 2295 | RecordDecl *RDecl = RTy->getDecl(); |
| 2296 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 2297 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2298 | << BaseRange)) |
| 2299 | return true; |
| 2300 | |
| 2301 | DeclContext *DC = RDecl; |
| 2302 | if (SS.isSet()) { |
| 2303 | // If the member name was a qualified-id, look into the |
| 2304 | // nested-name-specifier. |
| 2305 | DC = SemaRef.computeDeclContext(SS, false); |
| 2306 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2307 | if (SemaRef.RequireCompleteDeclContext(SS)) { |
| 2308 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2309 | << SS.getRange() << DC; |
| 2310 | return true; |
| 2311 | } |
| 2312 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2313 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2314 | |
| 2315 | if (!isa<TypeDecl>(DC)) { |
| 2316 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2317 | << DC << SS.getRange(); |
| 2318 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2322 | // The record definition is complete, now look up the member. |
| 2323 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2324 | |
| 2325 | return false; |
| 2326 | } |
| 2327 | |
| 2328 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2329 | Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2330 | SourceLocation OpLoc, bool IsArrow, |
| 2331 | const CXXScopeSpec &SS, |
| 2332 | NamedDecl *FirstQualifierInScope, |
| 2333 | DeclarationName Name, SourceLocation NameLoc, |
| 2334 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2335 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2336 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2337 | if (BaseType->isDependentType() || |
| 2338 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2339 | return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2340 | IsArrow, OpLoc, |
| 2341 | SS, FirstQualifierInScope, |
| 2342 | Name, NameLoc, |
| 2343 | TemplateArgs); |
| 2344 | |
| 2345 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2346 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2347 | // Implicit member accesses. |
| 2348 | if (!Base) { |
| 2349 | QualType RecordTy = BaseType; |
| 2350 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2351 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2352 | RecordTy->getAs<RecordType>(), |
| 2353 | OpLoc, SS)) |
| 2354 | return ExprError(); |
| 2355 | |
| 2356 | // Explicit member accesses. |
| 2357 | } else { |
| 2358 | OwningExprResult Result = |
| 2359 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 2360 | SS, FirstQualifierInScope, |
| 2361 | /*ObjCImpDecl*/ DeclPtrTy()); |
| 2362 | |
| 2363 | if (Result.isInvalid()) { |
| 2364 | Owned(Base); |
| 2365 | return ExprError(); |
| 2366 | } |
| 2367 | |
| 2368 | if (Result.get()) |
| 2369 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2372 | return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType, |
| 2373 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2377 | Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType, |
| 2378 | SourceLocation OpLoc, bool IsArrow, |
| 2379 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2380 | LookupResult &R, |
| 2381 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2382 | Expr *BaseExpr = Base.takeAs<Expr>(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2383 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2384 | if (IsArrow) { |
| 2385 | assert(BaseType->isPointerType()); |
| 2386 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2387 | } |
| 2388 | |
| 2389 | NestedNameSpecifier *Qualifier = |
| 2390 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 2391 | DeclarationName MemberName = R.getLookupName(); |
| 2392 | SourceLocation MemberLoc = R.getNameLoc(); |
| 2393 | |
| 2394 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2395 | return ExprError(); |
| 2396 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2397 | if (R.empty()) { |
| 2398 | // Rederive where we looked up. |
| 2399 | DeclContext *DC = (SS.isSet() |
| 2400 | ? computeDeclContext(SS, false) |
| 2401 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2402 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2403 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2404 | << MemberName << DC |
| 2405 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2406 | return ExprError(); |
| 2407 | } |
| 2408 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2409 | // Diagnose qualified lookups that find only declarations from a |
| 2410 | // non-base type. Note that it's okay for lookup to find |
| 2411 | // declarations from a non-base type as long as those aren't the |
| 2412 | // ones picked by overload resolution. |
| 2413 | if (SS.isSet() && CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2414 | return ExprError(); |
| 2415 | |
| 2416 | // Construct an unresolved result if we in fact got an unresolved |
| 2417 | // result. |
| 2418 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2419 | bool Dependent = |
| 2420 | R.isUnresolvableResult() || |
| 2421 | UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2422 | |
| 2423 | UnresolvedMemberExpr *MemExpr |
| 2424 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2425 | R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2426 | BaseExpr, BaseExprType, |
| 2427 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2428 | Qualifier, SS.getRange(), |
| 2429 | MemberName, MemberLoc, |
| 2430 | TemplateArgs); |
| 2431 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2432 | MemExpr->addDecl(*I); |
| 2433 | |
| 2434 | return Owned(MemExpr); |
| 2435 | } |
| 2436 | |
| 2437 | assert(R.isSingleResult()); |
| 2438 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2439 | |
| 2440 | // FIXME: diagnose the presence of template arguments now. |
| 2441 | |
| 2442 | // If the decl being referenced had an error, return an error for this |
| 2443 | // sub-expr without emitting another error, in order to avoid cascading |
| 2444 | // error cases. |
| 2445 | if (MemberDecl->isInvalidDecl()) |
| 2446 | return ExprError(); |
| 2447 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2448 | // Handle the implicit-member-access case. |
| 2449 | if (!BaseExpr) { |
| 2450 | // If this is not an instance member, convert to a non-member access. |
| 2451 | if (!IsInstanceMember(MemberDecl)) |
| 2452 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl); |
| 2453 | |
| 2454 | BaseExpr = new (Context) CXXThisExpr(SourceLocation(), BaseExprType); |
| 2455 | } |
| 2456 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2457 | bool ShouldCheckUse = true; |
| 2458 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2459 | // Don't diagnose the use of a virtual member function unless it's |
| 2460 | // explicitly qualified. |
| 2461 | if (MD->isVirtual() && !SS.isSet()) |
| 2462 | ShouldCheckUse = false; |
| 2463 | } |
| 2464 | |
| 2465 | // Check the use of this member. |
| 2466 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2467 | Owned(BaseExpr); |
| 2468 | return ExprError(); |
| 2469 | } |
| 2470 | |
| 2471 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2472 | // We may have found a field within an anonymous union or struct |
| 2473 | // (C++ [class.union]). |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2474 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2475 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2476 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2477 | BaseExpr, OpLoc); |
| 2478 | |
| 2479 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2480 | QualType MemberType = FD->getType(); |
| 2481 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2482 | MemberType = Ref->getPointeeType(); |
| 2483 | else { |
| 2484 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2485 | BaseQuals.removeObjCGCAttr(); |
| 2486 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2487 | |
| 2488 | Qualifiers MemberQuals |
| 2489 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2490 | |
| 2491 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2492 | if (Combined != MemberQuals) |
| 2493 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2494 | } |
| 2495 | |
| 2496 | MarkDeclarationReferenced(MemberLoc, FD); |
| 2497 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2498 | return ExprError(); |
| 2499 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2500 | FD, MemberLoc, MemberType)); |
| 2501 | } |
| 2502 | |
| 2503 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2504 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2505 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2506 | Var, MemberLoc, |
| 2507 | Var->getType().getNonReferenceType())); |
| 2508 | } |
| 2509 | |
| 2510 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2511 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2512 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2513 | MemberFn, MemberLoc, |
| 2514 | MemberFn->getType())); |
| 2515 | } |
| 2516 | |
| 2517 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2518 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2519 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2520 | Enum, MemberLoc, Enum->getType())); |
| 2521 | } |
| 2522 | |
| 2523 | Owned(BaseExpr); |
| 2524 | |
| 2525 | if (isa<TypeDecl>(MemberDecl)) |
| 2526 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2527 | << MemberName << int(IsArrow)); |
| 2528 | |
| 2529 | // We found a declaration kind that we didn't expect. This is a |
| 2530 | // generic error message that tells the user that she can't refer |
| 2531 | // to this member with '.' or '->'. |
| 2532 | return ExprError(Diag(MemberLoc, |
| 2533 | diag::err_typecheck_member_reference_unknown) |
| 2534 | << MemberName << int(IsArrow)); |
| 2535 | } |
| 2536 | |
| 2537 | /// Look up the given member of the given non-type-dependent |
| 2538 | /// expression. This can return in one of two ways: |
| 2539 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2540 | /// assume that lookup was performed and the results written into |
| 2541 | /// the provided structure. It will take over from there. |
| 2542 | /// * Otherwise, the returned expression will be produced in place of |
| 2543 | /// an ordinary member expression. |
| 2544 | /// |
| 2545 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2546 | /// fixed for ObjC++. |
| 2547 | Sema::OwningExprResult |
| 2548 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2549 | bool &IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2550 | const CXXScopeSpec &SS, |
| 2551 | NamedDecl *FirstQualifierInScope, |
| 2552 | DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2553 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2555 | // Perform default conversions. |
| 2556 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2557 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2558 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2559 | assert(!BaseType->isDependentType()); |
| 2560 | |
| 2561 | DeclarationName MemberName = R.getLookupName(); |
| 2562 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2563 | |
| 2564 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2565 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2566 | // function. Suggest the addition of those parentheses, build the |
| 2567 | // call, and continue on. |
| 2568 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 2569 | if (const FunctionProtoType *Fun |
| 2570 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 2571 | QualType ResultTy = Fun->getResultType(); |
| 2572 | if (Fun->getNumArgs() == 0 && |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2573 | ((!IsArrow && ResultTy->isRecordType()) || |
| 2574 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2575 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 2576 | ->isRecordType()))) { |
| 2577 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2578 | Diag(Loc, diag::err_member_reference_needs_call) |
| 2579 | << QualType(Fun, 0) |
| 2580 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2581 | |
| 2582 | OwningExprResult NewBase |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2583 | = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc, |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2584 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 2585 | if (NewBase.isInvalid()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2586 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2587 | |
| 2588 | BaseExpr = NewBase.takeAs<Expr>(); |
| 2589 | DefaultFunctionArrayConversion(BaseExpr); |
| 2590 | BaseType = BaseExpr->getType(); |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2595 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2596 | // use that. |
| 2597 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2598 | if (IsArrow) { |
| 2599 | // Handle the following exceptional case PObj->isa. |
| 2600 | if (const ObjCObjectPointerType *OPT = |
| 2601 | BaseType->getAs<ObjCObjectPointerType>()) { |
| 2602 | if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && |
| 2603 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2604 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 2605 | Context.getObjCClassType())); |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2606 | } |
| 2607 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2608 | // We have an 'id' type. Rather than fall through, we check if this |
| 2609 | // is a reference to 'isa'. |
| 2610 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2611 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2612 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2613 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2614 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2615 | |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 2616 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2617 | // use that. |
| 2618 | if (Context.isObjCSelType(BaseType)) { |
| 2619 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 2620 | // is a reference to 'sel_id'. |
| 2621 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 2622 | BaseType = Context.ObjCSelRedefinitionType; |
| 2623 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
| 2624 | } |
| 2625 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2626 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2627 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2628 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2629 | // Handle properties on ObjC 'Class' types. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2630 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2631 | // Also must look for a getter name which uses property syntax. |
| 2632 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2633 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 2634 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2635 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2636 | ObjCMethodDecl *Getter; |
| 2637 | // FIXME: need to also look locally in the implementation. |
| 2638 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2639 | // Check the use of this method. |
| 2640 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2641 | return ExprError(); |
| 2642 | } |
| 2643 | // If we found a getter then this may be a valid dot-reference, we |
| 2644 | // will look for the matching setter, in case it is needed. |
| 2645 | Selector SetterSel = |
| 2646 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2647 | PP.getSelectorTable(), Member); |
| 2648 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2649 | if (!Setter) { |
| 2650 | // If this reference is in an @implementation, also check for 'private' |
| 2651 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2652 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2653 | } |
| 2654 | // Look through local category implementations associated with the class. |
| 2655 | if (!Setter) |
| 2656 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 2657 | |
| 2658 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2659 | return ExprError(); |
| 2660 | |
| 2661 | if (Getter || Setter) { |
| 2662 | QualType PType; |
| 2663 | |
| 2664 | if (Getter) |
| 2665 | PType = Getter->getResultType(); |
| 2666 | else |
| 2667 | // Get the expression type from Setter's incoming parameter. |
| 2668 | PType = (*(Setter->param_end() -1))->getType(); |
| 2669 | // FIXME: we must check that the setter has property type. |
| 2670 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 2671 | PType, |
| 2672 | Setter, MemberLoc, BaseExpr)); |
| 2673 | } |
| 2674 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2675 | << MemberName << BaseType); |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | if (BaseType->isObjCClassType() && |
| 2680 | BaseType != Context.ObjCClassRedefinitionType) { |
| 2681 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2682 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2683 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2685 | if (IsArrow) { |
| 2686 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2687 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2688 | else if (BaseType->isObjCObjectPointerType()) |
| 2689 | ; |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2690 | else if (BaseType->isRecordType()) { |
| 2691 | // Recover from arrow accesses to records, e.g.: |
| 2692 | // struct MyRecord foo; |
| 2693 | // foo->bar |
| 2694 | // This is actually well-formed in C++ if MyRecord has an |
| 2695 | // overloaded operator->, but that should have been dealt with |
| 2696 | // by now. |
| 2697 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2698 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2699 | << CodeModificationHint::CreateReplacement(OpLoc, "."); |
| 2700 | IsArrow = false; |
| 2701 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2702 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 2703 | << BaseType << BaseExpr->getSourceRange(); |
| 2704 | return ExprError(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2705 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2706 | } else { |
| 2707 | // Recover from dot accesses to pointers, e.g.: |
| 2708 | // type *foo; |
| 2709 | // foo.bar |
| 2710 | // This is actually well-formed in two cases: |
| 2711 | // - 'type' is an Objective C type |
| 2712 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 2713 | // the appropriate pointer type |
| 2714 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 2715 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 2716 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 2717 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2718 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2719 | << CodeModificationHint::CreateReplacement(OpLoc, "->"); |
| 2720 | BaseType = PT->getPointeeType(); |
| 2721 | IsArrow = true; |
| 2722 | } |
| 2723 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2724 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2725 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2726 | // Handle field access to simple records. This also handles access |
| 2727 | // to fields of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2728 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2729 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 2730 | RTy, OpLoc, SS)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2731 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2732 | return Owned((Expr*) 0); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2733 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2734 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2735 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2736 | // into a record type was handled above, any destructor we see here is a |
| 2737 | // pseudo-destructor. |
| 2738 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2739 | // C++ [expr.pseudo]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | // The left hand side of the dot operator shall be of scalar type. The |
| 2741 | // 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] | 2742 | // type. |
| 2743 | if (!BaseType->isScalarType()) |
| 2744 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2745 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2747 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2748 | // same as the object type. |
| 2749 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2750 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2751 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2752 | << BaseType << MemberName.getCXXNameType() |
| 2753 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | |
| 2755 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2756 | // the form |
| 2757 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2759 | // |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2760 | // shall designate the same scalar type. |
| 2761 | // |
| 2762 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2763 | // isn't checked here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2765 | // FIXME: We've lost the precise spelling of the type by going through |
| 2766 | // DeclarationName. Can we do better? |
| 2767 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2768 | IsArrow, OpLoc, |
| 2769 | (NestedNameSpecifier *) SS.getScopeRep(), |
| 2770 | SS.getRange(), |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2771 | MemberName.getCXXNameType(), |
| 2772 | MemberLoc)); |
| 2773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2774 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2775 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2776 | // (*Obj).ivar. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2777 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2778 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2779 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | const ObjCInterfaceType *IFaceT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2781 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2782 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2783 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2784 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2785 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2786 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2787 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2789 | if (IV) { |
| 2790 | // If the decl being referenced had an error, return an error for this |
| 2791 | // sub-expr without emitting another error, in order to avoid cascading |
| 2792 | // error cases. |
| 2793 | if (IV->isInvalidDecl()) |
| 2794 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2795 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2796 | // Check whether we can reference this field. |
| 2797 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2798 | return ExprError(); |
| 2799 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2800 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2801 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2802 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2803 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2804 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2805 | // Case of a c-function declared inside an objc implementation. |
| 2806 | // FIXME: For a c-style function nested inside an objc implementation |
| 2807 | // class, there is no implementation context available, so we pass |
| 2808 | // down the context as argument to this routine. Ideally, this context |
| 2809 | // need be passed down in the AST node and somehow calculated from the |
| 2810 | // AST for a function decl. |
| 2811 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2813 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2814 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2815 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2816 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2817 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | |
| 2820 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2821 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2822 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2824 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2825 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2826 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2828 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2829 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2830 | |
| 2831 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2832 | MemberLoc, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2833 | IsArrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2834 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2835 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2836 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2837 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2838 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2839 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2840 | // Handle properties on 'id' and qualified "id". |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2841 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 2842 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2843 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2844 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2846 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2847 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2848 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2849 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2850 | // Check the use of this declaration |
| 2851 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2852 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2854 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2855 | MemberLoc, BaseExpr)); |
| 2856 | } |
| 2857 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2858 | // Check the use of this method. |
| 2859 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2860 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2862 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | OMD->getResultType(), |
| 2864 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2865 | NULL, 0)); |
| 2866 | } |
| 2867 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2868 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2869 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2870 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2871 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2872 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2873 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2874 | const ObjCObjectPointerType *OPT; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2875 | if (!IsArrow && (OPT = BaseType->getAsObjCInterfacePointerType())) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2876 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2877 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2878 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2880 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2881 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2882 | // Check whether we can reference this property. |
| 2883 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2884 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2885 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2886 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2887 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2888 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2889 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2890 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2891 | MemberLoc, BaseExpr)); |
| 2892 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2893 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2894 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2895 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2896 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2897 | // Check whether we can reference this property. |
| 2898 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2899 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2900 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2901 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2902 | MemberLoc, BaseExpr)); |
| 2903 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2904 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2905 | // selector is implemented. |
| 2906 | |
| 2907 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2908 | // shared with the code in ActOnInstanceMessage. |
| 2909 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2910 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2911 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2912 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2913 | // If this reference is in an @implementation, check for 'private' methods. |
| 2914 | if (!Getter) |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2915 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2916 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2917 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2918 | if (!Getter) |
| 2919 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2920 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2921 | // Check if we can reference this property. |
| 2922 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2923 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2924 | } |
| 2925 | // If we found a getter then this may be a valid dot-reference, we |
| 2926 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | Selector SetterSel = |
| 2928 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2929 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2930 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2931 | if (!Setter) { |
| 2932 | // If this reference is in an @implementation, also check for 'private' |
| 2933 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2934 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2935 | } |
| 2936 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2937 | if (!Setter) |
| 2938 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2939 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2940 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2941 | return ExprError(); |
| 2942 | |
| 2943 | if (Getter || Setter) { |
| 2944 | QualType PType; |
| 2945 | |
| 2946 | if (Getter) |
| 2947 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2948 | else |
| 2949 | // Get the expression type from Setter's incoming parameter. |
| 2950 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2951 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2952 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2953 | Setter, MemberLoc, BaseExpr)); |
| 2954 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2955 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2956 | << MemberName << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2958 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2959 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2960 | if (!IsArrow && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2961 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2962 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2963 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2964 | Context.getObjCClassType())); |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2966 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2967 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2968 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2969 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2970 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2971 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2972 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2973 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2974 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2975 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2976 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2977 | << BaseType << BaseExpr->getSourceRange(); |
| 2978 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2979 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2982 | static Sema::OwningExprResult DiagnoseDtorReference(Sema &SemaRef, |
| 2983 | SourceLocation NameLoc, |
| 2984 | Sema::ExprArg MemExpr) { |
| 2985 | Expr *E = (Expr *) MemExpr.get(); |
| 2986 | SourceLocation ExpectedLParenLoc = SemaRef.PP.getLocForEndOfToken(NameLoc); |
| 2987 | SemaRef.Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2988 | << isa<CXXPseudoDestructorExpr>(E) |
| 2989 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 2990 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2991 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, |
| 2992 | move(MemExpr), |
| 2993 | /*LPLoc*/ ExpectedLParenLoc, |
| 2994 | Sema::MultiExprArg(SemaRef, 0, 0), |
| 2995 | /*CommaLocs*/ 0, |
| 2996 | /*RPLoc*/ ExpectedLParenLoc); |
| 2997 | } |
| 2998 | |
| 2999 | /// The main callback when the parser finds something like |
| 3000 | /// expression . [nested-name-specifier] identifier |
| 3001 | /// expression -> [nested-name-specifier] identifier |
| 3002 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3003 | /// possibilities, including destructor and operator references. |
| 3004 | /// |
| 3005 | /// \param OpKind either tok::arrow or tok::period |
| 3006 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3007 | /// is used to diagnose mis-uses of special members that can |
| 3008 | /// only be called |
| 3009 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3010 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3011 | /// aren't properly put in the context chain |
| 3012 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg BaseArg, |
| 3013 | SourceLocation OpLoc, |
| 3014 | tok::TokenKind OpKind, |
| 3015 | const CXXScopeSpec &SS, |
| 3016 | UnqualifiedId &Id, |
| 3017 | DeclPtrTy ObjCImpDecl, |
| 3018 | bool HasTrailingLParen) { |
| 3019 | if (SS.isSet() && SS.isInvalid()) |
| 3020 | return ExprError(); |
| 3021 | |
| 3022 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3023 | |
| 3024 | // Decompose the name into its component parts. |
| 3025 | DeclarationName Name; |
| 3026 | SourceLocation NameLoc; |
| 3027 | const TemplateArgumentListInfo *TemplateArgs; |
| 3028 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 3029 | Name, NameLoc, TemplateArgs); |
| 3030 | |
| 3031 | bool IsArrow = (OpKind == tok::arrow); |
| 3032 | |
| 3033 | NamedDecl *FirstQualifierInScope |
| 3034 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3035 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3036 | |
| 3037 | // This is a postfix expression, so get rid of ParenListExprs. |
| 3038 | BaseArg = MaybeConvertParenListExprToParenExpr(S, move(BaseArg)); |
| 3039 | |
| 3040 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 3041 | OwningExprResult Result(*this); |
| 3042 | if (Base->getType()->isDependentType()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3043 | Result = ActOnDependentMemberExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3044 | IsArrow, OpLoc, |
| 3045 | SS, FirstQualifierInScope, |
| 3046 | Name, NameLoc, |
| 3047 | TemplateArgs); |
| 3048 | } else { |
| 3049 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
| 3050 | if (TemplateArgs) { |
| 3051 | // Re-use the lookup done for the template name. |
| 3052 | DecomposeTemplateName(R, Id); |
| 3053 | } else { |
| 3054 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 3055 | SS, FirstQualifierInScope, |
| 3056 | ObjCImpDecl); |
| 3057 | |
| 3058 | if (Result.isInvalid()) { |
| 3059 | Owned(Base); |
| 3060 | return ExprError(); |
| 3061 | } |
| 3062 | |
| 3063 | if (Result.get()) { |
| 3064 | // The only way a reference to a destructor can be used is to |
| 3065 | // immediately call it, which falls into this case. If the |
| 3066 | // next token is not a '(', produce a diagnostic and build the |
| 3067 | // call now. |
| 3068 | if (!HasTrailingLParen && |
| 3069 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| 3070 | return DiagnoseDtorReference(*this, NameLoc, move(Result)); |
| 3071 | |
| 3072 | return move(Result); |
| 3073 | } |
| 3074 | } |
| 3075 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3076 | Result = BuildMemberReferenceExpr(ExprArg(*this, Base), Base->getType(), |
| 3077 | OpLoc, IsArrow, SS, R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3083 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 3084 | FunctionDecl *FD, |
| 3085 | ParmVarDecl *Param) { |
| 3086 | if (Param->hasUnparsedDefaultArg()) { |
| 3087 | Diag (CallLoc, |
| 3088 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3089 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3091 | diag::note_default_argument_declared_here); |
| 3092 | } else { |
| 3093 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3094 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 3095 | |
| 3096 | // Instantiate the expression. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3097 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3098 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3100 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3101 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3102 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3103 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3105 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | |
| 3107 | if (SetParamDefaultArgument(Param, move(Result), |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3108 | /*FIXME:EqualLoc*/ |
| 3109 | UninstExpr->getSourceRange().getBegin())) |
| 3110 | return ExprError(); |
| 3111 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3113 | // If the default expression creates temporaries, we need to |
| 3114 | // push them to the current stack of expression temporaries so they'll |
| 3115 | // be properly destroyed. |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 3116 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3117 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | // We already type-checked the argument, so we know it works. |
| 3121 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 3122 | } |
| 3123 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3124 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3125 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3126 | /// function prototype Proto. Call is the call expression itself, and |
| 3127 | /// Fn is the function expression. For a C++ member function, this |
| 3128 | /// routine does not attempt to convert the object argument. Returns |
| 3129 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3130 | bool |
| 3131 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3132 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3133 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3134 | Expr **Args, unsigned NumArgs, |
| 3135 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3136 | // 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] | 3137 | // assignment, to the types of the corresponding parameter, ... |
| 3138 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3139 | bool Invalid = false; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3140 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3141 | // If too few arguments are available (and we don't have default |
| 3142 | // arguments for the remaining parameters), don't make the call. |
| 3143 | if (NumArgs < NumArgsInProto) { |
| 3144 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3145 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 3146 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3147 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | // If too many are passed and not variadic, error on the extras and drop |
| 3151 | // them. |
| 3152 | if (NumArgs > NumArgsInProto) { |
| 3153 | if (!Proto->isVariadic()) { |
| 3154 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3155 | diag::err_typecheck_call_too_many_args) |
| 3156 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 3157 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3158 | Args[NumArgs-1]->getLocEnd()); |
| 3159 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3160 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3161 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3162 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3163 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3164 | llvm::SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3165 | VariadicCallType CallType = |
| 3166 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3167 | if (Fn->getType()->isBlockPointerType()) |
| 3168 | CallType = VariadicBlock; // Block |
| 3169 | else if (isa<MemberExpr>(Fn)) |
| 3170 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3171 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3172 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3173 | if (Invalid) |
| 3174 | return true; |
| 3175 | unsigned TotalNumArgs = AllArgs.size(); |
| 3176 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3177 | Call->setArg(i, AllArgs[i]); |
| 3178 | |
| 3179 | return false; |
| 3180 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3181 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3182 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3183 | FunctionDecl *FDecl, |
| 3184 | const FunctionProtoType *Proto, |
| 3185 | unsigned FirstProtoArg, |
| 3186 | Expr **Args, unsigned NumArgs, |
| 3187 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3188 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3189 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3190 | unsigned NumArgsToCheck = NumArgs; |
| 3191 | bool Invalid = false; |
| 3192 | if (NumArgs != NumArgsInProto) |
| 3193 | // Use default arguments for missing arguments |
| 3194 | NumArgsToCheck = NumArgsInProto; |
| 3195 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3196 | // 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] | 3197 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3198 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3199 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3200 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3201 | if (ArgIx < NumArgs) { |
| 3202 | Arg = Args[ArgIx++]; |
| 3203 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3204 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3205 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3206 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3207 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3208 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3209 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3210 | // Pass the argument. |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3211 | if (PerformCopyInitialization(Arg, ProtoArgType, AA_Passing)) |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3212 | return true; |
Anders Carlsson | 03d8ed4 | 2009-11-13 04:34:45 +0000 | [diff] [blame] | 3213 | |
Anders Carlsson | 4b3cbea | 2009-11-13 17:04:35 +0000 | [diff] [blame] | 3214 | if (!ProtoArgType->isReferenceType()) |
| 3215 | Arg = MaybeBindToTemporary(Arg).takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3216 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3217 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3218 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3220 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3221 | if (ArgExpr.isInvalid()) |
| 3222 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3223 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3224 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3225 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3226 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3227 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3228 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3229 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3230 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3231 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3232 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3233 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 3234 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3235 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3238 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3241 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3242 | /// This provides the location of the left/right parens and a list of comma |
| 3243 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3244 | Action::OwningExprResult |
| 3245 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 3246 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3247 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3248 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 3251 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 3253 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3254 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 3255 | assert(Fn && "no function call expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3257 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3258 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3259 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3260 | if (NumArgs > 0) { |
| 3261 | // Pseudo-destructor calls should not have any arguments. |
| 3262 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 3263 | << CodeModificationHint::CreateRemoval( |
| 3264 | SourceRange(Args[0]->getLocStart(), |
| 3265 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3267 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3268 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3270 | NumArgs = 0; |
| 3271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3273 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3274 | RParenLoc)); |
| 3275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3277 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3278 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3279 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3280 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3281 | bool Dependent = false; |
| 3282 | if (Fn->isTypeDependent()) |
| 3283 | Dependent = true; |
| 3284 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3285 | Dependent = true; |
| 3286 | |
| 3287 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3288 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3289 | Context.DependentTy, RParenLoc)); |
| 3290 | |
| 3291 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3292 | if (Fn->getType()->isRecordType()) |
| 3293 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 3294 | CommaLocs, RParenLoc)); |
| 3295 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3296 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3297 | |
| 3298 | // Determine whether this is a call to an unresolved member function. |
| 3299 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3300 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3301 | // an unresolved using declaration), it has to be an overloaded |
| 3302 | // function set, which means it must contain either multiple |
| 3303 | // declarations (all methods or method templates) or a single |
| 3304 | // method template. |
| 3305 | assert((MemE->getNumDecls() > 1) || |
| 3306 | isa<FunctionTemplateDecl>(*MemE->decls_begin())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3307 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3308 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3309 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3310 | CommaLocs, RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3313 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3314 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3315 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3316 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3317 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3318 | CommaLocs, RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3319 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3320 | |
| 3321 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3322 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3323 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 3324 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3325 | if (const FunctionProtoType *FPT = |
| 3326 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 3327 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3328 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3329 | ExprOwningPtr<CXXMemberCallExpr> |
| 3330 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3331 | NumArgs, ResultTy, |
| 3332 | RParenLoc)); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3333 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3334 | if (CheckCallReturnType(FPT->getResultType(), |
| 3335 | BO->getRHS()->getSourceRange().getBegin(), |
| 3336 | TheCall.get(), 0)) |
| 3337 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3338 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3339 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 3340 | RParenLoc)) |
| 3341 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3342 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3343 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 3344 | } |
| 3345 | return ExprError(Diag(Fn->getLocStart(), |
| 3346 | diag::err_typecheck_call_not_function) |
| 3347 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3352 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3354 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3355 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3356 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
| 3357 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 3358 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 3359 | return BuildOverloadedCallExpr(Fn, ULE, LParenLoc, Args, NumArgs, |
| 3360 | CommaLocs, RParenLoc); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3361 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3362 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3363 | NamedDecl *NDecl = 0; |
| 3364 | if (isa<DeclRefExpr>(NakedFn)) |
| 3365 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 3366 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3367 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3368 | } |
| 3369 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3370 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3371 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3372 | /// unary-convert to an expression of function-pointer or |
| 3373 | /// block-pointer type. |
| 3374 | /// |
| 3375 | /// \param NDecl the declaration being called, if available |
| 3376 | Sema::OwningExprResult |
| 3377 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3378 | SourceLocation LParenLoc, |
| 3379 | Expr **Args, unsigned NumArgs, |
| 3380 | SourceLocation RParenLoc) { |
| 3381 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3382 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3383 | // Promote the function operand. |
| 3384 | UsualUnaryConversions(Fn); |
| 3385 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3386 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3387 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3388 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 3389 | Args, NumArgs, |
| 3390 | Context.BoolTy, |
| 3391 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3392 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3393 | const FunctionType *FuncT; |
| 3394 | if (!Fn->getType()->isBlockPointerType()) { |
| 3395 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3396 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3397 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3398 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3399 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3400 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3401 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3402 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3403 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3404 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3405 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3406 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3407 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3408 | << Fn->getType() << Fn->getSourceRange()); |
| 3409 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3410 | // Check for a valid return type |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3411 | if (CheckCallReturnType(FuncT->getResultType(), |
| 3412 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 3413 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3414 | return ExprError(); |
| 3415 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3416 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3417 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3419 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3420 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3421 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3422 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3423 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3424 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3426 | if (FDecl) { |
| 3427 | // Check if we have too few/too many template arguments, based |
| 3428 | // on our knowledge of the function definition. |
| 3429 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3430 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3431 | const FunctionProtoType *Proto = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3432 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3433 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3434 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3435 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3436 | } |
| 3437 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3440 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3441 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3442 | Expr *Arg = Args[i]; |
| 3443 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3444 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3445 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3446 | PDiag(diag::err_call_incomplete_argument) |
| 3447 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3448 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3449 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3450 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3451 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3452 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3453 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3454 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3455 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3456 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3457 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3458 | // Check for sentinels |
| 3459 | if (NDecl) |
| 3460 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3462 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3463 | if (FDecl) { |
| 3464 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3465 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3467 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3468 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3469 | } else if (NDecl) { |
| 3470 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3471 | return ExprError(); |
| 3472 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3473 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3474 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3477 | Action::OwningExprResult |
| 3478 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3479 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3480 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3481 | |
| 3482 | TypeSourceInfo *TInfo = 0; |
| 3483 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3484 | if (!TInfo) |
| 3485 | TInfo = Context.getTrivialTypeSourceInfo(literalType, LParenLoc); |
| 3486 | |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3487 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3488 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3489 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3490 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3491 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3492 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3493 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3494 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3495 | } else if (!literalType->isDependentType() && |
| 3496 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3497 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3498 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3499 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3500 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3502 | InitializedEntity Entity |
| 3503 | = InitializedEntity::InitializeTemporary(TInfo->getTypeLoc()); |
| 3504 | InitializationKind Kind |
| 3505 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| 3506 | /*IsCStyleCast=*/true); |
| 3507 | if (CheckInitializerTypes(literalExpr, literalType, Entity, Kind)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3508 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3510 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3511 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3512 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3513 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3514 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3515 | InitExpr.release(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3516 | |
| 3517 | // FIXME: Store the TInfo to preserve type information better. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3518 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3519 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3522 | Action::OwningExprResult |
| 3523 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3524 | SourceLocation RBraceLoc) { |
| 3525 | unsigned NumInit = initlist.size(); |
| 3526 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3527 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3528 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3529 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3530 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3531 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3532 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3533 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3534 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3537 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3538 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3539 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3540 | return CastExpr::CK_NoOp; |
| 3541 | |
| 3542 | if (SrcTy->hasPointerRepresentation()) { |
| 3543 | if (DestTy->hasPointerRepresentation()) |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 3544 | return DestTy->isObjCObjectPointerType() ? |
| 3545 | CastExpr::CK_AnyPointerToObjCPointerCast : |
| 3546 | CastExpr::CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3547 | if (DestTy->isIntegerType()) |
| 3548 | return CastExpr::CK_PointerToIntegral; |
| 3549 | } |
| 3550 | |
| 3551 | if (SrcTy->isIntegerType()) { |
| 3552 | if (DestTy->isIntegerType()) |
| 3553 | return CastExpr::CK_IntegralCast; |
| 3554 | if (DestTy->hasPointerRepresentation()) |
| 3555 | return CastExpr::CK_IntegralToPointer; |
| 3556 | if (DestTy->isRealFloatingType()) |
| 3557 | return CastExpr::CK_IntegralToFloating; |
| 3558 | } |
| 3559 | |
| 3560 | if (SrcTy->isRealFloatingType()) { |
| 3561 | if (DestTy->isRealFloatingType()) |
| 3562 | return CastExpr::CK_FloatingCast; |
| 3563 | if (DestTy->isIntegerType()) |
| 3564 | return CastExpr::CK_FloatingToIntegral; |
| 3565 | } |
| 3566 | |
| 3567 | // FIXME: Assert here. |
| 3568 | // assert(false && "Unhandled cast combination!"); |
| 3569 | return CastExpr::CK_Unknown; |
| 3570 | } |
| 3571 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3572 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3573 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3575 | CXXMethodDecl *& ConversionDecl, |
| 3576 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3577 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3578 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3579 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3580 | |
Eli Friedman | 199ea95 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3581 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3582 | |
| 3583 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3584 | // type needs to be scalar. |
| 3585 | if (castType->isVoidType()) { |
| 3586 | // Cast to void allows any expr type. |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3587 | Kind = CastExpr::CK_ToVoid; |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3592 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3593 | (castType->isStructureType() || castType->isUnionType())) { |
| 3594 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3595 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3596 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3597 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3598 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3599 | return false; |
| 3600 | } |
| 3601 | |
| 3602 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3603 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3604 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3605 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3606 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3607 | Field != FieldEnd; ++Field) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3608 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3609 | castExpr->getType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3610 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3611 | << castExpr->getSourceRange(); |
| 3612 | break; |
| 3613 | } |
| 3614 | } |
| 3615 | if (Field == FieldEnd) |
| 3616 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3617 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3618 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3619 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3620 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3621 | |
| 3622 | // Reject any other conversions to non-scalar types. |
| 3623 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3624 | << castType << castExpr->getSourceRange(); |
| 3625 | } |
| 3626 | |
| 3627 | if (!castExpr->getType()->isScalarType() && |
| 3628 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3629 | return Diag(castExpr->getLocStart(), |
| 3630 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3631 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3634 | if (castType->isExtVectorType()) |
| 3635 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3636 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3637 | if (castType->isVectorType()) |
| 3638 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3639 | if (castExpr->getType()->isVectorType()) |
| 3640 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3641 | |
| 3642 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3643 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3644 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3645 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3646 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3647 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3648 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3649 | QualType castExprType = castExpr->getType(); |
| 3650 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3651 | return Diag(castExpr->getLocStart(), |
| 3652 | diag::err_cast_pointer_from_non_pointer_int) |
| 3653 | << castExprType << castExpr->getSourceRange(); |
| 3654 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3655 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3656 | return Diag(castExpr->getLocStart(), |
| 3657 | diag::err_cast_pointer_to_non_pointer_int) |
| 3658 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3659 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3660 | |
| 3661 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3662 | return false; |
| 3663 | } |
| 3664 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3665 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3666 | CastExpr::CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3667 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3668 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3669 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3670 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3671 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3672 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3673 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3674 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3675 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3676 | } else |
| 3677 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3678 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3679 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3680 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3681 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3682 | return false; |
| 3683 | } |
| 3684 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3685 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3686 | CastExpr::CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3687 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3688 | |
| 3689 | QualType SrcTy = CastExpr->getType(); |
| 3690 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3691 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3692 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3693 | if (SrcTy->isVectorType()) { |
| 3694 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3695 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3696 | << DestTy << SrcTy << R; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3697 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3698 | return false; |
| 3699 | } |
| 3700 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3701 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3702 | // conversion will take place first from scalar to elt type, and then |
| 3703 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3704 | if (SrcTy->isPointerType()) |
| 3705 | return Diag(R.getBegin(), |
| 3706 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3707 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3708 | |
| 3709 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3710 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3711 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3712 | |
| 3713 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3714 | return false; |
| 3715 | } |
| 3716 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3717 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3718 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3719 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3720 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3722 | assert((Ty != 0) && (Op.get() != 0) && |
| 3723 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3724 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3725 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3726 | //FIXME: Preserve type source info. |
| 3727 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3729 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3730 | if (isa<ParenListExpr>(castExpr)) |
| 3731 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3732 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3734 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3735 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3736 | |
| 3737 | if (Method) { |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3738 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3739 | Method, move(Op)); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3740 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3741 | if (CastArg.isInvalid()) |
| 3742 | return ExprError(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3743 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3744 | castExpr = CastArg.takeAs<Expr>(); |
| 3745 | } else { |
| 3746 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3749 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | Kind, castExpr, castType, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3751 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3754 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3755 | /// of comma binary operators. |
| 3756 | Action::OwningExprResult |
| 3757 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3758 | Expr *expr = EA.takeAs<Expr>(); |
| 3759 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3760 | if (!E) |
| 3761 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3763 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3765 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3766 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3767 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3769 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3770 | } |
| 3771 | |
| 3772 | Action::OwningExprResult |
| 3773 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3774 | SourceLocation RParenLoc, ExprArg Op, |
| 3775 | QualType Ty) { |
| 3776 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | |
| 3778 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3779 | // then handle it as such. |
| 3780 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3781 | if (PE->getNumExprs() == 0) { |
| 3782 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3783 | return ExprError(); |
| 3784 | } |
| 3785 | |
| 3786 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3787 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3788 | initExprs.push_back(PE->getExpr(i)); |
| 3789 | |
| 3790 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3791 | // braces instead of the original commas. |
| 3792 | Op.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3794 | initExprs.size(), RParenLoc); |
| 3795 | E->setType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3797 | Owned(E)); |
| 3798 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | // 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] | 3800 | // sequence of BinOp comma operators. |
| 3801 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3802 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3803 | } |
| 3804 | } |
| 3805 | |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3806 | Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3807 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3808 | MultiExprArg Val, |
| 3809 | TypeTy *TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3810 | unsigned nexprs = Val.size(); |
| 3811 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3812 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 3813 | Expr *expr; |
| 3814 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 3815 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 3816 | else |
| 3817 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3818 | return Owned(expr); |
| 3819 | } |
| 3820 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3821 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3822 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3823 | /// C99 6.5.15 |
| 3824 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3825 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3826 | // C++ is sufficiently different to merit its own checker. |
| 3827 | if (getLangOptions().CPlusPlus) |
| 3828 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3829 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 3830 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 3831 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3832 | UsualUnaryConversions(Cond); |
| 3833 | UsualUnaryConversions(LHS); |
| 3834 | UsualUnaryConversions(RHS); |
| 3835 | QualType CondTy = Cond->getType(); |
| 3836 | QualType LHSTy = LHS->getType(); |
| 3837 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3839 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3840 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3841 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3842 | << CondTy; |
| 3843 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3844 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3846 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3847 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3848 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3849 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3850 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3851 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3852 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3853 | UsualArithmeticConversions(LHS, RHS); |
| 3854 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3855 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3856 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3857 | // If both operands are the same structure or union type, the result is that |
| 3858 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3859 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3860 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3861 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3862 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3863 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3864 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3865 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3866 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3867 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3868 | // 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] | 3869 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3870 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3871 | if (!LHSTy->isVoidType()) |
| 3872 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3873 | << RHS->getSourceRange(); |
| 3874 | if (!RHSTy->isVoidType()) |
| 3875 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3876 | << LHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3877 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 3878 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3879 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3880 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3881 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3882 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3883 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3884 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3885 | // promote the null to a pointer. |
| 3886 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3887 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3888 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3889 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3890 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3891 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3892 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3893 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3894 | |
| 3895 | // All objective-c pointer type analysis is done here. |
| 3896 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 3897 | QuestionLoc); |
| 3898 | if (!compositeType.isNull()) |
| 3899 | return compositeType; |
| 3900 | |
| 3901 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3902 | // Handle block pointer types. |
| 3903 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3904 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3905 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3906 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3907 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 3908 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3909 | return destType; |
| 3910 | } |
| 3911 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3912 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3913 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3914 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3915 | // We have 2 block pointer types. |
| 3916 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3917 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3918 | return LHSTy; |
| 3919 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3920 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3921 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3922 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3923 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3924 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3925 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3926 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3927 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3928 | // In this situation, we assume void* type. No especially good |
| 3929 | // reason, but this is what gcc does, and we do have to pick |
| 3930 | // to get a consistent AST. |
| 3931 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3932 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3933 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3934 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3935 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3936 | // The block pointer types are compatible. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3937 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3938 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3939 | return LHSTy; |
| 3940 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 3941 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3942 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3943 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3944 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3945 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3946 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3947 | |
| 3948 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3949 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3950 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3951 | QualType destPointee |
| 3952 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3953 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3954 | // Add qualifiers if necessary. |
| 3955 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 3956 | // Promote to void*. |
| 3957 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3958 | return destType; |
| 3959 | } |
| 3960 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3961 | QualType destPointee |
| 3962 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3963 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3964 | // Add qualifiers if necessary. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3965 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3966 | // Promote to void*. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3967 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3968 | return destType; |
| 3969 | } |
| 3970 | |
| 3971 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3972 | // Two identical pointer types are always compatible. |
| 3973 | return LHSTy; |
| 3974 | } |
| 3975 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3976 | rhptee.getUnqualifiedType())) { |
| 3977 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3978 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3979 | // In this situation, we assume void* type. No especially good |
| 3980 | // reason, but this is what gcc does, and we do have to pick |
| 3981 | // to get a consistent AST. |
| 3982 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3983 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3984 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3985 | return incompatTy; |
| 3986 | } |
| 3987 | // The pointer types are compatible. |
| 3988 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3989 | // differently qualified versions of compatible types, the result type is |
| 3990 | // a pointer to an appropriately qualified version of the *composite* |
| 3991 | // type. |
| 3992 | // FIXME: Need to calculate the composite type. |
| 3993 | // FIXME: Need to add qualifiers |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3994 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3995 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3996 | return LHSTy; |
| 3997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3998 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3999 | // GCC compatibility: soften pointer/integer mismatch. |
| 4000 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4001 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4002 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4003 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4004 | return RHSTy; |
| 4005 | } |
| 4006 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4007 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4008 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4009 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4010 | return LHSTy; |
| 4011 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4013 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4014 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4015 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4016 | return QualType(); |
| 4017 | } |
| 4018 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4019 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4020 | /// two objective-c pointer types of the two input expressions. |
| 4021 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4022 | SourceLocation QuestionLoc) { |
| 4023 | QualType LHSTy = LHS->getType(); |
| 4024 | QualType RHSTy = RHS->getType(); |
| 4025 | |
| 4026 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4027 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4028 | // redefinition type if an attempt is made to access its fields. |
| 4029 | if (LHSTy->isObjCClassType() && |
| 4030 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4031 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4032 | return LHSTy; |
| 4033 | } |
| 4034 | if (RHSTy->isObjCClassType() && |
| 4035 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4036 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4037 | return RHSTy; |
| 4038 | } |
| 4039 | // And the same for struct objc_object* / id |
| 4040 | if (LHSTy->isObjCIdType() && |
| 4041 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4042 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4043 | return LHSTy; |
| 4044 | } |
| 4045 | if (RHSTy->isObjCIdType() && |
| 4046 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4047 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4048 | return RHSTy; |
| 4049 | } |
| 4050 | // And the same for struct objc_selector* / SEL |
| 4051 | if (Context.isObjCSelType(LHSTy) && |
| 4052 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4053 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4054 | return LHSTy; |
| 4055 | } |
| 4056 | if (Context.isObjCSelType(RHSTy) && |
| 4057 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4058 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4059 | return RHSTy; |
| 4060 | } |
| 4061 | // Check constraints for Objective-C object pointers types. |
| 4062 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4063 | |
| 4064 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4065 | // Two identical object pointer types are always compatible. |
| 4066 | return LHSTy; |
| 4067 | } |
| 4068 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4069 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4070 | QualType compositeType = LHSTy; |
| 4071 | |
| 4072 | // If both operands are interfaces and either operand can be |
| 4073 | // assigned to the other, use that type as the composite |
| 4074 | // type. This allows |
| 4075 | // xxx ? (A*) a : (B*) b |
| 4076 | // where B is a subclass of A. |
| 4077 | // |
| 4078 | // Additionally, as for assignment, if either type is 'id' |
| 4079 | // allow silent coercion. Finally, if the types are |
| 4080 | // incompatible then make sure to use 'id' as the composite |
| 4081 | // type so the result is acceptable for sending messages to. |
| 4082 | |
| 4083 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4084 | // It could return the composite type. |
| 4085 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4086 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4087 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4088 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4089 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4090 | RHSTy->isObjCQualifiedIdType()) && |
| 4091 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4092 | // Need to handle "id<xx>" explicitly. |
| 4093 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4094 | // id. Currently localizing to here until clear this should be |
| 4095 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4096 | compositeType = Context.getObjCIdType(); |
| 4097 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4098 | compositeType = Context.getObjCIdType(); |
| 4099 | } else if (!(compositeType = |
| 4100 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4101 | ; |
| 4102 | else { |
| 4103 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4104 | << LHSTy << RHSTy |
| 4105 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4106 | QualType incompatTy = Context.getObjCIdType(); |
| 4107 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4108 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
| 4109 | return incompatTy; |
| 4110 | } |
| 4111 | // The object pointer types are compatible. |
| 4112 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 4113 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
| 4114 | return compositeType; |
| 4115 | } |
| 4116 | // Check Objective-C object pointer types and 'void *' |
| 4117 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4118 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4119 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4120 | QualType destPointee |
| 4121 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4122 | QualType destType = Context.getPointerType(destPointee); |
| 4123 | // Add qualifiers if necessary. |
| 4124 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4125 | // Promote to void*. |
| 4126 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
| 4127 | return destType; |
| 4128 | } |
| 4129 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4130 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4131 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4132 | QualType destPointee |
| 4133 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4134 | QualType destType = Context.getPointerType(destPointee); |
| 4135 | // Add qualifiers if necessary. |
| 4136 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 4137 | // Promote to void*. |
| 4138 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4139 | return destType; |
| 4140 | } |
| 4141 | return QualType(); |
| 4142 | } |
| 4143 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4144 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4145 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4146 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 4147 | SourceLocation ColonLoc, |
| 4148 | ExprArg Cond, ExprArg LHS, |
| 4149 | ExprArg RHS) { |
| 4150 | Expr *CondExpr = (Expr *) Cond.get(); |
| 4151 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4152 | |
| 4153 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4154 | // was the condition. |
| 4155 | bool isLHSNull = LHSExpr == 0; |
| 4156 | if (isLHSNull) |
| 4157 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4158 | |
| 4159 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4160 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4161 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4162 | return ExprError(); |
| 4163 | |
| 4164 | Cond.release(); |
| 4165 | LHS.release(); |
| 4166 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4167 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4168 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4169 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4172 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4173 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4174 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4175 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4176 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4177 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4178 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4179 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4180 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4181 | if ((lhsType->isObjCClassType() && |
| 4182 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4183 | (rhsType->isObjCClassType() && |
| 4184 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4185 | return Compatible; |
| 4186 | } |
| 4187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4188 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4189 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4190 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4192 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4193 | lhptee = Context.getCanonicalType(lhptee); |
| 4194 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4195 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4196 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4197 | |
| 4198 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4199 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4200 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4201 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4202 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4203 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4204 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4205 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4206 | // 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] | 4207 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4208 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4209 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4210 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4211 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4212 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4213 | assert(rhptee->isFunctionType()); |
| 4214 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4215 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4216 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4217 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4218 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4219 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4220 | |
| 4221 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4222 | assert(lhptee->isFunctionType()); |
| 4223 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4224 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4225 | // 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] | 4226 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4227 | lhptee = lhptee.getUnqualifiedType(); |
| 4228 | rhptee = rhptee.getUnqualifiedType(); |
| 4229 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4230 | // Check if the pointee types are compatible ignoring the sign. |
| 4231 | // We explicitly check for char so that we catch "char" vs |
| 4232 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4233 | if (lhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4234 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4235 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4236 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4237 | |
| 4238 | if (rhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4239 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4240 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4241 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4242 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4243 | if (lhptee == rhptee) { |
| 4244 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4245 | // takes priority over sign incompatibility because the sign |
| 4246 | // warning can be disabled. |
| 4247 | if (ConvTy != Compatible) |
| 4248 | return ConvTy; |
| 4249 | return IncompatiblePointerSign; |
| 4250 | } |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4251 | |
| 4252 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4253 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4254 | // the eventual target type is the same and the pointers have the same |
| 4255 | // level of indirection, this must be the issue. |
| 4256 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4257 | do { |
| 4258 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4259 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 4260 | |
| 4261 | lhptee = Context.getCanonicalType(lhptee); |
| 4262 | rhptee = Context.getCanonicalType(rhptee); |
| 4263 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 4264 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4265 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4266 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4269 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4271 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4272 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4275 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4276 | /// block pointer types are compatible or whether a block and normal pointer |
| 4277 | /// are compatible. It is more restrict than comparing two function pointer |
| 4278 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4279 | Sema::AssignConvertType |
| 4280 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4281 | QualType rhsType) { |
| 4282 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4283 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4284 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4285 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4286 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4287 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4288 | // make sure we operate on the canonical type |
| 4289 | lhptee = Context.getCanonicalType(lhptee); |
| 4290 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4291 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4292 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4293 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4294 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4295 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4296 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4297 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4298 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4299 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4300 | return ConvTy; |
| 4301 | } |
| 4302 | |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4303 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4304 | /// for assignment compatibility. |
| 4305 | Sema::AssignConvertType |
| 4306 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4307 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 4308 | return Compatible; |
| 4309 | QualType lhptee = |
| 4310 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4311 | QualType rhptee = |
| 4312 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4313 | // make sure we operate on the canonical type |
| 4314 | lhptee = Context.getCanonicalType(lhptee); |
| 4315 | rhptee = Context.getCanonicalType(rhptee); |
| 4316 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4317 | return CompatiblePointerDiscardsQualifiers; |
| 4318 | |
| 4319 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4320 | return Compatible; |
| 4321 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4322 | return IncompatibleObjCQualifiedId; |
| 4323 | return IncompatiblePointer; |
| 4324 | } |
| 4325 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4326 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4327 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4328 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4329 | /// |
| 4330 | /// int a, *pint; |
| 4331 | /// short *pshort; |
| 4332 | /// struct foo *pfoo; |
| 4333 | /// |
| 4334 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4335 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4336 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4337 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4338 | /// |
| 4339 | /// 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] | 4340 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4341 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4342 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4343 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4344 | // Get canonical types. We're not formatting these types, just comparing |
| 4345 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4346 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4347 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4348 | |
| 4349 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4350 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4351 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4352 | if ((lhsType->isObjCClassType() && |
| 4353 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4354 | (rhsType->isObjCClassType() && |
| 4355 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4356 | return Compatible; |
| 4357 | } |
| 4358 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4359 | // If the left-hand side is a reference type, then we are in a |
| 4360 | // (rare!) case where we've allowed the use of references in C, |
| 4361 | // e.g., as a parameter type in a built-in function. In this case, |
| 4362 | // just make sure that the type referenced is compatible with the |
| 4363 | // right-hand side type. The caller is responsible for adjusting |
| 4364 | // lhsType so that the resulting expression does not have reference |
| 4365 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4366 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4367 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4368 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4369 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4370 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4371 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4372 | // to the same ExtVector type. |
| 4373 | if (lhsType->isExtVectorType()) { |
| 4374 | if (rhsType->isExtVectorType()) |
| 4375 | return lhsType == rhsType ? Compatible : Incompatible; |
| 4376 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 4377 | return Compatible; |
| 4378 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4380 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4381 | // 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] | 4382 | // 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] | 4383 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4384 | if (getLangOptions().LaxVectorConversions && |
| 4385 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4386 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4387 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4388 | } |
| 4389 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4390 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4391 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4392 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4393 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4394 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4395 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4396 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4397 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4398 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4399 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4400 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4401 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4402 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4403 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4404 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4405 | return Compatible; |
| 4406 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4407 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4408 | if (rhsType->getAs<BlockPointerType>()) { |
| 4409 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4410 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4411 | |
| 4412 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4413 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4414 | return Compatible; |
| 4415 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4416 | return Incompatible; |
| 4417 | } |
| 4418 | |
| 4419 | if (isa<BlockPointerType>(lhsType)) { |
| 4420 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4421 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4422 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4423 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4424 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4425 | return Compatible; |
| 4426 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4427 | if (rhsType->isBlockPointerType()) |
| 4428 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4429 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4430 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4431 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4432 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4433 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4434 | return Incompatible; |
| 4435 | } |
| 4436 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4437 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4438 | if (rhsType->isIntegerType()) |
| 4439 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4441 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4442 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4443 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4444 | return Compatible; |
| 4445 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4446 | } |
| 4447 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4448 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4449 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4450 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4451 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4452 | return Compatible; |
| 4453 | } |
| 4454 | // Treat block pointers as objects. |
| 4455 | if (rhsType->isBlockPointerType()) |
| 4456 | return Compatible; |
| 4457 | return Incompatible; |
| 4458 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4459 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4460 | // 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] | 4461 | if (lhsType == Context.BoolTy) |
| 4462 | return Compatible; |
| 4463 | |
| 4464 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4465 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4466 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4467 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4468 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4469 | |
| 4470 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4471 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4472 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4473 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4474 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4475 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4476 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4477 | if (lhsType == Context.BoolTy) |
| 4478 | return Compatible; |
| 4479 | |
| 4480 | if (lhsType->isIntegerType()) |
| 4481 | return PointerToInt; |
| 4482 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4483 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4484 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4485 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4486 | return Compatible; |
| 4487 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4488 | } |
| 4489 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4490 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4491 | return Compatible; |
| 4492 | return Incompatible; |
| 4493 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4494 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4495 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4496 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4497 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4498 | } |
| 4499 | return Incompatible; |
| 4500 | } |
| 4501 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4502 | /// \brief Constructs a transparent union from an expression that is |
| 4503 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4505 | QualType UnionType, FieldDecl *Field) { |
| 4506 | // Build an initializer list that designates the appropriate member |
| 4507 | // of the transparent union. |
| 4508 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 4509 | &E, 1, |
| 4510 | SourceLocation()); |
| 4511 | Initializer->setType(UnionType); |
| 4512 | Initializer->setInitializedFieldInUnion(Field); |
| 4513 | |
| 4514 | // Build a compound literal constructing a value of the transparent |
| 4515 | // union type from this initializer list. |
| 4516 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 4517 | false); |
| 4518 | } |
| 4519 | |
| 4520 | Sema::AssignConvertType |
| 4521 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4522 | QualType FromType = rExpr->getType(); |
| 4523 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | // 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] | 4525 | // transparent_union GCC extension. |
| 4526 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4527 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4528 | return Incompatible; |
| 4529 | |
| 4530 | // The field to initialize within the transparent union. |
| 4531 | RecordDecl *UD = UT->getDecl(); |
| 4532 | FieldDecl *InitField = 0; |
| 4533 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4534 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4535 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4536 | it != itend; ++it) { |
| 4537 | if (it->getType()->isPointerType()) { |
| 4538 | // If the transparent union contains a pointer type, we allow: |
| 4539 | // 1) void pointer |
| 4540 | // 2) null pointer constant |
| 4541 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4542 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4543 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4544 | InitField = *it; |
| 4545 | break; |
| 4546 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4548 | if (rExpr->isNullPointerConstant(Context, |
| 4549 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4550 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4551 | InitField = *it; |
| 4552 | break; |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4557 | == Compatible) { |
| 4558 | InitField = *it; |
| 4559 | break; |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | if (!InitField) |
| 4564 | return Incompatible; |
| 4565 | |
| 4566 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4567 | return Compatible; |
| 4568 | } |
| 4569 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4570 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4571 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4572 | if (getLangOptions().CPlusPlus) { |
| 4573 | if (!lhsType->isRecordType()) { |
| 4574 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4575 | // expression is implicitly converted (C++ 4) to the |
| 4576 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4577 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4578 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4579 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4580 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4584 | // structures. |
| 4585 | } |
| 4586 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4587 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4588 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | if ((lhsType->isPointerType() || |
| 4590 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4591 | lhsType->isBlockPointerType()) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4592 | && rExpr->isNullPointerConstant(Context, |
| 4593 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4594 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4595 | return Compatible; |
| 4596 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4597 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4598 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4599 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4600 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4601 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4602 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4603 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4604 | if (!lhsType->isReferenceType()) |
| 4605 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4606 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4607 | Sema::AssignConvertType result = |
| 4608 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4609 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4610 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4611 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4612 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4613 | // so that we can use references in built-in functions even in C. |
| 4614 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4615 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4616 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4617 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4618 | CastExpr::CK_Unknown); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4619 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4622 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4623 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4624 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4625 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4626 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4629 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4630 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4631 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4632 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4633 | QualType lhsType = |
| 4634 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4635 | QualType rhsType = |
| 4636 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4637 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4638 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4639 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4640 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4641 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4642 | // Handle the case of a vector & extvector type of the same size and element |
| 4643 | // 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] | 4644 | if (getLangOptions().LaxVectorConversions) { |
| 4645 | // FIXME: Should we warn here? |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4646 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4647 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4648 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4649 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4650 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4651 | } |
| 4652 | } |
| 4653 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4654 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4655 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4656 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4657 | bool swapped = false; |
| 4658 | if (rhsType->isExtVectorType()) { |
| 4659 | swapped = true; |
| 4660 | std::swap(rex, lex); |
| 4661 | std::swap(rhsType, lhsType); |
| 4662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4664 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4665 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4666 | QualType EltTy = LV->getElementType(); |
| 4667 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4668 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4669 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4670 | if (swapped) std::swap(rex, lex); |
| 4671 | return lhsType; |
| 4672 | } |
| 4673 | } |
| 4674 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4675 | rhsType->isRealFloatingType()) { |
| 4676 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4677 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4678 | if (swapped) std::swap(rex, lex); |
| 4679 | return lhsType; |
| 4680 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4681 | } |
| 4682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4684 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4685 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4686 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4687 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4688 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4691 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4693 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4694 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4695 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4696 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4697 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4698 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4699 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4700 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4705 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4706 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4707 | return CheckVectorOperands(Loc, lex, rex); |
| 4708 | return InvalidOperands(Loc, lex, rex); |
| 4709 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4710 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4711 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4712 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4713 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4714 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4715 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4720 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4721 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4722 | if (CompLHSTy) *CompLHSTy = compType; |
| 4723 | return compType; |
| 4724 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4725 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4726 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4727 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4728 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4729 | if (lex->getType()->isArithmeticType() && |
| 4730 | rex->getType()->isArithmeticType()) { |
| 4731 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4732 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4733 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4734 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4735 | // Put any potential pointer into PExp |
| 4736 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4737 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4738 | std::swap(PExp, IExp); |
| 4739 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4740 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4742 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4743 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4745 | // Check for arithmetic on pointers to incomplete types. |
| 4746 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4747 | if (getLangOptions().CPlusPlus) { |
| 4748 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4749 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4750 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4751 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4752 | |
| 4753 | // GNU extension: arithmetic on pointer to void |
| 4754 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4755 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4756 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4757 | if (getLangOptions().CPlusPlus) { |
| 4758 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4759 | << lex->getType() << lex->getSourceRange(); |
| 4760 | return QualType(); |
| 4761 | } |
| 4762 | |
| 4763 | // GNU extension: arithmetic on pointer to function |
| 4764 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4765 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4766 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4767 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4769 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4770 | PExp->getType()->isObjCObjectPointerType()) && |
| 4771 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4773 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4774 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4775 | return QualType(); |
| 4776 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4777 | // Diagnose bad cases where we step over interface counts. |
| 4778 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4779 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4780 | << PointeeTy << PExp->getSourceRange(); |
| 4781 | return QualType(); |
| 4782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4784 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4785 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4786 | if (LHSTy.isNull()) { |
| 4787 | LHSTy = lex->getType(); |
| 4788 | if (LHSTy->isPromotableIntegerType()) |
| 4789 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4790 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4791 | *CompLHSTy = LHSTy; |
| 4792 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4793 | return PExp->getType(); |
| 4794 | } |
| 4795 | } |
| 4796 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4797 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4798 | } |
| 4799 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4800 | // C99 6.5.6 |
| 4801 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4802 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4803 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4804 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4805 | if (CompLHSTy) *CompLHSTy = compType; |
| 4806 | return compType; |
| 4807 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4808 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4809 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4810 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4811 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4812 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4813 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4814 | if (lex->getType()->isArithmeticType() |
| 4815 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4816 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4817 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4820 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4821 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4822 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4824 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4825 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4826 | bool ComplainAboutVoid = false; |
| 4827 | Expr *ComplainAboutFunc = 0; |
| 4828 | if (lpointee->isVoidType()) { |
| 4829 | if (getLangOptions().CPlusPlus) { |
| 4830 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4831 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4832 | return QualType(); |
| 4833 | } |
| 4834 | |
| 4835 | // GNU C extension: arithmetic on pointer to void |
| 4836 | ComplainAboutVoid = true; |
| 4837 | } else if (lpointee->isFunctionType()) { |
| 4838 | if (getLangOptions().CPlusPlus) { |
| 4839 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4840 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4841 | return QualType(); |
| 4842 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4843 | |
| 4844 | // GNU C extension: arithmetic on pointer to function |
| 4845 | ComplainAboutFunc = lex; |
| 4846 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4848 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4850 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4851 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4852 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4853 | // Diagnose bad cases where we step over interface counts. |
| 4854 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4855 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4856 | << lpointee << lex->getSourceRange(); |
| 4857 | return QualType(); |
| 4858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4859 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4860 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4861 | if (rex->getType()->isIntegerType()) { |
| 4862 | if (ComplainAboutVoid) |
| 4863 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4864 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4865 | if (ComplainAboutFunc) |
| 4866 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4868 | << ComplainAboutFunc->getSourceRange(); |
| 4869 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4870 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4871 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4872 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4873 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4874 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4875 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4876 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4878 | // RHS must be a completely-type object type. |
| 4879 | // Handle the GNU void* extension. |
| 4880 | if (rpointee->isVoidType()) { |
| 4881 | if (getLangOptions().CPlusPlus) { |
| 4882 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4883 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4884 | return QualType(); |
| 4885 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4886 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4887 | ComplainAboutVoid = true; |
| 4888 | } else if (rpointee->isFunctionType()) { |
| 4889 | if (getLangOptions().CPlusPlus) { |
| 4890 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4891 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4892 | return QualType(); |
| 4893 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4894 | |
| 4895 | // GNU extension: arithmetic on pointer to function |
| 4896 | if (!ComplainAboutFunc) |
| 4897 | ComplainAboutFunc = rex; |
| 4898 | } else if (!rpointee->isDependentType() && |
| 4899 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4900 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4901 | << rex->getSourceRange() |
| 4902 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4903 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4904 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4905 | if (getLangOptions().CPlusPlus) { |
| 4906 | // Pointee types must be the same: C++ [expr.add] |
| 4907 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4908 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4909 | << lex->getType() << rex->getType() |
| 4910 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4911 | return QualType(); |
| 4912 | } |
| 4913 | } else { |
| 4914 | // Pointee types must be compatible C99 6.5.6p3 |
| 4915 | if (!Context.typesAreCompatible( |
| 4916 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4917 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4918 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4919 | << lex->getType() << rex->getType() |
| 4920 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4921 | return QualType(); |
| 4922 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4923 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4925 | if (ComplainAboutVoid) |
| 4926 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4927 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4928 | if (ComplainAboutFunc) |
| 4929 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4930 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4931 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4932 | |
| 4933 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4934 | return Context.getPointerDiffType(); |
| 4935 | } |
| 4936 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4937 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4938 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4941 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4942 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4943 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4944 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4945 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4946 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4947 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 4948 | // Vector shifts promote their scalar inputs to vector type. |
| 4949 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 4950 | return CheckVectorOperands(Loc, lex, rex); |
| 4951 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4952 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4953 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4954 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4955 | if (LHSTy.isNull()) { |
| 4956 | LHSTy = lex->getType(); |
| 4957 | if (LHSTy->isPromotableIntegerType()) |
| 4958 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4959 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4960 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4961 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4962 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4963 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4964 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4965 | // Sanity-check shift operands |
| 4966 | llvm::APSInt Right; |
| 4967 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 4968 | if (!rex->isValueDependent() && |
| 4969 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4970 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4971 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4972 | else { |
| 4973 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4974 | Context.getTypeSize(lex->getType())); |
| 4975 | if (Right.uge(LeftBits)) |
| 4976 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4977 | } |
| 4978 | } |
| 4979 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4980 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4981 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4984 | /// \brief Implements -Wsign-compare. |
| 4985 | /// |
| 4986 | /// \param lex the left-hand expression |
| 4987 | /// \param rex the right-hand expression |
| 4988 | /// \param OpLoc the location of the joining operator |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4989 | /// \param Equality whether this is an "equality-like" join, which |
| 4990 | /// suppresses the warning in some cases |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4991 | void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc, |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4992 | const PartialDiagnostic &PD, bool Equality) { |
John McCall | 7d62a8f | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 4993 | // Don't warn if we're in an unevaluated context. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 4994 | if (ExprEvalContexts.back().Context == Unevaluated) |
John McCall | 7d62a8f | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 4995 | return; |
| 4996 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4997 | QualType lt = lex->getType(), rt = rex->getType(); |
| 4998 | |
| 4999 | // Only warn if both operands are integral. |
| 5000 | if (!lt->isIntegerType() || !rt->isIntegerType()) |
| 5001 | return; |
| 5002 | |
Sebastian Redl | 732429c | 2009-11-05 21:09:23 +0000 | [diff] [blame] | 5003 | // If either expression is value-dependent, don't warn. We'll get another |
| 5004 | // chance at instantiation time. |
| 5005 | if (lex->isValueDependent() || rex->isValueDependent()) |
| 5006 | return; |
| 5007 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5008 | // The rule is that the signed operand becomes unsigned, so isolate the |
| 5009 | // signed operand. |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5010 | Expr *signedOperand, *unsignedOperand; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5011 | if (lt->isSignedIntegerType()) { |
| 5012 | if (rt->isSignedIntegerType()) return; |
| 5013 | signedOperand = lex; |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5014 | unsignedOperand = rex; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5015 | } else { |
| 5016 | if (!rt->isSignedIntegerType()) return; |
| 5017 | signedOperand = rex; |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5018 | unsignedOperand = lex; |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5021 | // If the unsigned type is strictly smaller than the signed type, |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 5022 | // then (1) the result type will be signed and (2) the unsigned |
| 5023 | // value will fit fully within the signed type, and thus the result |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5024 | // of the comparison will be exact. |
| 5025 | if (Context.getIntWidth(signedOperand->getType()) > |
| 5026 | Context.getIntWidth(unsignedOperand->getType())) |
| 5027 | return; |
| 5028 | |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5029 | // If the value is a non-negative integer constant, then the |
| 5030 | // signed->unsigned conversion won't change it. |
| 5031 | llvm::APSInt value; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 5032 | if (signedOperand->isIntegerConstantExpr(value, Context)) { |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5033 | assert(value.isSigned() && "result of signed expression not signed"); |
| 5034 | |
| 5035 | if (value.isNonNegative()) |
| 5036 | return; |
| 5037 | } |
| 5038 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5039 | if (Equality) { |
| 5040 | // For (in)equality comparisons, if the unsigned operand is a |
John McCall | 48f5e63 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 5041 | // constant which cannot collide with a overflowed signed operand, |
| 5042 | // then reinterpreting the signed operand as unsigned will not |
| 5043 | // change the result of the comparison. |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5044 | if (unsignedOperand->isIntegerConstantExpr(value, Context)) { |
| 5045 | assert(!value.isSigned() && "result of unsigned expression is signed"); |
| 5046 | |
| 5047 | // 2's complement: test the top bit. |
| 5048 | if (value.isNonNegative()) |
| 5049 | return; |
| 5050 | } |
| 5051 | } |
| 5052 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 5053 | Diag(OpLoc, PD) |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5054 | << lex->getType() << rex->getType() |
| 5055 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5056 | } |
| 5057 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5058 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5059 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5060 | unsigned OpaqueOpc, bool isRelational) { |
| 5061 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 5062 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5063 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5064 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5065 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5066 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5067 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 5068 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5069 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5070 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 5071 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5072 | UsualArithmeticConversions(lex, rex); |
| 5073 | else { |
| 5074 | UsualUnaryConversions(lex); |
| 5075 | UsualUnaryConversions(rex); |
| 5076 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5077 | QualType lType = lex->getType(); |
| 5078 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5079 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5080 | if (!lType->isFloatingType() |
| 5081 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5082 | // For non-floating point types, check for self-comparisons of the form |
| 5083 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5084 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5085 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 5086 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5087 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5088 | Expr *RHSStripped = rex->IgnoreParens(); |
| 5089 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 5090 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 5091 | if (DRL->getDecl() == DRR->getDecl() && |
| 5092 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5093 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5094 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5095 | if (isa<CastExpr>(LHSStripped)) |
| 5096 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5097 | if (isa<CastExpr>(RHSStripped)) |
| 5098 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5099 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5100 | // Warn about comparisons against a string constant (unless the other |
| 5101 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5102 | Expr *literalString = 0; |
| 5103 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5104 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5105 | !RHSStripped->isNullPointerConstant(Context, |
| 5106 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5107 | literalString = lex; |
| 5108 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5109 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5110 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5111 | !LHSStripped->isNullPointerConstant(Context, |
| 5112 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5113 | literalString = rex; |
| 5114 | literalStringStripped = RHSStripped; |
| 5115 | } |
| 5116 | |
| 5117 | if (literalString) { |
| 5118 | std::string resultComparison; |
| 5119 | switch (Opc) { |
| 5120 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 5121 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 5122 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 5123 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 5124 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 5125 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 5126 | default: assert(false && "Invalid comparison operator"); |
| 5127 | } |
| 5128 | Diag(Loc, diag::warn_stringcompare) |
| 5129 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 5130 | << literalString->getSourceRange() |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5131 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 5132 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 5133 | "strcmp(") |
| 5134 | << CodeModificationHint::CreateInsertion( |
| 5135 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5136 | resultComparison); |
| 5137 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5138 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5140 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5141 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5142 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5143 | if (isRelational) { |
| 5144 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5145 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5146 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5147 | // Check for comparisons of floating point operands using != and ==. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5148 | if (lType->isFloatingType() && rType->isFloatingType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5149 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5150 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5151 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5152 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5153 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5154 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5155 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 5156 | Expr::NPC_ValueDependentIsNull); |
| 5157 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 5158 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5159 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5160 | // All of the following pointer related warnings are GCC extensions, except |
| 5161 | // when handling null pointer constants. One day, we can consider making them |
| 5162 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5163 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5164 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5165 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5166 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5167 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5169 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5170 | if (LCanPointeeTy == RCanPointeeTy) |
| 5171 | return ResultTy; |
| 5172 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5173 | // C++ [expr.rel]p2: |
| 5174 | // [...] Pointer conversions (4.10) and qualification |
| 5175 | // conversions (4.4) are performed on pointer operands (or on |
| 5176 | // a pointer operand and a null pointer constant) to bring |
| 5177 | // them to their composite pointer type. [...] |
| 5178 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5179 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5180 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 5181 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5182 | if (T.isNull()) { |
| 5183 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5184 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5185 | return QualType(); |
| 5186 | } |
| 5187 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5188 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5189 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5190 | return ResultTy; |
| 5191 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5192 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5193 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5194 | RCanPointeeTy.getUnqualifiedType())) { |
| 5195 | // Valid unless a relational comparison of function pointers |
| 5196 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5197 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5198 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5199 | } |
| 5200 | } else if (!isRelational && |
| 5201 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5202 | // Valid unless comparison between non-null pointer and function pointer |
| 5203 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5204 | && !LHSIsNull && !RHSIsNull) { |
| 5205 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5206 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5207 | } |
| 5208 | } else { |
| 5209 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5210 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5211 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5212 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5213 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5214 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5215 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5217 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5218 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5219 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5220 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5222 | (lType->isPointerType() || |
| 5223 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5224 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5225 | return ResultTy; |
| 5226 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5227 | if (LHSIsNull && |
| 5228 | (rType->isPointerType() || |
| 5229 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5230 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5231 | return ResultTy; |
| 5232 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5233 | |
| 5234 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5235 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5236 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5237 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | // In addition, pointers to members can be compared, or a pointer to |
| 5239 | // member and a null pointer constant. Pointer to member conversions |
| 5240 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5241 | // them to a common type. If one operand is a null pointer constant, |
| 5242 | // the common type is the type of the other operand. Otherwise, the |
| 5243 | // common type is a pointer to member type similar (4.4) to the type |
| 5244 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5245 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5246 | // types. |
| 5247 | QualType T = FindCompositePointerType(lex, rex); |
| 5248 | if (T.isNull()) { |
| 5249 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5250 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5251 | return QualType(); |
| 5252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5254 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5255 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5256 | return ResultTy; |
| 5257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5259 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5260 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5261 | return ResultTy; |
| 5262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5263 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5264 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5265 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5266 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5267 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5268 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5269 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5270 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5271 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5272 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5273 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5274 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5275 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5276 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5277 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5278 | if (!isRelational |
| 5279 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5280 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5281 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5282 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5283 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5284 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5285 | ->getPointeeType()->isVoidType()))) |
| 5286 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5287 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5288 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5289 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5290 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5291 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5292 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5293 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5294 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5295 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5296 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5297 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5298 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5299 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5300 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5301 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5302 | if (!LPtrToVoid && !RPtrToVoid && |
| 5303 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5304 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5305 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5306 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5307 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5308 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5309 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5310 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5311 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5312 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5313 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5314 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5315 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5316 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5317 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5318 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5319 | unsigned DiagID = 0; |
| 5320 | if (RHSIsNull) { |
| 5321 | if (isRelational) |
| 5322 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5323 | } else if (isRelational) |
| 5324 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5325 | else |
| 5326 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5328 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5329 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5330 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5331 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5332 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5333 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5334 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5335 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5336 | unsigned DiagID = 0; |
| 5337 | if (LHSIsNull) { |
| 5338 | if (isRelational) |
| 5339 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5340 | } else if (isRelational) |
| 5341 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5342 | else |
| 5343 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5345 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5346 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5347 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5348 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5349 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5350 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5351 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5352 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5353 | if (!isRelational && RHSIsNull |
| 5354 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5355 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5356 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5357 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5358 | if (!isRelational && LHSIsNull |
| 5359 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5360 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5361 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5362 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5363 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5366 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5367 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5368 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5369 | /// types. |
| 5370 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5371 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5372 | bool isRelational) { |
| 5373 | // Check to make sure we're operating on vectors of the same type and width, |
| 5374 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5375 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5376 | if (vType.isNull()) |
| 5377 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5378 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5379 | QualType lType = lex->getType(); |
| 5380 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5381 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5382 | // For non-floating point types, check for self-comparisons of the form |
| 5383 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5384 | // often indicate logic errors in the program. |
| 5385 | if (!lType->isFloatingType()) { |
| 5386 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5387 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5388 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5389 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5390 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5391 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5392 | // Check for comparisons of floating point operands using != and ==. |
| 5393 | if (!isRelational && lType->isFloatingType()) { |
| 5394 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5395 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5396 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5397 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5398 | // Return the type for the comparison, which is the same as vector type for |
| 5399 | // integer vectors, or an integer type of identical size and number of |
| 5400 | // elements for floating point vectors. |
| 5401 | if (lType->isIntegerType()) |
| 5402 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5403 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5404 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5405 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5406 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5407 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5408 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5409 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5410 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5411 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5412 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5413 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5414 | } |
| 5415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5416 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 5418 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5419 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5420 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5421 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5422 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5423 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5424 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5425 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5430 | if (!Context.getLangOptions().CPlusPlus) { |
| 5431 | UsualUnaryConversions(lex); |
| 5432 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5433 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5434 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5435 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5436 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5437 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5438 | } |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5439 | |
| 5440 | // C++ [expr.log.and]p1 |
| 5441 | // C++ [expr.log.or]p1 |
| 5442 | // The operands are both implicitly converted to type bool (clause 4). |
| 5443 | StandardConversionSequence LHS; |
| 5444 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 5445 | /*InOverloadResolution=*/false, LHS)) |
| 5446 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5447 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5448 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5449 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5450 | return InvalidOperands(Loc, lex, rex); |
| 5451 | |
| 5452 | StandardConversionSequence RHS; |
| 5453 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 5454 | /*InOverloadResolution=*/false, RHS)) |
| 5455 | return InvalidOperands(Loc, lex, rex); |
| 5456 | |
| 5457 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5458 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5459 | return InvalidOperands(Loc, lex, rex); |
| 5460 | |
| 5461 | // C++ [expr.log.and]p2 |
| 5462 | // C++ [expr.log.or]p2 |
| 5463 | // The result is a bool. |
| 5464 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5467 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5468 | /// is a read-only property; return true if so. A readonly property expression |
| 5469 | /// depends on various declarations and thus must be treated specially. |
| 5470 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5472 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5473 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5474 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5475 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5476 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5477 | BaseType->getAsObjCInterfacePointerType()) |
| 5478 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5479 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5480 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | return false; |
| 5484 | } |
| 5485 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5486 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5487 | /// emit an error and return true. If so, return false. |
| 5488 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5489 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5490 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5491 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5492 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5493 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5494 | if (IsLV == Expr::MLV_Valid) |
| 5495 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5496 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5497 | unsigned Diag = 0; |
| 5498 | bool NeedType = false; |
| 5499 | switch (IsLV) { // C99 6.5.16p2 |
| 5500 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 5501 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5502 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5503 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5504 | NeedType = true; |
| 5505 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5506 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5507 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5508 | NeedType = true; |
| 5509 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5510 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5511 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5512 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5513 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5514 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5515 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5516 | case Expr::MLV_IncompleteType: |
| 5517 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5518 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5519 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 5520 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5521 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5522 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5523 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5524 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5525 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5526 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5527 | case Expr::MLV_ReadonlyProperty: |
| 5528 | Diag = diag::error_readonly_property_assignment; |
| 5529 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5530 | case Expr::MLV_NoSetterProperty: |
| 5531 | Diag = diag::error_nosetter_property_assignment; |
| 5532 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5533 | case Expr::MLV_SubObjCPropertySetting: |
| 5534 | Diag = diag::error_no_subobject_property_setting; |
| 5535 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5536 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5537 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5538 | SourceRange Assign; |
| 5539 | if (Loc != OrigLoc) |
| 5540 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5541 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5542 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5543 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5544 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5545 | return true; |
| 5546 | } |
| 5547 | |
| 5548 | |
| 5549 | |
| 5550 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5551 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5552 | SourceLocation Loc, |
| 5553 | QualType CompoundType) { |
| 5554 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5555 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5556 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5557 | |
| 5558 | QualType LHSType = LHS->getType(); |
| 5559 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5560 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5561 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5562 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5563 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5564 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5565 | // Special case of NSObject attributes on c-style pointer types. |
| 5566 | if (ConvTy == IncompatiblePointer && |
| 5567 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5568 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5569 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5570 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5571 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5572 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5573 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5574 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5575 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5576 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5577 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5578 | RHSCheck = ICE->getSubExpr(); |
| 5579 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5580 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5581 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5582 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5583 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5584 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5585 | // And there is a space or other character before the subexpr of the |
| 5586 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5587 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5588 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5589 | Diag(Loc, diag::warn_not_compound_assign) |
| 5590 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5591 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5592 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5593 | } |
| 5594 | } else { |
| 5595 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5596 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5597 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5598 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5599 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5600 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5601 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5603 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5604 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5605 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5606 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5607 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5608 | // 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] | 5609 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5610 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5613 | // C99 6.5.17 |
| 5614 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5615 | // 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] | 5616 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5617 | |
| 5618 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5619 | // incomplete in C++). |
| 5620 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5621 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5624 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5625 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5626 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5627 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5628 | if (Op->isTypeDependent()) |
| 5629 | return Context.DependentTy; |
| 5630 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5631 | QualType ResType = Op->getType(); |
| 5632 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5633 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5634 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5635 | // Decrement of bool is not allowed. |
| 5636 | if (!isInc) { |
| 5637 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5638 | return QualType(); |
| 5639 | } |
| 5640 | // Increment of bool sets it to true, but is deprecated. |
| 5641 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5642 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5643 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5644 | } else if (ResType->isAnyPointerType()) { |
| 5645 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5646 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5647 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5648 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5649 | if (getLangOptions().CPlusPlus) { |
| 5650 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5651 | << Op->getSourceRange(); |
| 5652 | return QualType(); |
| 5653 | } |
| 5654 | |
| 5655 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5656 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5657 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5658 | if (getLangOptions().CPlusPlus) { |
| 5659 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5660 | << Op->getType() << Op->getSourceRange(); |
| 5661 | return QualType(); |
| 5662 | } |
| 5663 | |
| 5664 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5665 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5666 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5667 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5668 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5669 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5670 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5671 | // Diagnose bad cases where we step over interface counts. |
| 5672 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5673 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5674 | << PointeeTy << Op->getSourceRange(); |
| 5675 | return QualType(); |
| 5676 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5677 | } else if (ResType->isComplexType()) { |
| 5678 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5679 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5680 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5681 | } else { |
| 5682 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 5683 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5684 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5685 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5686 | // 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] | 5687 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5688 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5689 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5690 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5693 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5694 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5695 | /// where the declaration is needed for type checking. We only need to |
| 5696 | /// handle cases when the expression references a function designator |
| 5697 | /// or is an lvalue. Here are some examples: |
| 5698 | /// - &(x) => x |
| 5699 | /// - &*****f => f for f a function designator. |
| 5700 | /// - &s.xx => s |
| 5701 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5702 | /// - *(x + 1) -> x, if x is an array |
| 5703 | /// - &"123"[2] -> 0 |
| 5704 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5705 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5706 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5707 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5708 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5709 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5710 | // If this is an arrow operator, the address is an offset from |
| 5711 | // the base's value, so the object the base refers to is |
| 5712 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5713 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5714 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5715 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5716 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5717 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5718 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5719 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5720 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5721 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5722 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5723 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5724 | } |
| 5725 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5726 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5727 | case Stmt::UnaryOperatorClass: { |
| 5728 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5729 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5730 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5731 | case UnaryOperator::Real: |
| 5732 | case UnaryOperator::Imag: |
| 5733 | case UnaryOperator::Extension: |
| 5734 | return getPrimaryDecl(UO->getSubExpr()); |
| 5735 | default: |
| 5736 | return 0; |
| 5737 | } |
| 5738 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5739 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5740 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5741 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5742 | // If the result of an implicit cast is an l-value, we care about |
| 5743 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5744 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5745 | default: |
| 5746 | return 0; |
| 5747 | } |
| 5748 | } |
| 5749 | |
| 5750 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5751 | /// 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] | 5752 | /// 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] | 5753 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5754 | /// 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] | 5755 | /// 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] | 5756 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5757 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5758 | // Make sure to ignore parentheses in subsequent checks |
| 5759 | op = op->IgnoreParens(); |
| 5760 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5761 | if (op->isTypeDependent()) |
| 5762 | return Context.DependentTy; |
| 5763 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5764 | if (getLangOptions().C99) { |
| 5765 | // Implement C99-only parts of addressof rules. |
| 5766 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5767 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5768 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5769 | // (assuming the deref expression is valid). |
| 5770 | return uOp->getSubExpr()->getType(); |
| 5771 | } |
| 5772 | // Technically, there should be a check for array subscript |
| 5773 | // expressions here, but the result of one is always an lvalue anyway. |
| 5774 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5775 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5776 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5777 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5778 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5779 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5780 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5781 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5782 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5783 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5784 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5785 | return QualType(); |
| 5786 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5787 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5788 | // The operand cannot be a bit-field |
| 5789 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5790 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5791 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5792 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5793 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5794 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5795 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5796 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5797 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5798 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5799 | // cannot take address of a property expression. |
| 5800 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5801 | << "property expression" << op->getSourceRange(); |
| 5802 | return QualType(); |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5803 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5804 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 474e102 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5805 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5806 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5807 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5808 | return Context.OverloadTy; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5809 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5810 | // 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] | 5811 | // with the register storage-class specifier. |
| 5812 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5813 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5814 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5815 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5816 | return QualType(); |
| 5817 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5818 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5819 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5820 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5821 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5822 | // Could be a pointer to member, though, if there is an explicit |
| 5823 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5824 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5825 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5826 | if (Ctx && Ctx->isRecord()) { |
| 5827 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5828 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5829 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5830 | << FD->getDeclName() << FD->getType(); |
| 5831 | return QualType(); |
| 5832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5833 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5834 | return Context.getMemberPointerType(op->getType(), |
| 5835 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5836 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5837 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5838 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5839 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5840 | // As above. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5841 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 5842 | MD->isInstance()) |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5843 | return Context.getMemberPointerType(op->getType(), |
| 5844 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5845 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5846 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5847 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5848 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5849 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5850 | // Taking the address of a void variable is technically illegal, but we |
| 5851 | // allow it in cases which are otherwise valid. |
| 5852 | // Example: "extern void x; void* y = &x;". |
| 5853 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5854 | } |
| 5855 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5856 | // If the operand has type "type", the result has type "pointer to type". |
| 5857 | return Context.getPointerType(op->getType()); |
| 5858 | } |
| 5859 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5860 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5861 | if (Op->isTypeDependent()) |
| 5862 | return Context.DependentTy; |
| 5863 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5864 | UsualUnaryConversions(Op); |
| 5865 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5866 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5867 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5868 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5869 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5870 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5871 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5872 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5873 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5874 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5875 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5876 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5877 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5878 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5879 | return QualType(); |
| 5880 | } |
| 5881 | |
| 5882 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5883 | tok::TokenKind Kind) { |
| 5884 | BinaryOperator::Opcode Opc; |
| 5885 | switch (Kind) { |
| 5886 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5887 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5888 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5889 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5890 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5891 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5892 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5893 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5894 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5895 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5896 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5897 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5898 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5899 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5900 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5901 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5902 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5903 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5904 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5905 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5906 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5907 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5908 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5909 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5910 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5911 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5912 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5913 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5914 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5915 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5916 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5917 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5918 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5919 | } |
| 5920 | return Opc; |
| 5921 | } |
| 5922 | |
| 5923 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5924 | tok::TokenKind Kind) { |
| 5925 | UnaryOperator::Opcode Opc; |
| 5926 | switch (Kind) { |
| 5927 | default: assert(0 && "Unknown unary op!"); |
| 5928 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5929 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5930 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5931 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5932 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5933 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5934 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5935 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5936 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5937 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 5938 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 5939 | } |
| 5940 | return Opc; |
| 5941 | } |
| 5942 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5943 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5944 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5945 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5946 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5947 | unsigned Op, |
| 5948 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5949 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5950 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5951 | // The following two variables are used for compound assignment operators |
| 5952 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5953 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5954 | |
| 5955 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5956 | case BinaryOperator::Assign: |
| 5957 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5958 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5959 | case BinaryOperator::PtrMemD: |
| 5960 | case BinaryOperator::PtrMemI: |
| 5961 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5962 | Opc == BinaryOperator::PtrMemI); |
| 5963 | break; |
| 5964 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5965 | case BinaryOperator::Div: |
| 5966 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5967 | break; |
| 5968 | case BinaryOperator::Rem: |
| 5969 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5970 | break; |
| 5971 | case BinaryOperator::Add: |
| 5972 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5973 | break; |
| 5974 | case BinaryOperator::Sub: |
| 5975 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5976 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5977 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5978 | case BinaryOperator::Shr: |
| 5979 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5980 | break; |
| 5981 | case BinaryOperator::LE: |
| 5982 | case BinaryOperator::LT: |
| 5983 | case BinaryOperator::GE: |
| 5984 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5985 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5986 | break; |
| 5987 | case BinaryOperator::EQ: |
| 5988 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5989 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5990 | break; |
| 5991 | case BinaryOperator::And: |
| 5992 | case BinaryOperator::Xor: |
| 5993 | case BinaryOperator::Or: |
| 5994 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5995 | break; |
| 5996 | case BinaryOperator::LAnd: |
| 5997 | case BinaryOperator::LOr: |
| 5998 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5999 | break; |
| 6000 | case BinaryOperator::MulAssign: |
| 6001 | case BinaryOperator::DivAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6002 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 6003 | CompLHSTy = CompResultTy; |
| 6004 | if (!CompResultTy.isNull()) |
| 6005 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6006 | break; |
| 6007 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6008 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6009 | CompLHSTy = CompResultTy; |
| 6010 | if (!CompResultTy.isNull()) |
| 6011 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6012 | break; |
| 6013 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6014 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6015 | if (!CompResultTy.isNull()) |
| 6016 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6017 | break; |
| 6018 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6019 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6020 | if (!CompResultTy.isNull()) |
| 6021 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6022 | break; |
| 6023 | case BinaryOperator::ShlAssign: |
| 6024 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6025 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6026 | CompLHSTy = CompResultTy; |
| 6027 | if (!CompResultTy.isNull()) |
| 6028 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6029 | break; |
| 6030 | case BinaryOperator::AndAssign: |
| 6031 | case BinaryOperator::XorAssign: |
| 6032 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6033 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6034 | CompLHSTy = CompResultTy; |
| 6035 | if (!CompResultTy.isNull()) |
| 6036 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6037 | break; |
| 6038 | case BinaryOperator::Comma: |
| 6039 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6040 | break; |
| 6041 | } |
| 6042 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6043 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6044 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6045 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6046 | else |
| 6047 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6048 | CompLHSTy, CompResultTy, |
| 6049 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6052 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6053 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6054 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6055 | const PartialDiagnostic &PD, |
| 6056 | SourceRange ParenRange) |
| 6057 | { |
| 6058 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6059 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6060 | // We can't display the parentheses, so just dig the |
| 6061 | // warning/error and return. |
| 6062 | Self.Diag(Loc, PD); |
| 6063 | return; |
| 6064 | } |
| 6065 | |
| 6066 | Self.Diag(Loc, PD) |
| 6067 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6068 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
| 6069 | } |
| 6070 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6071 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6072 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6073 | /// comparison operators have higher precedence. The most typical example of |
| 6074 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6075 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6076 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6077 | typedef BinaryOperator BinOp; |
| 6078 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6079 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6080 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6081 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6082 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6083 | rhsopc = BO->getOpcode(); |
| 6084 | |
| 6085 | // Subs are not binary operators. |
| 6086 | if (lhsopc == -1 && rhsopc == -1) |
| 6087 | return; |
| 6088 | |
| 6089 | // Bitwise operations are sometimes used as eager logical ops. |
| 6090 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6091 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6092 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6093 | return; |
| 6094 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6095 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6096 | SuggestParentheses(Self, OpLoc, |
| 6097 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6098 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6099 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
| 6100 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 6101 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6102 | SuggestParentheses(Self, OpLoc, |
| 6103 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6104 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6105 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
| 6106 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6110 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6111 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 6112 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6113 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6114 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6115 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6116 | } |
| 6117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6118 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6119 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 6120 | tok::TokenKind Kind, |
| 6121 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6122 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6123 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6124 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6125 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6126 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6127 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6128 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6129 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6130 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6131 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6132 | } |
| 6133 | |
| 6134 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 6135 | BinaryOperator::Opcode Opc, |
| 6136 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6137 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6138 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6139 | rhs->getType()->isOverloadableType())) { |
| 6140 | // Find all of the overloaded operators visible from this |
| 6141 | // point. We perform both an operator-name lookup from the local |
| 6142 | // scope and an argument-dependent lookup based on the types of |
| 6143 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6144 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6145 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 6146 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6147 | if (S) |
| 6148 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6149 | Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6150 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6151 | DeclarationName OpName |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6152 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6153 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6154 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6155 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6156 | // Build the (potentially-overloaded, potentially-dependent) |
| 6157 | // binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6158 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6159 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6161 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6162 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6165 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6166 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6167 | ExprArg InputArg) { |
| 6168 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6169 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6170 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6171 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6172 | QualType resultType; |
| 6173 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6174 | case UnaryOperator::OffsetOf: |
| 6175 | assert(false && "Invalid unary operator"); |
| 6176 | break; |
| 6177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6178 | case UnaryOperator::PreInc: |
| 6179 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6180 | case UnaryOperator::PostInc: |
| 6181 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6182 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6183 | Opc == UnaryOperator::PreInc || |
| 6184 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6185 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6186 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6187 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 6188 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6189 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 6190 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6191 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 6192 | break; |
| 6193 | case UnaryOperator::Plus: |
| 6194 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6195 | UsualUnaryConversions(Input); |
| 6196 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6197 | if (resultType->isDependentType()) |
| 6198 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6199 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 6200 | break; |
| 6201 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6202 | resultType->isEnumeralType()) |
| 6203 | break; |
| 6204 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 6205 | Opc == UnaryOperator::Plus && |
| 6206 | resultType->isPointerType()) |
| 6207 | break; |
| 6208 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6209 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6210 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6211 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6212 | UsualUnaryConversions(Input); |
| 6213 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6214 | if (resultType->isDependentType()) |
| 6215 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6216 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6217 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6218 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6219 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6220 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6221 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6222 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6223 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6224 | break; |
| 6225 | case UnaryOperator::LNot: // logical negation |
| 6226 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6227 | DefaultFunctionArrayConversion(Input); |
| 6228 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6229 | if (resultType->isDependentType()) |
| 6230 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6231 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6232 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6233 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6234 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6235 | // In C++, it's bool. C++ 5.3.1p8 |
| 6236 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6237 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6238 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6239 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 6240 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6241 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6242 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6243 | resultType = Input->getType(); |
| 6244 | break; |
| 6245 | } |
| 6246 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6247 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6248 | |
| 6249 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6250 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6253 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6254 | UnaryOperator::Opcode Opc, |
| 6255 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6256 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6257 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 6258 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 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. |
| 6263 | FunctionSet Functions; |
| 6264 | OverloadedOperatorKind OverOp = UnaryOperator::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, Input->getType(), QualType(), |
| 6268 | Functions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | DeclarationName OpName |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6270 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6271 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6272 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6274 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 6275 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6276 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6277 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 6278 | } |
| 6279 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6280 | // Unary Operators. 'Tok' is the token for the operator. |
| 6281 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6282 | tok::TokenKind Op, ExprArg input) { |
| 6283 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 6284 | } |
| 6285 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6286 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6287 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 6288 | SourceLocation LabLoc, |
| 6289 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6290 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 6291 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6292 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6293 | // If we haven't seen this label yet, create a forward reference. It |
| 6294 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6295 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6296 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6298 | // 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] | 6299 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6300 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6303 | Sema::OwningExprResult |
| 6304 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 6305 | SourceLocation RPLoc) { // "({..})" |
| 6306 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6307 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6308 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6309 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6310 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6311 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6312 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6313 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6314 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6315 | // example, it is not possible to goto into a stmt expression apparently. |
| 6316 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6317 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6318 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6319 | // as the type of the stmtexpr. |
| 6320 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6321 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6322 | if (!Compound->body_empty()) { |
| 6323 | Stmt *LastStmt = Compound->body_back(); |
| 6324 | // If LastStmt is a label, skip down through into the body. |
| 6325 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6326 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6327 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6328 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6329 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6330 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6331 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6332 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6333 | // expressions are not lvalues. |
| 6334 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6335 | substmt.release(); |
| 6336 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6337 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6338 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6339 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 6340 | SourceLocation BuiltinLoc, |
| 6341 | SourceLocation TypeLoc, |
| 6342 | TypeTy *argty, |
| 6343 | OffsetOfComponent *CompPtr, |
| 6344 | unsigned NumComponents, |
| 6345 | SourceLocation RPLoc) { |
| 6346 | // FIXME: This function leaks all expressions in the offset components on |
| 6347 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6348 | // FIXME: Preserve type source info. |
| 6349 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6350 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6351 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6352 | bool Dependent = ArgTy->isDependentType(); |
| 6353 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6354 | // We must have at least one component that refers to the type, and the first |
| 6355 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6356 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6357 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6358 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6359 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6360 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 6361 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 6362 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6363 | // Otherwise, create a null pointer as the base, and iteratively process |
| 6364 | // the offsetof designators. |
| 6365 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 6366 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6367 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6368 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 6369 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6370 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6371 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6372 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6373 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6374 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6375 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6376 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6377 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6378 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 6379 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6380 | |
John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 6381 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 6382 | diag::err_offsetof_incomplete_type)) |
| 6383 | return ExprError(); |
| 6384 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6385 | // FIXME: Dependent case loses a lot of information here. And probably |
| 6386 | // leaks like a sieve. |
| 6387 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6388 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6389 | if (OC.isBrackets) { |
| 6390 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6391 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 6392 | if (!AT) { |
| 6393 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6394 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6395 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 6399 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6400 | // Promote the array so it looks more like a normal array subscript |
| 6401 | // expression. |
| 6402 | DefaultFunctionArrayConversion(Res); |
| 6403 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6404 | // C99 6.5.2.1p1 |
| 6405 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6406 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6407 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6408 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 6409 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6410 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6411 | |
| 6412 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 6413 | OC.LocEnd); |
| 6414 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6415 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6416 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6417 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6418 | if (!RC) { |
| 6419 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6420 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6421 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6422 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 6423 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6424 | // Get the decl corresponding to this. |
| 6425 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6426 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 6427 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 6428 | switch (ExprEvalContexts.back().Context ) { |
| 6429 | case Unevaluated: |
| 6430 | // The argument will never be evaluated, so don't complain. |
| 6431 | break; |
| 6432 | |
| 6433 | case PotentiallyEvaluated: |
| 6434 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 6435 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6436 | << Res->getType()); |
| 6437 | DidWarnAboutNonPOD = true; |
| 6438 | break; |
| 6439 | |
| 6440 | case PotentiallyPotentiallyEvaluated: |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 6441 | ExprEvalContexts.back().addDiagnostic(BuiltinLoc, |
| 6442 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6443 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6444 | << Res->getType()); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 6445 | DidWarnAboutNonPOD = true; |
| 6446 | break; |
| 6447 | } |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 6448 | } |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6450 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6451 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6452 | LookupQualifiedName(R, RD); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6453 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6454 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6455 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6456 | if (!MemberDecl) |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6457 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6458 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6459 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6460 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 6461 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6462 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 6463 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 09b6d0e | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 6464 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6465 | } else { |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 6466 | PerformObjectMemberConversion(Res, MemberDecl); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6467 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 6468 | // doesn't matter here. |
| 6469 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 6470 | MemberDecl->getType().getNonReferenceType()); |
| 6471 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6472 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6473 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6474 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6475 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 6476 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6480 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 6481 | TypeTy *arg1,TypeTy *arg2, |
| 6482 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6483 | // FIXME: Preserve type source info. |
| 6484 | QualType argT1 = GetTypeFromParser(arg1); |
| 6485 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6486 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6487 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6488 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 6489 | if (getLangOptions().CPlusPlus) { |
| 6490 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 6491 | << SourceRange(BuiltinLoc, RPLoc); |
| 6492 | return ExprError(); |
| 6493 | } |
| 6494 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6495 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 6496 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6499 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 6500 | ExprArg cond, |
| 6501 | ExprArg expr1, ExprArg expr2, |
| 6502 | SourceLocation RPLoc) { |
| 6503 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 6504 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 6505 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6506 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6507 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 6508 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6509 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6510 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 6511 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6512 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6513 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6514 | } else { |
| 6515 | // The conditional expression is required to be a constant expression. |
| 6516 | llvm::APSInt condEval(32); |
| 6517 | SourceLocation ExpLoc; |
| 6518 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6519 | return ExprError(Diag(ExpLoc, |
| 6520 | diag::err_typecheck_choose_expr_requires_constant) |
| 6521 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6522 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6523 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 6524 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6525 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 6526 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6529 | cond.release(); expr1.release(); expr2.release(); |
| 6530 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6531 | resType, RPLoc, |
| 6532 | resType->isDependentType(), |
| 6533 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6536 | //===----------------------------------------------------------------------===// |
| 6537 | // Clang Extensions. |
| 6538 | //===----------------------------------------------------------------------===// |
| 6539 | |
| 6540 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6541 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6542 | // Analyze block parameters. |
| 6543 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6544 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6545 | // Add BSI to CurBlock. |
| 6546 | BSI->PrevBlockInfo = CurBlock; |
| 6547 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6548 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6549 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6550 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 6551 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 6552 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6553 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 6554 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6555 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6556 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Ted Kremenek | 3cdff23 | 2009-12-07 22:01:30 +0000 | [diff] [blame] | 6557 | CurContext->addDecl(BSI->TheDecl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6558 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6561 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6562 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6563 | |
| 6564 | if (ParamInfo.getNumTypeObjects() == 0 |
| 6565 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6566 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6567 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 6568 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6569 | if (T->isArrayType()) { |
| 6570 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6571 | diag::err_block_returns_array); |
| 6572 | return; |
| 6573 | } |
| 6574 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6575 | // The parameter list is optional, if there was none, assume (). |
| 6576 | if (!T->isFunctionType()) |
| 6577 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6578 | |
| 6579 | CurBlock->hasPrototype = true; |
| 6580 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6581 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6582 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6583 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6584 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6585 | // FIXME: remove the attribute. |
| 6586 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6587 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6588 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6589 | // Do not allow returning a objc interface by-value. |
| 6590 | if (RetTy->isObjCInterfaceType()) { |
| 6591 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6592 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6593 | return; |
| 6594 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6595 | return; |
| 6596 | } |
| 6597 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6598 | // Analyze arguments to block. |
| 6599 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6600 | "Not a function declarator!"); |
| 6601 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6602 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6603 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6604 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6605 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6606 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6607 | // no arguments, not a function that takes a single void argument. |
| 6608 | if (FTI.hasPrototype && |
| 6609 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6610 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6611 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6612 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6613 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6614 | } else if (FTI.hasPrototype) { |
| 6615 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6616 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6617 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6618 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6619 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6620 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6621 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6622 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6623 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6624 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6625 | // If this has an identifier, add it to the scope stack. |
| 6626 | if ((*AI)->getIdentifier()) |
| 6627 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6628 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6629 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6630 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6631 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6632 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6633 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6634 | // FIXME: remove the attribute. |
| 6635 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6636 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6637 | // Analyze the return type. |
| 6638 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6639 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6640 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6641 | // Do not allow returning a objc interface by-value. |
| 6642 | if (RetTy->isObjCInterfaceType()) { |
| 6643 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6644 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6645 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6646 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
| 6649 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6650 | /// is invoked to pop the information about the block from the action impl. |
| 6651 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6652 | // Ensure that CurBlock is deleted. |
| 6653 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6654 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6655 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6656 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6657 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6658 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6659 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6660 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6664 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6665 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6666 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6667 | // If blocks are disabled, emit an error. |
| 6668 | if (!LangOpts.Blocks) |
| 6669 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6671 | // Ensure that CurBlock is deleted. |
| 6672 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6673 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6674 | PopDeclContext(); |
| 6675 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6676 | // Pop off CurBlock, handle nested blocks. |
| 6677 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6678 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6679 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6680 | if (!BSI->ReturnType.isNull()) |
| 6681 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6682 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6683 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6684 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6685 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6686 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6687 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6688 | QualType BlockTy; |
| 6689 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6690 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6691 | NoReturn); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6692 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6693 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6694 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6695 | NoReturn); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6696 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6697 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6698 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6699 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6700 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6701 | // If needed, diagnose invalid gotos and switches in the block. |
| 6702 | if (CurFunctionNeedsScopeChecking) |
| 6703 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6704 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6705 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6706 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6707 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6708 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6709 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6712 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6713 | ExprArg expr, TypeTy *type, |
| 6714 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6715 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6716 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6717 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6718 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6719 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6720 | |
| 6721 | // Get the va_list type |
| 6722 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6723 | if (VaListType->isArrayType()) { |
| 6724 | // Deal with implicit array decay; for example, on x86-64, |
| 6725 | // va_list is an array, but it's supposed to decay to |
| 6726 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6727 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6728 | // Make sure the input expression also decays appropriately. |
| 6729 | UsualUnaryConversions(E); |
| 6730 | } else { |
| 6731 | // Otherwise, the va_list argument must be an l-value because |
| 6732 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6733 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6734 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6735 | return ExprError(); |
| 6736 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6737 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6738 | if (!E->isTypeDependent() && |
| 6739 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6740 | return ExprError(Diag(E->getLocStart(), |
| 6741 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6742 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6743 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6744 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6745 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6746 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6747 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6748 | expr.release(); |
| 6749 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6750 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6753 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6754 | // The type of __null will be int or long, depending on the size of |
| 6755 | // pointers on the target. |
| 6756 | QualType Ty; |
| 6757 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6758 | Ty = Context.IntTy; |
| 6759 | else |
| 6760 | Ty = Context.LongTy; |
| 6761 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6762 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6765 | static void |
| 6766 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6767 | QualType DstType, |
| 6768 | Expr *SrcExpr, |
| 6769 | CodeModificationHint &Hint) { |
| 6770 | if (!SemaRef.getLangOptions().ObjC1) |
| 6771 | return; |
| 6772 | |
| 6773 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6774 | if (!PT) |
| 6775 | return; |
| 6776 | |
| 6777 | // Check if the destination is of type 'id'. |
| 6778 | if (!PT->isObjCIdType()) { |
| 6779 | // Check if the destination is the 'NSString' interface. |
| 6780 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6781 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6782 | return; |
| 6783 | } |
| 6784 | |
| 6785 | // Strip off any parens and casts. |
| 6786 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6787 | if (!SL || SL->isWide()) |
| 6788 | return; |
| 6789 | |
| 6790 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6791 | } |
| 6792 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6793 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6794 | SourceLocation Loc, |
| 6795 | QualType DstType, QualType SrcType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6796 | Expr *SrcExpr, AssignmentAction Action) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6797 | // Decode the result (notice that AST's are still created for extensions). |
| 6798 | bool isInvalid = false; |
| 6799 | unsigned DiagKind; |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6800 | CodeModificationHint Hint; |
| 6801 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6802 | switch (ConvTy) { |
| 6803 | default: assert(0 && "Unknown conversion type"); |
| 6804 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6805 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6806 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6807 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6808 | case IntToPointer: |
| 6809 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6810 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6811 | case IncompatiblePointer: |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6812 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6813 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6814 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6815 | case IncompatiblePointerSign: |
| 6816 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6817 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6818 | case FunctionVoidPointer: |
| 6819 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6820 | break; |
| 6821 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 6822 | // If the qualifiers lost were because we were applying the |
| 6823 | // (deprecated) C++ conversion from a string literal to a char* |
| 6824 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 6825 | // Ideally, this check would be performed in |
| 6826 | // CheckPointerTypesForAssignment. However, that would require a |
| 6827 | // bit of refactoring (so that the second argument is an |
| 6828 | // expression, rather than a type), which should be done as part |
| 6829 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 6830 | // C++ semantics. |
| 6831 | if (getLangOptions().CPlusPlus && |
| 6832 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 6833 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6834 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 6835 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 6836 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 6837 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6838 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6839 | case IntToBlockPointer: |
| 6840 | DiagKind = diag::err_int_to_block_pointer; |
| 6841 | break; |
| 6842 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 6843 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6844 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6845 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6846 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6847 | // it can give a more specific diagnostic. |
| 6848 | DiagKind = diag::warn_incompatible_qualified_id; |
| 6849 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6850 | case IncompatibleVectors: |
| 6851 | DiagKind = diag::warn_incompatible_vectors; |
| 6852 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6853 | case Incompatible: |
| 6854 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 6855 | isInvalid = true; |
| 6856 | break; |
| 6857 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6858 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6859 | Diag(Loc, DiagKind) << DstType << SrcType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6860 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6861 | return isInvalid; |
| 6862 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6863 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 6864 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6865 | llvm::APSInt ICEResult; |
| 6866 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 6867 | if (Result) |
| 6868 | *Result = ICEResult; |
| 6869 | return false; |
| 6870 | } |
| 6871 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6872 | Expr::EvalResult EvalResult; |
| 6873 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6874 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6875 | EvalResult.HasSideEffects) { |
| 6876 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 6877 | |
| 6878 | if (EvalResult.Diag) { |
| 6879 | // We only show the note if it's not the usual "invalid subexpression" |
| 6880 | // or if it's actually in a subexpression. |
| 6881 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 6882 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 6883 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 6884 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6885 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6886 | return true; |
| 6887 | } |
| 6888 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6889 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 6890 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6891 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6892 | if (EvalResult.Diag && |
| 6893 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 6894 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6895 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6896 | if (Result) |
| 6897 | *Result = EvalResult.Val.getInt(); |
| 6898 | return false; |
| 6899 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6900 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6901 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6902 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6903 | ExprEvalContexts.push_back( |
| 6904 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6907 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6908 | Sema::PopExpressionEvaluationContext() { |
| 6909 | // Pop the current expression evaluation context off the stack. |
| 6910 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 6911 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6912 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 6913 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 6914 | if (Rec.PotentiallyReferenced) { |
| 6915 | // Mark any remaining declarations in the current position of the stack |
| 6916 | // as "referenced". If they were not meant to be referenced, semantic |
| 6917 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6918 | for (PotentiallyReferencedDecls::iterator |
| 6919 | I = Rec.PotentiallyReferenced->begin(), |
| 6920 | IEnd = Rec.PotentiallyReferenced->end(); |
| 6921 | I != IEnd; ++I) |
| 6922 | MarkDeclarationReferenced(I->first, I->second); |
| 6923 | } |
| 6924 | |
| 6925 | if (Rec.PotentiallyDiagnosed) { |
| 6926 | // Emit any pending diagnostics. |
| 6927 | for (PotentiallyEmittedDiagnostics::iterator |
| 6928 | I = Rec.PotentiallyDiagnosed->begin(), |
| 6929 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 6930 | I != IEnd; ++I) |
| 6931 | Diag(I->first, I->second); |
| 6932 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | // When are coming out of an unevaluated context, clear out any |
| 6936 | // temporaries that we may have created as part of the evaluation of |
| 6937 | // the expression in that context: they aren't relevant because they |
| 6938 | // will never be constructed. |
| 6939 | if (Rec.Context == Unevaluated && |
| 6940 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 6941 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 6942 | ExprTemporaries.end()); |
| 6943 | |
| 6944 | // Destroy the popped expression evaluation record. |
| 6945 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6946 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6947 | |
| 6948 | /// \brief Note that the given declaration was referenced in the source code. |
| 6949 | /// |
| 6950 | /// This routine should be invoke whenever a given declaration is referenced |
| 6951 | /// in the source code, and where that reference occurred. If this declaration |
| 6952 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 6953 | /// C99 6.9p3), then the declaration will be marked as used. |
| 6954 | /// |
| 6955 | /// \param Loc the location where the declaration was referenced. |
| 6956 | /// |
| 6957 | /// \param D the declaration that has been referenced by the source code. |
| 6958 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 6959 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6960 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6961 | if (D->isUsed()) |
| 6962 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6963 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 6964 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 6965 | // template or not. The reason for this is that unevaluated expressions |
| 6966 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 6967 | // -Wunused-parameters) |
| 6968 | if (isa<ParmVarDecl>(D) || |
| 6969 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6970 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6971 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6972 | // Do not mark anything as "used" within a dependent context; wait for |
| 6973 | // an instantiation. |
| 6974 | if (CurContext->isDependentContext()) |
| 6975 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6976 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6977 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6978 | case Unevaluated: |
| 6979 | // We are in an expression that is not potentially evaluated; do nothing. |
| 6980 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6981 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6982 | case PotentiallyEvaluated: |
| 6983 | // We are in a potentially-evaluated expression, so this declaration is |
| 6984 | // "used"; handle this below. |
| 6985 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6986 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6987 | case PotentiallyPotentiallyEvaluated: |
| 6988 | // We are in an expression that may be potentially evaluated; queue this |
| 6989 | // declaration reference until we know whether the expression is |
| 6990 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 6991 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6992 | return; |
| 6993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6994 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6995 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 6996 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6997 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6998 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 6999 | if (!Constructor->isUsed()) |
| 7000 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7001 | } else if (Constructor->isImplicit() && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7002 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7003 | if (!Constructor->isUsed()) |
| 7004 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7005 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7006 | |
| 7007 | MaybeMarkVirtualMembersReferenced(Loc, Constructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7008 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 7009 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 7010 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7011 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7012 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7013 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7014 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 7015 | if (!MethodDecl->isUsed()) |
| 7016 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 7017 | } |
| 7018 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7019 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7020 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7021 | // class templates. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7022 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7023 | bool AlreadyInstantiated = false; |
| 7024 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7025 | = Function->getTemplateSpecializationInfo()) { |
| 7026 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7027 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7028 | else if (SpecInfo->getTemplateSpecializationKind() |
| 7029 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7030 | AlreadyInstantiated = true; |
| 7031 | } else if (MemberSpecializationInfo *MSInfo |
| 7032 | = Function->getMemberSpecializationInfo()) { |
| 7033 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7034 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7035 | else if (MSInfo->getTemplateSpecializationKind() |
| 7036 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7037 | AlreadyInstantiated = true; |
| 7038 | } |
| 7039 | |
| 7040 | if (!AlreadyInstantiated) |
| 7041 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
| 7042 | } |
| 7043 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7044 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7045 | Function->setUsed(true); |
| 7046 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7047 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7048 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7049 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7050 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7051 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7052 | Var->getInstantiatedFromStaticDataMember()) { |
| 7053 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7054 | assert(MSInfo && "Missing member specialization information?"); |
| 7055 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7056 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7057 | MSInfo->setPointOfInstantiation(Loc); |
| 7058 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 7059 | } |
| 7060 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7061 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7062 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7063 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7064 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7065 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7066 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7067 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7068 | |
| 7069 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7070 | CallExpr *CE, FunctionDecl *FD) { |
| 7071 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7072 | return false; |
| 7073 | |
| 7074 | PartialDiagnostic Note = |
| 7075 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7076 | << FD->getDeclName() : PDiag(); |
| 7077 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 7078 | |
| 7079 | if (RequireCompleteType(Loc, ReturnType, |
| 7080 | FD ? |
| 7081 | PDiag(diag::err_call_function_incomplete_return) |
| 7082 | << CE->getSourceRange() << FD->getDeclName() : |
| 7083 | PDiag(diag::err_call_incomplete_return) |
| 7084 | << CE->getSourceRange(), |
| 7085 | std::make_pair(NoteLoc, Note))) |
| 7086 | return true; |
| 7087 | |
| 7088 | return false; |
| 7089 | } |
| 7090 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7091 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7092 | // will prevent this condition from triggering, which is what we want. |
| 7093 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7094 | SourceLocation Loc; |
| 7095 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7096 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7097 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7098 | if (isa<BinaryOperator>(E)) { |
| 7099 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 7100 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 7101 | return; |
| 7102 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7103 | // Greylist some idioms by putting them into a warning subcategory. |
| 7104 | if (ObjCMessageExpr *ME |
| 7105 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7106 | Selector Sel = ME->getSelector(); |
| 7107 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7108 | // self = [<foo> init...] |
| 7109 | if (isSelfExpr(Op->getLHS()) |
| 7110 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7111 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7112 | |
| 7113 | // <foo> = [<bar> nextObject] |
| 7114 | else if (Sel.isUnarySelector() && |
| 7115 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7116 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7117 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7118 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7119 | Loc = Op->getOperatorLoc(); |
| 7120 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7121 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7122 | if (Op->getOperator() != OO_Equal) |
| 7123 | return; |
| 7124 | |
| 7125 | Loc = Op->getOperatorLoc(); |
| 7126 | } else { |
| 7127 | // Not an assignment. |
| 7128 | return; |
| 7129 | } |
| 7130 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7131 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7132 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7133 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7134 | Diag(Loc, diagnostic) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7135 | << E->getSourceRange() |
| 7136 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 7137 | << CodeModificationHint::CreateInsertion(Close, ")"); |
| 7138 | } |
| 7139 | |
| 7140 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7141 | DiagnoseAssignmentAsCondition(E); |
| 7142 | |
| 7143 | if (!E->isTypeDependent()) { |
| 7144 | DefaultFunctionArrayConversion(E); |
| 7145 | |
| 7146 | QualType T = E->getType(); |
| 7147 | |
| 7148 | if (getLangOptions().CPlusPlus) { |
| 7149 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7150 | return true; |
| 7151 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7152 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7153 | << T << E->getSourceRange(); |
| 7154 | return true; |
| 7155 | } |
| 7156 | } |
| 7157 | |
| 7158 | return false; |
| 7159 | } |