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" |
Mike Stump | fa6ef18 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/AnalysisContext.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 26 | #include "clang/Lex/LiteralSupport.h" |
| 27 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 28 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 29 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 30 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 31 | #include "clang/Parse/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 35 | /// \brief Determine whether the use of this declaration is valid, and |
| 36 | /// emit any corresponding diagnostics. |
| 37 | /// |
| 38 | /// This routine diagnoses various problems with referencing |
| 39 | /// declarations that can occur when using a declaration. For example, |
| 40 | /// it might warn if a deprecated or unavailable declaration is being |
| 41 | /// used, or produce an error (and return true) if a C++0x deleted |
| 42 | /// function is being used. |
| 43 | /// |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 44 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 45 | /// decls. |
| 46 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 47 | /// \returns true if there was an error (this declaration cannot be |
| 48 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 49 | /// |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 50 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 51 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 52 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 53 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 56 | // See if the decl is unavailable |
| 57 | if (D->getAttr<UnavailableAttr>()) { |
| 58 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 59 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 60 | } |
| 61 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 62 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 63 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 64 | if (FD->isDeleted()) { |
| 65 | Diag(Loc, diag::err_deleted_function_use); |
| 66 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 67 | return true; |
| 68 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 69 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 71 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 74 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 76 | /// attribute. It warns if call does not have the sentinel argument. |
| 77 | /// |
| 78 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 80 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 82 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 83 | int sentinelPos = attr->getSentinel(); |
| 84 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 86 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 87 | // 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] | 88 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 89 | bool warnNotEnoughArgs = false; |
| 90 | int isMethod = 0; |
| 91 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 92 | // skip over named parameters. |
| 93 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 94 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 95 | if (nullPos) |
| 96 | --nullPos; |
| 97 | else |
| 98 | ++i; |
| 99 | } |
| 100 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 101 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 102 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 103 | // skip over named parameters. |
| 104 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 105 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 106 | if (nullPos) |
| 107 | --nullPos; |
| 108 | else |
| 109 | ++i; |
| 110 | } |
| 111 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 112 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 113 | // block or function pointer call. |
| 114 | QualType Ty = V->getType(); |
| 115 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 117 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 118 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 119 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 120 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 121 | unsigned k; |
| 122 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 123 | if (nullPos) |
| 124 | --nullPos; |
| 125 | else |
| 126 | ++i; |
| 127 | } |
| 128 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 129 | } |
| 130 | if (Ty->isBlockPointerType()) |
| 131 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 132 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 133 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 134 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 138 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 139 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | int sentinel = i; |
| 143 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 144 | --sentinelPos; |
| 145 | ++i; |
| 146 | } |
| 147 | if (sentinelPos > 0) { |
| 148 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 149 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | while (i < NumArgs-1) { |
| 153 | ++i; |
| 154 | ++sentinel; |
| 155 | } |
| 156 | Expr *sentinelExpr = Args[sentinel]; |
Anders Carlsson | e4d2bdd | 2009-11-24 17:24:21 +0000 | [diff] [blame] | 157 | if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) && |
| 158 | (!sentinelExpr->getType()->isPointerType() || |
| 159 | !sentinelExpr->isNullPointerConstant(Context, |
| 160 | Expr::NPC_ValueDependentIsNull)))) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 161 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 162 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 163 | } |
| 164 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 167 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 168 | Expr *Ex = (Expr *)E; |
| 169 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 170 | } |
| 171 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
| 173 | // Standard Promotions and Conversions |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 176 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 177 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 178 | QualType Ty = E->getType(); |
| 179 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 180 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 181 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 183 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 184 | else if (Ty->isArrayType()) { |
| 185 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 186 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 187 | // type 'array of type' is converted to an expression that has type 'pointer |
| 188 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 189 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 190 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 191 | // |
| 192 | // C++ 4.2p1: |
| 193 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 194 | // T" can be converted to an rvalue of type "pointer to T". |
| 195 | // |
| 196 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 197 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 198 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 199 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 205 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 206 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 207 | /// In these instances, this routine should *not* be called. |
| 208 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 209 | QualType Ty = Expr->getType(); |
| 210 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 212 | // C99 6.3.1.1p2: |
| 213 | // |
| 214 | // The following may be used in an expression wherever an int or |
| 215 | // unsigned int may be used: |
| 216 | // - an object or expression with an integer type whose integer |
| 217 | // conversion rank is less than or equal to the rank of int |
| 218 | // and unsigned int. |
| 219 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 220 | // |
| 221 | // If an int can represent all values of the original type, the |
| 222 | // value is converted to an int; otherwise, it is converted to an |
| 223 | // unsigned int. These are called the integer promotions. All |
| 224 | // other types are unchanged by the integer promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 225 | QualType PTy = Context.isPromotableBitField(Expr); |
| 226 | if (!PTy.isNull()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 227 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 228 | return Expr; |
| 229 | } |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 230 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 231 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 232 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 233 | return Expr; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 236 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 237 | return Expr; |
| 238 | } |
| 239 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 240 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | /// 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] | 242 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 243 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 244 | QualType Ty = Expr->getType(); |
| 245 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 247 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 248 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 249 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 250 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 251 | CastExpr::CK_FloatingCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 253 | UsualUnaryConversions(Expr); |
| 254 | } |
| 255 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 256 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 257 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 258 | /// interfaces passed by value. This returns true if the argument type is |
| 259 | /// completely illegal. |
| 260 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 261 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 263 | if (Expr->getType()->isObjCInterfaceType() && |
| 264 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 265 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 266 | << Expr->getType() << CT)) |
| 267 | return true; |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 269 | if (!Expr->getType()->isPODType() && |
| 270 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 271 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 272 | << Expr->getType() << CT)) |
| 273 | return true; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 274 | |
| 275 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 280 | /// 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] | 281 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 282 | /// responsible for emitting appropriate error diagnostics. |
| 283 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 284 | /// GCC. |
| 285 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 286 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 287 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 288 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 289 | |
| 290 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 291 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 293 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 294 | QualType lhs = |
| 295 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 297 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 298 | |
| 299 | // If both types are identical, no conversion is needed. |
| 300 | if (lhs == rhs) |
| 301 | return lhs; |
| 302 | |
| 303 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 304 | // The caller can deal with this (e.g. pointer + int). |
| 305 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 306 | return lhs; |
| 307 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 308 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 309 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 310 | if (!LHSBitfieldPromoteTy.isNull()) |
| 311 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 312 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 313 | if (!RHSBitfieldPromoteTy.isNull()) |
| 314 | rhs = RHSBitfieldPromoteTy; |
| 315 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 316 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 317 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 318 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 319 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 320 | return destType; |
| 321 | } |
| 322 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 323 | //===----------------------------------------------------------------------===// |
| 324 | // Semantic Analysis for various Expression Types |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | |
| 327 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 328 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 330 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 331 | /// multiple tokens. However, the common case is that StringToks points to one |
| 332 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 333 | /// |
| 334 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 335 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | assert(NumStringToks && "Must have at least one string!"); |
| 337 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 338 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 340 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | |
| 342 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 343 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 344 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 345 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 346 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 347 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 348 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 349 | |
| 350 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 351 | if (getLangOptions().CPlusPlus) |
| 352 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 353 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 354 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 355 | // the nul terminator character as well as the string length for pascal |
| 356 | // strings. |
| 357 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 358 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 359 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 363 | Literal.GetStringLength(), |
| 364 | Literal.AnyWide, StrTy, |
| 365 | &StringTokLocs[0], |
| 366 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 369 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 370 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 371 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 372 | /// for values inside the block or for globals). |
| 373 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 374 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 375 | /// up-to-date. |
| 376 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 377 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 378 | ValueDecl *VD) { |
| 379 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 380 | // we wanted to. |
| 381 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 382 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 384 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 385 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 386 | return false; |
| 387 | |
| 388 | // If this is a reference to an extern, static, or global variable, no need to |
| 389 | // snapshot it. |
| 390 | // FIXME: What about 'const' variables in C++? |
| 391 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 392 | if (!Var->hasLocalStorage()) |
| 393 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 395 | // Blocks that have these can't be constant. |
| 396 | CurBlock->hasBlockDeclRefExprs = true; |
| 397 | |
| 398 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 399 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 400 | // be defined outside all of the current blocks (in which case the blocks do |
| 401 | // all get the bit). Walk the nesting chain. |
| 402 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 403 | NextBlock = NextBlock->PrevBlockInfo) { |
| 404 | // If we found the defining block for the variable, don't mark the block as |
| 405 | // having a reference outside it. |
| 406 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 407 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 409 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 410 | // a snapshot as well. |
| 411 | NextBlock->hasBlockDeclRefExprs = true; |
| 412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 414 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 417 | |
| 418 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 419 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 420 | Sema::OwningExprResult |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 421 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 422 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 423 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 424 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 426 | << D->getDeclName(); |
| 427 | return ExprError(); |
| 428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 430 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 431 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 432 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 433 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 435 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 437 | << D->getIdentifier(); |
| 438 | return ExprError(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 444 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 446 | return Owned(DeclRefExpr::Create(Context, |
| 447 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 448 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 449 | D, Loc, Ty)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 452 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 453 | /// variable corresponding to the anonymous union or struct whose type |
| 454 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 455 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 456 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 458 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 460 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 461 | // operation rather than a slow walk through DeclContext's vector (which |
| 462 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 463 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 465 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 466 | D != DEnd; ++D) { |
| 467 | if (*D == Record) { |
| 468 | // The object for the anonymous struct/union directly |
| 469 | // follows its type in the list of declarations. |
| 470 | ++D; |
| 471 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 472 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 473 | return *D; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | assert(false && "Missing object for anonymous record"); |
| 478 | return 0; |
| 479 | } |
| 480 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 481 | /// \brief Given a field that represents a member of an anonymous |
| 482 | /// struct/union, build the path from that field's context to the |
| 483 | /// actual member. |
| 484 | /// |
| 485 | /// Construct the sequence of field member references we'll have to |
| 486 | /// perform to get to the field in the anonymous union/struct. The |
| 487 | /// list of members is built from the field outward, so traverse it |
| 488 | /// backwards to go from an object in the current context to the field |
| 489 | /// we found. |
| 490 | /// |
| 491 | /// \returns The variable from which the field access should begin, |
| 492 | /// for an anonymous struct/union that is not a member of another |
| 493 | /// class. Otherwise, returns NULL. |
| 494 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 495 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 496 | assert(Field->getDeclContext()->isRecord() && |
| 497 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 498 | && "Field must be stored inside an anonymous struct or union"); |
| 499 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 500 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 501 | VarDecl *BaseObject = 0; |
| 502 | DeclContext *Ctx = Field->getDeclContext(); |
| 503 | do { |
| 504 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 505 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 506 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 507 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 508 | else { |
| 509 | BaseObject = cast<VarDecl>(AnonObject); |
| 510 | break; |
| 511 | } |
| 512 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 514 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 515 | |
| 516 | return BaseObject; |
| 517 | } |
| 518 | |
| 519 | Sema::OwningExprResult |
| 520 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 521 | FieldDecl *Field, |
| 522 | Expr *BaseObjectExpr, |
| 523 | SourceLocation OpLoc) { |
| 524 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 526 | AnonFields); |
| 527 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 528 | // Build the expression that refers to the base object, from |
| 529 | // which we will build a sequence of member references to each |
| 530 | // of the anonymous union objects and, eventually, the field we |
| 531 | // found via name lookup. |
| 532 | bool BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 533 | Qualifiers BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 534 | if (BaseObject) { |
| 535 | // BaseObject is an anonymous struct/union variable (and is, |
| 536 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 537 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 538 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 539 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 540 | SourceLocation()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 541 | BaseQuals |
| 542 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 543 | } else if (BaseObjectExpr) { |
| 544 | // The caller provided the base object expression. Determine |
| 545 | // whether its a pointer and whether it adds any qualifiers to the |
| 546 | // anonymous struct/union fields we're looking into. |
| 547 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 548 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 549 | BaseObjectIsPointer = true; |
| 550 | ObjectType = ObjectPtr->getPointeeType(); |
| 551 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 552 | BaseQuals |
| 553 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 554 | } else { |
| 555 | // We've found a member of an anonymous struct/union that is |
| 556 | // inside a non-anonymous struct/union, so in a well-formed |
| 557 | // program our base object expression is "this". |
| 558 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 559 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 561 | = Context.getTagDeclType( |
| 562 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 563 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 565 | == Context.getCanonicalType(ThisType)) || |
| 566 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 567 | // Our base object expression is "this". |
Douglas Gregor | 8aa5f40 | 2009-12-24 20:23:34 +0000 | [diff] [blame] | 568 | BaseObjectExpr = new (Context) CXXThisExpr(Loc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 569 | MD->getThisType(Context), |
| 570 | /*isImplicit=*/true); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 571 | BaseObjectIsPointer = true; |
| 572 | } |
| 573 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 574 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 575 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 576 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 577 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 581 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 582 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // Build the implicit member references to the field of the |
| 586 | // anonymous struct/union. |
| 587 | Expr *Result = BaseObjectExpr; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 588 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 589 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 590 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 591 | FI != FIEnd; ++FI) { |
| 592 | QualType MemberType = (*FI)->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 593 | Qualifiers MemberTypeQuals = |
| 594 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 595 | |
| 596 | // CVR attributes from the base are picked up by members, |
| 597 | // except that 'mutable' members don't pick up 'const'. |
| 598 | if ((*FI)->isMutable()) |
| 599 | ResultQuals.removeConst(); |
| 600 | |
| 601 | // GC attributes are never picked up by members. |
| 602 | ResultQuals.removeObjCGCAttr(); |
| 603 | |
| 604 | // TR 18037 does not allow fields to be declared with address spaces. |
| 605 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 606 | |
| 607 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 608 | if (NewQuals != MemberTypeQuals) |
| 609 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 610 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 611 | MarkDeclarationReferenced(Loc, *FI); |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 612 | PerformObjectMemberConversion(Result, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 613 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 614 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 615 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 616 | BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 617 | ResultQuals = NewQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 620 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 621 | } |
| 622 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 623 | /// Decomposes the given name into a DeclarationName, its location, and |
| 624 | /// possibly a list of template arguments. |
| 625 | /// |
| 626 | /// If this produces template arguments, it is permitted to call |
| 627 | /// DecomposeTemplateName. |
| 628 | /// |
| 629 | /// This actually loses a lot of source location information for |
| 630 | /// non-standard name kinds; we should consider preserving that in |
| 631 | /// some way. |
| 632 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 633 | const UnqualifiedId &Id, |
| 634 | TemplateArgumentListInfo &Buffer, |
| 635 | DeclarationName &Name, |
| 636 | SourceLocation &NameLoc, |
| 637 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 638 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 639 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 640 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 641 | |
| 642 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 643 | Id.TemplateId->getTemplateArgs(), |
| 644 | Id.TemplateId->NumArgs); |
| 645 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 646 | TemplateArgsPtr.release(); |
| 647 | |
| 648 | TemplateName TName = |
| 649 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 650 | |
| 651 | Name = SemaRef.Context.getNameForTemplate(TName); |
| 652 | NameLoc = Id.TemplateId->TemplateNameLoc; |
| 653 | TemplateArgs = &Buffer; |
| 654 | } else { |
| 655 | Name = SemaRef.GetNameFromUnqualifiedId(Id); |
| 656 | NameLoc = Id.StartLocation; |
| 657 | TemplateArgs = 0; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /// Decompose the given template name into a list of lookup results. |
| 662 | /// |
| 663 | /// The unqualified ID must name a non-dependent template, which can |
| 664 | /// be more easily tested by checking whether DecomposeUnqualifiedId |
| 665 | /// found template arguments. |
| 666 | static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) { |
| 667 | assert(Id.getKind() == UnqualifiedId::IK_TemplateId); |
| 668 | TemplateName TName = |
| 669 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 670 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 671 | if (TemplateDecl *TD = TName.getAsTemplateDecl()) |
| 672 | R.addDecl(TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 673 | else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate()) |
| 674 | for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end(); |
| 675 | I != E; ++I) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 676 | R.addDecl(*I); |
John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 677 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 678 | R.resolveKind(); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 679 | } |
| 680 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 681 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
| 682 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 683 | E = Record->bases_end(); I != E; ++I) { |
| 684 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 685 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 686 | if (!BaseRT) return false; |
| 687 | |
| 688 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 689 | if (!BaseRecord->isDefinition() || |
| 690 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | return true; |
| 695 | } |
| 696 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 697 | /// Determines whether we can lookup this id-expression now or whether |
| 698 | /// we have to wait until template instantiation is complete. |
| 699 | static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 700 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 701 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 702 | // If the qualifier scope isn't computable, it's definitely dependent. |
| 703 | if (!DC) return true; |
| 704 | |
| 705 | // If the qualifier scope doesn't name a record, we can always look into it. |
| 706 | if (!isa<CXXRecordDecl>(DC)) return false; |
| 707 | |
| 708 | // We can't look into record types unless they're fully-formed. |
| 709 | if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true; |
| 710 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 711 | return false; |
| 712 | } |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 713 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 714 | /// Determines if the given class is provably not derived from all of |
| 715 | /// the prospective base classes. |
| 716 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 717 | CXXRecordDecl *Record, |
| 718 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 719 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 720 | return false; |
| 721 | |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 722 | RecordDecl *RD = Record->getDefinition(SemaRef.Context); |
| 723 | if (!RD) return false; |
| 724 | Record = cast<CXXRecordDecl>(RD); |
| 725 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 726 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 727 | E = Record->bases_end(); I != E; ++I) { |
| 728 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 729 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 730 | if (!BaseRT) return false; |
| 731 | |
| 732 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 733 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | return true; |
| 738 | } |
| 739 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 740 | /// Determines if this is an instance member of a class. |
| 741 | static bool IsInstanceMember(NamedDecl *D) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 742 | assert(D->isCXXClassMember() && |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 743 | "checking whether non-member is instance member"); |
| 744 | |
| 745 | if (isa<FieldDecl>(D)) return true; |
| 746 | |
| 747 | if (isa<CXXMethodDecl>(D)) |
| 748 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 749 | |
| 750 | if (isa<FunctionTemplateDecl>(D)) { |
| 751 | D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 752 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 753 | } |
| 754 | |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | enum IMAKind { |
| 759 | /// The reference is definitely not an instance member access. |
| 760 | IMA_Static, |
| 761 | |
| 762 | /// The reference may be an implicit instance member access. |
| 763 | IMA_Mixed, |
| 764 | |
| 765 | /// The reference may be to an instance member, but it is invalid if |
| 766 | /// so, because the context is not an instance method. |
| 767 | IMA_Mixed_StaticContext, |
| 768 | |
| 769 | /// The reference may be to an instance member, but it is invalid if |
| 770 | /// so, because the context is from an unrelated class. |
| 771 | IMA_Mixed_Unrelated, |
| 772 | |
| 773 | /// The reference is definitely an implicit instance member access. |
| 774 | IMA_Instance, |
| 775 | |
| 776 | /// The reference may be to an unresolved using declaration. |
| 777 | IMA_Unresolved, |
| 778 | |
| 779 | /// The reference may be to an unresolved using declaration and the |
| 780 | /// context is not an instance method. |
| 781 | IMA_Unresolved_StaticContext, |
| 782 | |
| 783 | /// The reference is to a member of an anonymous structure in a |
| 784 | /// non-class context. |
| 785 | IMA_AnonymousMember, |
| 786 | |
| 787 | /// All possible referrents are instance members and the current |
| 788 | /// context is not an instance method. |
| 789 | IMA_Error_StaticContext, |
| 790 | |
| 791 | /// All possible referrents are instance members of an unrelated |
| 792 | /// class. |
| 793 | IMA_Error_Unrelated |
| 794 | }; |
| 795 | |
| 796 | /// The given lookup names class member(s) and is not being used for |
| 797 | /// an address-of-member expression. Classify the type of access |
| 798 | /// according to whether it's possible that this reference names an |
| 799 | /// instance member. This is best-effort; it is okay to |
| 800 | /// conservatively answer "yes", in which case some errors will simply |
| 801 | /// not be caught until template-instantiation. |
| 802 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 803 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 804 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 805 | |
| 806 | bool isStaticContext = |
| 807 | (!isa<CXXMethodDecl>(SemaRef.CurContext) || |
| 808 | cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic()); |
| 809 | |
| 810 | if (R.isUnresolvableResult()) |
| 811 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 812 | |
| 813 | // Collect all the declaring classes of instance members we find. |
| 814 | bool hasNonInstance = false; |
| 815 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 816 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 817 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 818 | if (IsInstanceMember(D)) { |
| 819 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 820 | |
| 821 | // If this is a member of an anonymous record, move out to the |
| 822 | // innermost non-anonymous struct or union. If there isn't one, |
| 823 | // that's a special case. |
| 824 | while (R->isAnonymousStructOrUnion()) { |
| 825 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 826 | if (!R) return IMA_AnonymousMember; |
| 827 | } |
| 828 | Classes.insert(R->getCanonicalDecl()); |
| 829 | } |
| 830 | else |
| 831 | hasNonInstance = true; |
| 832 | } |
| 833 | |
| 834 | // If we didn't find any instance members, it can't be an implicit |
| 835 | // member reference. |
| 836 | if (Classes.empty()) |
| 837 | return IMA_Static; |
| 838 | |
| 839 | // If the current context is not an instance method, it can't be |
| 840 | // an implicit member reference. |
| 841 | if (isStaticContext) |
| 842 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 843 | |
| 844 | // If we can prove that the current context is unrelated to all the |
| 845 | // declaring classes, it can't be an implicit member reference (in |
| 846 | // which case it's an error if any of those members are selected). |
| 847 | if (IsProvablyNotDerivedFrom(SemaRef, |
| 848 | cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(), |
| 849 | Classes)) |
| 850 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 851 | |
| 852 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 853 | } |
| 854 | |
| 855 | /// Diagnose a reference to a field with no object available. |
| 856 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 857 | const CXXScopeSpec &SS, |
| 858 | const LookupResult &R) { |
| 859 | SourceLocation Loc = R.getNameLoc(); |
| 860 | SourceRange Range(Loc); |
| 861 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 862 | |
| 863 | if (R.getAsSingle<FieldDecl>()) { |
| 864 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 865 | if (MD->isStatic()) { |
| 866 | // "invalid use of member 'x' in static member function" |
| 867 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 868 | << Range << R.getLookupName(); |
| 869 | return; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 874 | << R.getLookupName() << Range; |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 881 | /// Diagnose an empty lookup. |
| 882 | /// |
| 883 | /// \return false if new lookup candidates were found |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 884 | bool Sema::DiagnoseEmptyLookup(Scope *S, const CXXScopeSpec &SS, |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 885 | LookupResult &R) { |
| 886 | DeclarationName Name = R.getLookupName(); |
| 887 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 888 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 889 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 890 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 891 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 892 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 893 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 894 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 895 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 896 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 897 | // If the original lookup was an unqualified lookup, fake an |
| 898 | // unqualified lookup. This is useful when (for example) the |
| 899 | // original lookup would not have found something because it was a |
| 900 | // dependent name. |
| 901 | for (DeclContext *DC = SS.isEmpty()? CurContext : 0; |
| 902 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 903 | if (isa<CXXRecordDecl>(DC)) { |
| 904 | LookupQualifiedName(R, DC); |
| 905 | |
| 906 | if (!R.empty()) { |
| 907 | // Don't give errors about ambiguities in this lookup. |
| 908 | R.suppressDiagnostics(); |
| 909 | |
| 910 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 911 | bool isInstance = CurMethod && |
| 912 | CurMethod->isInstance() && |
| 913 | DC == CurMethod->getParent(); |
| 914 | |
| 915 | // Give a code modification hint to insert 'this->'. |
| 916 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 917 | // Actually quite difficult! |
| 918 | if (isInstance) |
| 919 | Diag(R.getNameLoc(), diagnostic) << Name |
| 920 | << CodeModificationHint::CreateInsertion(R.getNameLoc(), |
| 921 | "this->"); |
| 922 | else |
| 923 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 924 | |
| 925 | // Do we really want to note all of these? |
| 926 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 927 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 928 | |
| 929 | // Tell the callee to try to recover. |
| 930 | return false; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 935 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 936 | if (S && CorrectTypo(R, S, &SS)) { |
| 937 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 938 | if (SS.isEmpty()) |
| 939 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 940 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 941 | R.getLookupName().getAsString()); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 942 | else |
| 943 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 944 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 945 | << SS.getRange() |
| 946 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 947 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 948 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 949 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 950 | << ND->getDeclName(); |
| 951 | |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 952 | // Tell the callee to try to recover. |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 957 | // FIXME: If we ended up with a typo for a type name or |
| 958 | // Objective-C class name, we're in trouble because the parser |
| 959 | // is in the wrong place to recover. Suggest the typo |
| 960 | // correction, but don't make it a fix-it since we're not going |
| 961 | // to recover well anyway. |
| 962 | if (SS.isEmpty()) |
| 963 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 964 | else |
| 965 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 966 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 967 | << SS.getRange(); |
| 968 | |
| 969 | // Don't try to recover; it won't work. |
| 970 | return true; |
| 971 | } |
| 972 | |
| 973 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | // Emit a special diagnostic for failed member lookups. |
| 977 | // FIXME: computing the declaration context might fail here (?) |
| 978 | if (!SS.isEmpty()) { |
| 979 | Diag(R.getNameLoc(), diag::err_no_member) |
| 980 | << Name << computeDeclContext(SS, false) |
| 981 | << SS.getRange(); |
| 982 | return true; |
| 983 | } |
| 984 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 985 | // Give up, we can't recover. |
| 986 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 987 | return true; |
| 988 | } |
| 989 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 990 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 991 | const CXXScopeSpec &SS, |
| 992 | UnqualifiedId &Id, |
| 993 | bool HasTrailingLParen, |
| 994 | bool isAddressOfOperand) { |
| 995 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 996 | "cannot be direct & operand and have a trailing lparen"); |
| 997 | |
| 998 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 999 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1000 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1001 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Decompose the UnqualifiedId into the following data. |
| 1004 | DeclarationName Name; |
| 1005 | SourceLocation NameLoc; |
| 1006 | const TemplateArgumentListInfo *TemplateArgs; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1007 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 1008 | Name, NameLoc, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1010 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1011 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1012 | // C++ [temp.dep.expr]p3: |
| 1013 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1014 | // -- an identifier that was declared with a dependent type, |
| 1015 | // (note: handled after lookup) |
| 1016 | // -- a template-id that is dependent, |
| 1017 | // (note: handled in BuildTemplateIdExpr) |
| 1018 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1019 | // -- a nested-name-specifier that contains a class-name that |
| 1020 | // names a dependent type. |
| 1021 | // Determine whether this is a member of an unknown specialization; |
| 1022 | // we need to handle these differently. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1023 | if ((Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1024 | Name.getCXXNameType()->isDependentType()) || |
| 1025 | (SS.isSet() && IsDependentIdExpression(*this, SS))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1026 | return ActOnDependentIdExpression(SS, Name, NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 1027 | isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1028 | TemplateArgs); |
| 1029 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1030 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1031 | // Perform the required lookup. |
| 1032 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1033 | if (TemplateArgs) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1034 | // Just re-use the lookup done by isTemplateName. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1035 | DecomposeTemplateName(R, Id); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1036 | } else { |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1037 | bool IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
| 1038 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1040 | // If this reference is in an Objective-C method, then we need to do |
| 1041 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1042 | if (IvarLookupFollowUp) { |
| 1043 | OwningExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1044 | if (E.isInvalid()) |
| 1045 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1047 | Expr *Ex = E.takeAs<Expr>(); |
| 1048 | if (Ex) return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1049 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1050 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1051 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1052 | if (R.isAmbiguous()) |
| 1053 | return ExprError(); |
| 1054 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1055 | // Determine whether this name might be a candidate for |
| 1056 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1057 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1058 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1059 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1060 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1061 | // in C90, extension in C99, forbidden in C++). |
| 1062 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1063 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1064 | if (D) R.addDecl(D); |
| 1065 | } |
| 1066 | |
| 1067 | // If this name wasn't predeclared and if this is not a function |
| 1068 | // call, diagnose the problem. |
| 1069 | if (R.empty()) { |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1070 | if (DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1071 | return ExprError(); |
| 1072 | |
| 1073 | assert(!R.empty() && |
| 1074 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1075 | |
| 1076 | // If we found an Objective-C instance variable, let |
| 1077 | // LookupInObjCMethod build the appropriate expression to |
| 1078 | // reference the ivar. |
| 1079 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1080 | R.clear(); |
| 1081 | OwningExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
| 1082 | assert(E.isInvalid() || E.get()); |
| 1083 | return move(E); |
| 1084 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1088 | // This is guaranteed from this point on. |
| 1089 | assert(!R.empty() || ADL); |
| 1090 | |
| 1091 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1092 | // Warn about constructs like: |
| 1093 | // if (void *X = foo()) { ... } else { X }. |
| 1094 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1096 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1097 | Scope *CheckS = S; |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1098 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1100 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1101 | ExprError(Diag(NameLoc, diag::warn_value_always_zero) |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1102 | << Var->getDeclName() |
| 1103 | << (Var->getType()->isPointerType()? 2 : |
| 1104 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1108 | // Move to the parent of this scope. |
| 1109 | CheckS = CheckS->getParent(); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1112 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1113 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1114 | // C99 DR 316 says that, if a function type comes from a |
| 1115 | // function definition (without a prototype), that type is only |
| 1116 | // used for checking compatibility. Therefore, when referencing |
| 1117 | // the function, we pretend that we don't have the full function |
| 1118 | // type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1119 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1120 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1122 | QualType T = Func->getType(); |
| 1123 | QualType NoProtoType = T; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1124 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1125 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1126 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1130 | // Check whether this might be a C++ implicit instance member access. |
| 1131 | // C++ [expr.prim.general]p6: |
| 1132 | // Within the definition of a non-static member function, an |
| 1133 | // identifier that names a non-static member is transformed to a |
| 1134 | // class member access expression. |
| 1135 | // But note that &SomeClass::foo is grammatically distinct, even |
| 1136 | // though we don't parse it that way. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1137 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1138 | bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1139 | if (!isAbstractMemberPointer) |
| 1140 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1143 | if (TemplateArgs) |
| 1144 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1145 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1146 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1147 | } |
| 1148 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1149 | /// Builds an expression which might be an implicit member expression. |
| 1150 | Sema::OwningExprResult |
| 1151 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1152 | LookupResult &R, |
| 1153 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1154 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1155 | case IMA_Instance: |
| 1156 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1157 | |
| 1158 | case IMA_AnonymousMember: |
| 1159 | assert(R.isSingleResult()); |
| 1160 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1161 | R.getAsSingle<FieldDecl>()); |
| 1162 | |
| 1163 | case IMA_Mixed: |
| 1164 | case IMA_Mixed_Unrelated: |
| 1165 | case IMA_Unresolved: |
| 1166 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1167 | |
| 1168 | case IMA_Static: |
| 1169 | case IMA_Mixed_StaticContext: |
| 1170 | case IMA_Unresolved_StaticContext: |
| 1171 | if (TemplateArgs) |
| 1172 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1173 | return BuildDeclarationNameExpr(SS, R, false); |
| 1174 | |
| 1175 | case IMA_Error_StaticContext: |
| 1176 | case IMA_Error_Unrelated: |
| 1177 | DiagnoseInstanceReference(*this, SS, R); |
| 1178 | return ExprError(); |
| 1179 | } |
| 1180 | |
| 1181 | llvm_unreachable("unexpected instance member access kind"); |
| 1182 | return ExprError(); |
| 1183 | } |
| 1184 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1185 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1186 | /// declaration name, generally during template instantiation. |
| 1187 | /// There's a large number of things which don't need to be done along |
| 1188 | /// this path. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1189 | Sema::OwningExprResult |
| 1190 | Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS, |
| 1191 | DeclarationName Name, |
| 1192 | SourceLocation NameLoc) { |
| 1193 | DeclContext *DC; |
| 1194 | if (!(DC = computeDeclContext(SS, false)) || |
| 1195 | DC->isDependentContext() || |
| 1196 | RequireCompleteDeclContext(SS)) |
| 1197 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0); |
| 1198 | |
| 1199 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1200 | LookupQualifiedName(R, DC); |
| 1201 | |
| 1202 | if (R.isAmbiguous()) |
| 1203 | return ExprError(); |
| 1204 | |
| 1205 | if (R.empty()) { |
| 1206 | Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange(); |
| 1207 | return ExprError(); |
| 1208 | } |
| 1209 | |
| 1210 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1211 | } |
| 1212 | |
| 1213 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1214 | /// detected that we're currently inside an ObjC method. Perform some |
| 1215 | /// additional lookup. |
| 1216 | /// |
| 1217 | /// Ideally, most of this would be done by lookup, but there's |
| 1218 | /// actually quite a lot of extra work involved. |
| 1219 | /// |
| 1220 | /// Returns a null sentinel to indicate trivial success. |
| 1221 | Sema::OwningExprResult |
| 1222 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1223 | IdentifierInfo *II, |
| 1224 | bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1225 | SourceLocation Loc = Lookup.getNameLoc(); |
| 1226 | |
| 1227 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1228 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1229 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1230 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1231 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1232 | |
| 1233 | // If we're in a class method, we don't normally want to look for |
| 1234 | // ivars. But if we don't find anything else, and there's an |
| 1235 | // ivar, that's an error. |
| 1236 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 1237 | |
| 1238 | bool LookForIvars; |
| 1239 | if (Lookup.empty()) |
| 1240 | LookForIvars = true; |
| 1241 | else if (IsClassMethod) |
| 1242 | LookForIvars = false; |
| 1243 | else |
| 1244 | LookForIvars = (Lookup.isSingleResult() && |
| 1245 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1246 | |
| 1247 | if (LookForIvars) { |
| 1248 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1249 | ObjCInterfaceDecl *ClassDeclared; |
| 1250 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1251 | // Diagnose using an ivar in a class method. |
| 1252 | if (IsClassMethod) |
| 1253 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1254 | << IV->getDeclName()); |
| 1255 | |
| 1256 | // If we're referencing an invalid decl, just return this as a silent |
| 1257 | // error node. The error diagnostic was already emitted on the decl. |
| 1258 | if (IV->isInvalidDecl()) |
| 1259 | return ExprError(); |
| 1260 | |
| 1261 | // Check if referencing a field with __attribute__((deprecated)). |
| 1262 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1263 | return ExprError(); |
| 1264 | |
| 1265 | // Diagnose the use of an ivar outside of the declaring class. |
| 1266 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1267 | ClassDeclared != IFace) |
| 1268 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1269 | |
| 1270 | // FIXME: This should use a new expr for a direct reference, don't |
| 1271 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1272 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1273 | UnqualifiedId SelfName; |
| 1274 | SelfName.setIdentifier(&II, SourceLocation()); |
| 1275 | CXXScopeSpec SelfScopeSpec; |
| 1276 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 1277 | SelfName, false, false); |
| 1278 | MarkDeclarationReferenced(Loc, IV); |
| 1279 | return Owned(new (Context) |
| 1280 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1281 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1282 | } |
| 1283 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
| 1284 | // We should warn if a local variable hides an ivar. |
| 1285 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1286 | ObjCInterfaceDecl *ClassDeclared; |
| 1287 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1288 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1289 | IFace == ClassDeclared) |
| 1290 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | // Needed to implement property "super.method" notation. |
| 1295 | if (Lookup.empty() && II->isStr("super")) { |
| 1296 | QualType T; |
| 1297 | |
| 1298 | if (getCurMethodDecl()->isInstanceMethod()) |
| 1299 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 1300 | getCurMethodDecl()->getClassInterface())); |
| 1301 | else |
| 1302 | T = Context.getObjCClassType(); |
| 1303 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
| 1304 | } |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1305 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1306 | // FIXME. Consolidate this with similar code in LookupName. |
| 1307 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1308 | if (!(getLangOptions().CPlusPlus && |
| 1309 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1310 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1311 | S, Lookup.isForRedeclaration(), |
| 1312 | Lookup.getNameLoc()); |
| 1313 | if (D) Lookup.addDecl(D); |
| 1314 | } |
| 1315 | } |
| 1316 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1317 | // Sentinel value saying that we didn't do anything special. |
| 1318 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1319 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1320 | |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1321 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1322 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1323 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 1324 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1326 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1328 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1329 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 1330 | return false; |
| 1331 | QualType FromRecordType = From->getType(); |
| 1332 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1333 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1334 | DestType = Context.getPointerType(DestType); |
| 1335 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1336 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1337 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 1338 | CheckDerivedToBaseConversion(FromRecordType, |
| 1339 | DestRecordType, |
| 1340 | From->getSourceRange().getBegin(), |
| 1341 | From->getSourceRange())) |
| 1342 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 1343 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 1344 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1345 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1346 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1347 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1349 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1351 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1352 | SourceLocation Loc, QualType Ty, |
| 1353 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1354 | NestedNameSpecifier *Qualifier = 0; |
| 1355 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1356 | if (SS.isSet()) { |
| 1357 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1358 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1361 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
| 1362 | Member, Loc, TemplateArgs, Ty); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1365 | /// Builds an implicit member access expression. The current context |
| 1366 | /// is known to be an instance method, and the given unqualified lookup |
| 1367 | /// set is known to contain only instance members, at least one of which |
| 1368 | /// is from an appropriate type. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1369 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1370 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1371 | LookupResult &R, |
| 1372 | const TemplateArgumentListInfo *TemplateArgs, |
| 1373 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1374 | assert(!R.empty() && !R.isAmbiguous()); |
| 1375 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1376 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1378 | // We may have found a field within an anonymous union or struct |
| 1379 | // (C++ [class.union]). |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1380 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1381 | // FIXME: template-ids inside anonymous structs? |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1382 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1383 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1384 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1385 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1386 | // If this is known to be an instance access, go ahead and build a |
| 1387 | // 'this' expression now. |
| 1388 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 1389 | Expr *This = 0; // null signifies implicit access |
| 1390 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 1391 | SourceLocation Loc = R.getNameLoc(); |
| 1392 | if (SS.getRange().isValid()) |
| 1393 | Loc = SS.getRange().getBegin(); |
| 1394 | This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1397 | return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType, |
| 1398 | /*OpLoc*/ SourceLocation(), |
| 1399 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 1400 | SS, |
| 1401 | /*FirstQualifierInScope*/ 0, |
| 1402 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1405 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1406 | const LookupResult &R, |
| 1407 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1408 | // Only when used directly as the postfix-expression of a call. |
| 1409 | if (!HasTrailingLParen) |
| 1410 | return false; |
| 1411 | |
| 1412 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1413 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1414 | return false; |
| 1415 | |
| 1416 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1417 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1418 | return false; |
| 1419 | |
| 1420 | // Turn off ADL when we find certain kinds of declarations during |
| 1421 | // normal lookup: |
| 1422 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1423 | NamedDecl *D = *I; |
| 1424 | |
| 1425 | // C++0x [basic.lookup.argdep]p3: |
| 1426 | // -- a declaration of a class member |
| 1427 | // Since using decls preserve this property, we check this on the |
| 1428 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1429 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1430 | return false; |
| 1431 | |
| 1432 | // C++0x [basic.lookup.argdep]p3: |
| 1433 | // -- a block-scope function declaration that is not a |
| 1434 | // using-declaration |
| 1435 | // NOTE: we also trigger this for function templates (in fact, we |
| 1436 | // don't check the decl type at all, since all other decl types |
| 1437 | // turn off ADL anyway). |
| 1438 | if (isa<UsingShadowDecl>(D)) |
| 1439 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1440 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1441 | return false; |
| 1442 | |
| 1443 | // C++0x [basic.lookup.argdep]p3: |
| 1444 | // -- a declaration that is neither a function or a function |
| 1445 | // template |
| 1446 | // And also for builtin functions. |
| 1447 | if (isa<FunctionDecl>(D)) { |
| 1448 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1449 | |
| 1450 | // But also builtin functions. |
| 1451 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1452 | return false; |
| 1453 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1454 | return false; |
| 1455 | } |
| 1456 | |
| 1457 | return true; |
| 1458 | } |
| 1459 | |
| 1460 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1461 | /// Diagnoses obvious problems with the use of the given declaration |
| 1462 | /// as an expression. This is only actually called for lookups that |
| 1463 | /// were not overloaded, and it doesn't promise that the declaration |
| 1464 | /// will in fact be used. |
| 1465 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1466 | if (isa<TypedefDecl>(D)) { |
| 1467 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1472 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1473 | return true; |
| 1474 | } |
| 1475 | |
| 1476 | if (isa<NamespaceDecl>(D)) { |
| 1477 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1478 | return true; |
| 1479 | } |
| 1480 | |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1485 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1486 | LookupResult &R, |
| 1487 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1488 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1489 | // just build an ordinary singleton decl ref. |
| 1490 | if (!NeedsADL && R.isSingleResult()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1491 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1492 | |
| 1493 | // We only need to check the declaration if there's exactly one |
| 1494 | // result, because in the overloaded case the results can only be |
| 1495 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1496 | if (R.isSingleResult() && |
| 1497 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1498 | return ExprError(); |
| 1499 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1500 | bool Dependent |
| 1501 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1502 | UnresolvedLookupExpr *ULE |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1503 | = UnresolvedLookupExpr::Create(Context, Dependent, |
| 1504 | (NestedNameSpecifier*) SS.getScopeRep(), |
| 1505 | SS.getRange(), |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1506 | R.getLookupName(), R.getNameLoc(), |
| 1507 | NeedsADL, R.isOverloadedResult()); |
| 1508 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1509 | ULE->addDecl(*I); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1510 | |
| 1511 | return Owned(ULE); |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1516 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1517 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1518 | SourceLocation Loc, NamedDecl *D) { |
| 1519 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1520 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1521 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1524 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1525 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1526 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1527 | // Specifically diagnose references to class templates that are missing |
| 1528 | // a template argument list. |
| 1529 | Diag(Loc, diag::err_template_decl_ref) |
| 1530 | << Template << SS.getRange(); |
| 1531 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1532 | return ExprError(); |
| 1533 | } |
| 1534 | |
| 1535 | // Make sure that we're referring to a value. |
| 1536 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1537 | if (!VD) { |
| 1538 | Diag(Loc, diag::err_ref_non_value) |
| 1539 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 1540 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1541 | return ExprError(); |
| 1542 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1543 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1544 | // Check whether this declaration can be used. Note that we suppress |
| 1545 | // this check when we're going to perform argument-dependent lookup |
| 1546 | // on this function name, because this might not be the function |
| 1547 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1548 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1549 | return ExprError(); |
| 1550 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1551 | // Only create DeclRefExpr's for valid Decl's. |
| 1552 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1553 | return ExprError(); |
| 1554 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1555 | // If the identifier reference is inside a block, and it refers to a value |
| 1556 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1557 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1558 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1559 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1560 | // We do not do this for things like enum constants, global variables, etc, |
| 1561 | // as they do not get snapshotted. |
| 1562 | // |
| 1563 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 1564 | if (VD->getType().getTypePtr()->isVariablyModifiedType()) { |
| 1565 | Diag(Loc, diag::err_ref_vm_type); |
| 1566 | Diag(D->getLocation(), diag::note_declared_at); |
| 1567 | return ExprError(); |
| 1568 | } |
| 1569 | |
Mike Stump | 2849734 | 2010-01-05 03:10:36 +0000 | [diff] [blame] | 1570 | if (VD->getType()->isArrayType()) { |
| 1571 | Diag(Loc, diag::err_ref_array_type); |
| 1572 | Diag(D->getLocation(), diag::note_declared_at); |
| 1573 | return ExprError(); |
| 1574 | } |
| 1575 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1576 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1577 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1578 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1579 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1580 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1581 | // This is to record that a 'const' was actually synthesize and added. |
| 1582 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1583 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1585 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1587 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1588 | } |
| 1589 | // If this reference is not in a block or if the referenced variable is |
| 1590 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1591 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1592 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1595 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1596 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1597 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1598 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1599 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1600 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1601 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1602 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1603 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1605 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1606 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1607 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1609 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1610 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1611 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1612 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1613 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1614 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1615 | QualType ResTy; |
| 1616 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1617 | ResTy = Context.DependentTy; |
| 1618 | } else { |
| 1619 | unsigned Length = |
| 1620 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1621 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1622 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1623 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1624 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1625 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1626 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1629 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | llvm::SmallString<16> CharBuffer; |
| 1631 | CharBuffer.resize(Tok.getLength()); |
| 1632 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1633 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1634 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1636 | Tok.getLocation(), PP); |
| 1637 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1638 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1640 | QualType Ty; |
| 1641 | if (!getLangOptions().CPlusPlus) |
| 1642 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 1643 | else if (Literal.isWide()) |
| 1644 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
| 1645 | else |
| 1646 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1647 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1648 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1649 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1650 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1653 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1654 | // 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] | 1655 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1656 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1657 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1658 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1659 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1660 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1662 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1663 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1664 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1665 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1666 | const char *ThisTokBegin = &IntegerBuffer[0]; |
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 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1669 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1672 | Tok.getLocation(), PP); |
| 1673 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1674 | return ExprError(); |
| 1675 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1676 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1678 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1679 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1680 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1681 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1682 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1683 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1684 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1685 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1686 | |
| 1687 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1688 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1689 | using llvm::APFloat; |
| 1690 | APFloat Val(Format); |
| 1691 | |
| 1692 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 1693 | |
| 1694 | // Overflow is always an error, but underflow is only an error if |
| 1695 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 1696 | if ((result & APFloat::opOverflow) || |
| 1697 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1698 | unsigned diagnostic; |
| 1699 | llvm::SmallVector<char, 20> buffer; |
| 1700 | if (result & APFloat::opOverflow) { |
| 1701 | diagnostic = diag::err_float_overflow; |
| 1702 | APFloat::getLargest(Format).toString(buffer); |
| 1703 | } else { |
| 1704 | diagnostic = diag::err_float_underflow; |
| 1705 | APFloat::getSmallest(Format).toString(buffer); |
| 1706 | } |
| 1707 | |
| 1708 | Diag(Tok.getLocation(), diagnostic) |
| 1709 | << Ty |
| 1710 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 1711 | } |
| 1712 | |
| 1713 | bool isExact = (result == APFloat::opOK); |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1714 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1716 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1717 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1718 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1719 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1720 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1721 | // long long is a C99 feature. |
| 1722 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1723 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1724 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1726 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1727 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1729 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1730 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1731 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1732 | Ty = Context.UnsignedLongLongTy; |
| 1733 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1734 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1735 | } else { |
| 1736 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1737 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1738 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1740 | // be an unsigned int. |
| 1741 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1742 | |
| 1743 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1744 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1745 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1746 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1747 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1748 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | // Does it fit in a unsigned int? |
| 1750 | if (ResultVal.isIntN(IntSize)) { |
| 1751 | // Does it fit in a signed int? |
| 1752 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1753 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1754 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1755 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1756 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1757 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1759 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1760 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1761 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1762 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | // Does it fit in a unsigned long? |
| 1765 | if (ResultVal.isIntN(LongSize)) { |
| 1766 | // Does it fit in a signed long? |
| 1767 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1768 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1769 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1770 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1771 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1775 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1776 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1777 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1778 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1779 | // Does it fit in a unsigned long long? |
| 1780 | if (ResultVal.isIntN(LongLongSize)) { |
| 1781 | // Does it fit in a signed long long? |
| 1782 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1783 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1784 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1785 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1786 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | } |
| 1788 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1789 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1790 | // If we still couldn't decide a type, we probably have something that |
| 1791 | // 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] | 1792 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1793 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1794 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1795 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1796 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1798 | if (ResultVal.getBitWidth() != Width) |
| 1799 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1800 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1801 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1804 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1805 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1807 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1808 | |
| 1809 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1812 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1813 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1814 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1815 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1816 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1820 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1821 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1822 | SourceLocation OpLoc, |
| 1823 | const SourceRange &ExprRange, |
| 1824 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1825 | if (exprType->isDependentType()) |
| 1826 | return false; |
| 1827 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1828 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1829 | // the result is the size of the referenced type." |
| 1830 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1831 | // result shall be the alignment of the referenced type." |
| 1832 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1833 | exprType = Ref->getPointeeType(); |
| 1834 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1835 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1836 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1837 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1838 | if (isSizeof) |
| 1839 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1840 | return false; |
| 1841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1843 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1844 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1845 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1846 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1847 | return false; |
| 1848 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1850 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 1851 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 1852 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1853 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1855 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1856 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1857 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1858 | << exprType << isSizeof << ExprRange; |
| 1859 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1862 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1865 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1866 | const SourceRange &ExprRange) { |
| 1867 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1868 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1870 | if (isa<DeclRefExpr>(E)) |
| 1871 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1872 | |
| 1873 | // Cannot know anything else if the expression is dependent. |
| 1874 | if (E->isTypeDependent()) |
| 1875 | return false; |
| 1876 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1877 | if (E->getBitField()) { |
| 1878 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1879 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1880 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1881 | |
| 1882 | // Alignment of a field access is always okay, so long as it isn't a |
| 1883 | // bit-field. |
| 1884 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1885 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1886 | return false; |
| 1887 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1888 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1889 | } |
| 1890 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1891 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | Action::OwningExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1893 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1894 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1895 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1896 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1897 | return ExprError(); |
| 1898 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1899 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1900 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1901 | if (!T->isDependentType() && |
| 1902 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1903 | return ExprError(); |
| 1904 | |
| 1905 | // 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] | 1906 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1907 | Context.getSizeType(), OpLoc, |
| 1908 | R.getEnd())); |
| 1909 | } |
| 1910 | |
| 1911 | /// \brief Build a sizeof or alignof expression given an expression |
| 1912 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | Action::OwningExprResult |
| 1914 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1915 | bool isSizeOf, SourceRange R) { |
| 1916 | // Verify that the operand is valid. |
| 1917 | bool isInvalid = false; |
| 1918 | if (E->isTypeDependent()) { |
| 1919 | // Delay type-checking for type-dependent expressions. |
| 1920 | } else if (!isSizeOf) { |
| 1921 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1922 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1923 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1924 | isInvalid = true; |
| 1925 | } else { |
| 1926 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1927 | } |
| 1928 | |
| 1929 | if (isInvalid) |
| 1930 | return ExprError(); |
| 1931 | |
| 1932 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1933 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1934 | Context.getSizeType(), OpLoc, |
| 1935 | R.getEnd())); |
| 1936 | } |
| 1937 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1938 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1939 | /// the same for @c alignof and @c __alignof |
| 1940 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1941 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1942 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1943 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1944 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1945 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1946 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1947 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1948 | TypeSourceInfo *TInfo; |
| 1949 | (void) GetTypeFromParser(TyOrEx, &TInfo); |
| 1950 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1953 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1954 | Action::OwningExprResult Result |
| 1955 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1956 | |
| 1957 | if (Result.isInvalid()) |
| 1958 | DeleteExpr(ArgEx); |
| 1959 | |
| 1960 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1963 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1964 | if (V->isTypeDependent()) |
| 1965 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1967 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1968 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1969 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1971 | // Otherwise they pass through real integer and floating point types here. |
| 1972 | if (V->getType()->isArithmeticType()) |
| 1973 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1975 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1976 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1977 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1978 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1982 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1983 | Action::OwningExprResult |
| 1984 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1985 | tok::TokenKind Kind, ExprArg Input) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1986 | UnaryOperator::Opcode Opc; |
| 1987 | switch (Kind) { |
| 1988 | default: assert(0 && "Unknown unary op!"); |
| 1989 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1990 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1991 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1992 | |
Eli Friedman | e4216e9 | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 1993 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1996 | Action::OwningExprResult |
| 1997 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1998 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1999 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2000 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 2001 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2002 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 2003 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2005 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2006 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 2007 | Base.release(); |
| 2008 | Idx.release(); |
| 2009 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 2010 | Context.DependentTy, RLoc)); |
| 2011 | } |
| 2012 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2014 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2015 | LHSExp->getType()->isEnumeralType() || |
| 2016 | RHSExp->getType()->isRecordType() || |
| 2017 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2018 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2021 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | Action::OwningExprResult |
| 2026 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 2027 | ExprArg Idx, SourceLocation RLoc) { |
| 2028 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 2029 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 2030 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2031 | // Perform default conversions. |
| 2032 | DefaultFunctionArrayConversion(LHSExp); |
| 2033 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2035 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2036 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2037 | // 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] | 2038 | // 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] | 2039 | // 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] | 2040 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2041 | Expr *BaseExpr, *IndexExpr; |
| 2042 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2043 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2044 | BaseExpr = LHSExp; |
| 2045 | IndexExpr = RHSExp; |
| 2046 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2047 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2048 | BaseExpr = LHSExp; |
| 2049 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2050 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2051 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2052 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2053 | BaseExpr = RHSExp; |
| 2054 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2055 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2057 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2058 | BaseExpr = LHSExp; |
| 2059 | IndexExpr = RHSExp; |
| 2060 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2062 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2063 | // Handle the uncommon case of "123[Ptr]". |
| 2064 | BaseExpr = RHSExp; |
| 2065 | IndexExpr = LHSExp; |
| 2066 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2067 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2068 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2069 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2070 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2071 | // FIXME: need to deal with const... |
| 2072 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2073 | } else if (LHSTy->isArrayType()) { |
| 2074 | // If we see an array that wasn't promoted by |
| 2075 | // DefaultFunctionArrayConversion, it must be an array that |
| 2076 | // wasn't promoted because of the C90 rule that doesn't |
| 2077 | // allow promoting non-lvalue arrays. Warn, then |
| 2078 | // force the promotion here. |
| 2079 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2080 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2081 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 2082 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2083 | LHSTy = LHSExp->getType(); |
| 2084 | |
| 2085 | BaseExpr = LHSExp; |
| 2086 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2087 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2088 | } else if (RHSTy->isArrayType()) { |
| 2089 | // Same as previous, except for 123[f().a] case |
| 2090 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2091 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2092 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 2093 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2094 | RHSTy = RHSExp->getType(); |
| 2095 | |
| 2096 | BaseExpr = RHSExp; |
| 2097 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2098 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2100 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2101 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2102 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2103 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2104 | if (!(IndexExpr->getType()->isIntegerType() && |
| 2105 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2106 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2107 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2108 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2109 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2110 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2111 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2112 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2113 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2114 | // 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] | 2115 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2116 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2117 | // incomplete types are not object types. |
| 2118 | if (ResultType->isFunctionType()) { |
| 2119 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2120 | << ResultType << BaseExpr->getSourceRange(); |
| 2121 | return ExprError(); |
| 2122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2124 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2126 | PDiag(diag::err_subscript_incomplete_type) |
| 2127 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2128 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2130 | // Diagnose bad cases where we step over interface counts. |
| 2131 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 2132 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2133 | << ResultType << BaseExpr->getSourceRange(); |
| 2134 | return ExprError(); |
| 2135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2137 | Base.release(); |
| 2138 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2139 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2140 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2143 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2144 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2146 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2147 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2148 | // see FIXME there. |
| 2149 | // |
| 2150 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2151 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2152 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2153 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2154 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2155 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2156 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2157 | // 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] | 2158 | // special names that indicate a subset of exactly half the elements are |
| 2159 | // to be selected. |
| 2160 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2161 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2162 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2163 | // 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] | 2164 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Check that we've found one of the special components, or that the component |
| 2167 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2168 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2169 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2170 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2171 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2172 | do |
| 2173 | compStr++; |
| 2174 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2175 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2176 | do |
| 2177 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2178 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2179 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2180 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2181 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2182 | // We didn't get to the end of the string. This means the component names |
| 2183 | // 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] | 2184 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 2185 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2186 | return QualType(); |
| 2187 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2188 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2189 | // Ensure no component accessor exceeds the width of the vector type it |
| 2190 | // operates on. |
| 2191 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2192 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2193 | |
| 2194 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2195 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2196 | |
| 2197 | while (*compStr) { |
| 2198 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2199 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2200 | << baseType << SourceRange(CompLoc); |
| 2201 | return QualType(); |
| 2202 | } |
| 2203 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2204 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2205 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2206 | // 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] | 2207 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2208 | // 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] | 2209 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2210 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2211 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2212 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2213 | if (HexSwizzle) |
| 2214 | CompSize--; |
| 2215 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2216 | if (CompSize == 1) |
| 2217 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2218 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2219 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2220 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2221 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2222 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2223 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2224 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2225 | } |
| 2226 | 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] | 2227 | } |
| 2228 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2229 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2230 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2231 | const Selector &Sel, |
| 2232 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2234 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2235 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2236 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2237 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2239 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2240 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2241 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2242 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2243 | return D; |
| 2244 | } |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2248 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2249 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2250 | const Selector &Sel, |
| 2251 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2252 | // Check protocols on qualified interfaces. |
| 2253 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2254 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2255 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2256 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2257 | GDecl = PD; |
| 2258 | break; |
| 2259 | } |
| 2260 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2261 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2262 | GDecl = OMD; |
| 2263 | break; |
| 2264 | } |
| 2265 | } |
| 2266 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2267 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2268 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2269 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2270 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2271 | if (GDecl) |
| 2272 | return GDecl; |
| 2273 | } |
| 2274 | } |
| 2275 | return GDecl; |
| 2276 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2277 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2278 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2279 | Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType, |
| 2280 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2281 | const CXXScopeSpec &SS, |
| 2282 | NamedDecl *FirstQualifierInScope, |
| 2283 | DeclarationName Name, SourceLocation NameLoc, |
| 2284 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2285 | Expr *BaseExpr = Base.takeAs<Expr>(); |
| 2286 | |
| 2287 | // Even in dependent contexts, try to diagnose base expressions with |
| 2288 | // obviously wrong types, e.g.: |
| 2289 | // |
| 2290 | // T* t; |
| 2291 | // t.f; |
| 2292 | // |
| 2293 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2294 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2295 | // allows this, while still reporting an error if T is a struct pointer. |
| 2296 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2297 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2298 | if (PT && (!getLangOptions().ObjC1 || |
| 2299 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2300 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2301 | Diag(NameLoc, diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2302 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2303 | return ExprError(); |
| 2304 | } |
| 2305 | } |
| 2306 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2307 | assert(BaseType->isDependentType() || Name.isDependentName()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2308 | |
| 2309 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2310 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2311 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2312 | IsArrow, OpLoc, |
| 2313 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 2314 | SS.getRange(), |
| 2315 | FirstQualifierInScope, |
| 2316 | Name, NameLoc, |
| 2317 | TemplateArgs)); |
| 2318 | } |
| 2319 | |
| 2320 | /// We know that the given qualified member reference points only to |
| 2321 | /// declarations which do not belong to the static type of the base |
| 2322 | /// expression. Diagnose the problem. |
| 2323 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2324 | Expr *BaseExpr, |
| 2325 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2326 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2327 | const LookupResult &R) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2328 | // If this is an implicit member access, use a different set of |
| 2329 | // diagnostics. |
| 2330 | if (!BaseExpr) |
| 2331 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2332 | |
| 2333 | // FIXME: this is an exceedingly lame diagnostic for some of the more |
| 2334 | // complicated cases here. |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2335 | DeclContext *DC = R.getRepresentativeDecl()->getDeclContext(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2336 | SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual) |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2337 | << SS.getRange() << DC << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | // Check whether the declarations we found through a nested-name |
| 2341 | // specifier in a member expression are actually members of the base |
| 2342 | // type. The restriction here is: |
| 2343 | // |
| 2344 | // C++ [expr.ref]p2: |
| 2345 | // ... In these cases, the id-expression shall name a |
| 2346 | // member of the class or of one of its base classes. |
| 2347 | // |
| 2348 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2349 | // an unrelated class, and for us to find an overload set including |
| 2350 | // decls from classes which are not superclasses, as long as the decl |
| 2351 | // we actually pick through overload resolution is from a superclass. |
| 2352 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2353 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2354 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2355 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2356 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2357 | if (!BaseRT) { |
| 2358 | // We can't check this yet because the base type is still |
| 2359 | // dependent. |
| 2360 | assert(BaseType->isDependentType()); |
| 2361 | return false; |
| 2362 | } |
| 2363 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2364 | |
| 2365 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2366 | // If this is an implicit member reference and we find a |
| 2367 | // non-instance member, it's not an error. |
| 2368 | if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl())) |
| 2369 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2370 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2371 | // Note that we use the DC of the decl, not the underlying decl. |
| 2372 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext()); |
| 2373 | while (RecordD->isAnonymousStructOrUnion()) |
| 2374 | RecordD = cast<CXXRecordDecl>(RecordD->getParent()); |
| 2375 | |
| 2376 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| 2377 | MemberRecord.insert(RecordD->getCanonicalDecl()); |
| 2378 | |
| 2379 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2380 | return false; |
| 2381 | } |
| 2382 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2383 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2384 | return true; |
| 2385 | } |
| 2386 | |
| 2387 | static bool |
| 2388 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2389 | SourceRange BaseRange, const RecordType *RTy, |
| 2390 | SourceLocation OpLoc, const CXXScopeSpec &SS) { |
| 2391 | RecordDecl *RDecl = RTy->getDecl(); |
| 2392 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 2393 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2394 | << BaseRange)) |
| 2395 | return true; |
| 2396 | |
| 2397 | DeclContext *DC = RDecl; |
| 2398 | if (SS.isSet()) { |
| 2399 | // If the member name was a qualified-id, look into the |
| 2400 | // nested-name-specifier. |
| 2401 | DC = SemaRef.computeDeclContext(SS, false); |
| 2402 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2403 | if (SemaRef.RequireCompleteDeclContext(SS)) { |
| 2404 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2405 | << SS.getRange() << DC; |
| 2406 | return true; |
| 2407 | } |
| 2408 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2409 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2410 | |
| 2411 | if (!isa<TypeDecl>(DC)) { |
| 2412 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2413 | << DC << SS.getRange(); |
| 2414 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2418 | // The record definition is complete, now look up the member. |
| 2419 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2420 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2421 | if (!R.empty()) |
| 2422 | return false; |
| 2423 | |
| 2424 | // We didn't find anything with the given name, so try to correct |
| 2425 | // for typos. |
| 2426 | DeclarationName Name = R.getLookupName(); |
| 2427 | if (SemaRef.CorrectTypo(R, 0, &SS, DC) && |
| 2428 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 2429 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 2430 | << Name << DC << R.getLookupName() << SS.getRange() |
| 2431 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2432 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2433 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 2434 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 2435 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2436 | return false; |
| 2437 | } else { |
| 2438 | R.clear(); |
| 2439 | } |
| 2440 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
| 2443 | |
| 2444 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2445 | Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2446 | SourceLocation OpLoc, bool IsArrow, |
| 2447 | const CXXScopeSpec &SS, |
| 2448 | NamedDecl *FirstQualifierInScope, |
| 2449 | DeclarationName Name, SourceLocation NameLoc, |
| 2450 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2451 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2452 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2453 | if (BaseType->isDependentType() || |
| 2454 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2455 | return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2456 | IsArrow, OpLoc, |
| 2457 | SS, FirstQualifierInScope, |
| 2458 | Name, NameLoc, |
| 2459 | TemplateArgs); |
| 2460 | |
| 2461 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2462 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2463 | // Implicit member accesses. |
| 2464 | if (!Base) { |
| 2465 | QualType RecordTy = BaseType; |
| 2466 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2467 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2468 | RecordTy->getAs<RecordType>(), |
| 2469 | OpLoc, SS)) |
| 2470 | return ExprError(); |
| 2471 | |
| 2472 | // Explicit member accesses. |
| 2473 | } else { |
| 2474 | OwningExprResult Result = |
| 2475 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2476 | SS, /*ObjCImpDecl*/ DeclPtrTy()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2477 | |
| 2478 | if (Result.isInvalid()) { |
| 2479 | Owned(Base); |
| 2480 | return ExprError(); |
| 2481 | } |
| 2482 | |
| 2483 | if (Result.get()) |
| 2484 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2487 | return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2488 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 2489 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2493 | Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType, |
| 2494 | SourceLocation OpLoc, bool IsArrow, |
| 2495 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2496 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2497 | LookupResult &R, |
| 2498 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2499 | Expr *BaseExpr = Base.takeAs<Expr>(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2500 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2501 | if (IsArrow) { |
| 2502 | assert(BaseType->isPointerType()); |
| 2503 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2504 | } |
| 2505 | |
| 2506 | NestedNameSpecifier *Qualifier = |
| 2507 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 2508 | DeclarationName MemberName = R.getLookupName(); |
| 2509 | SourceLocation MemberLoc = R.getNameLoc(); |
| 2510 | |
| 2511 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2512 | return ExprError(); |
| 2513 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2514 | if (R.empty()) { |
| 2515 | // Rederive where we looked up. |
| 2516 | DeclContext *DC = (SS.isSet() |
| 2517 | ? computeDeclContext(SS, false) |
| 2518 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2519 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2520 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2521 | << MemberName << DC |
| 2522 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2523 | return ExprError(); |
| 2524 | } |
| 2525 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2526 | // Diagnose lookups that find only declarations from a non-base |
| 2527 | // type. This is possible for either qualified lookups (which may |
| 2528 | // have been qualified with an unrelated type) or implicit member |
| 2529 | // expressions (which were found with unqualified lookup and thus |
| 2530 | // may have come from an enclosing scope). Note that it's okay for |
| 2531 | // lookup to find declarations from a non-base type as long as those |
| 2532 | // aren't the ones picked by overload resolution. |
| 2533 | if ((SS.isSet() || !BaseExpr || |
| 2534 | (isa<CXXThisExpr>(BaseExpr) && |
| 2535 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
| 2536 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2537 | return ExprError(); |
| 2538 | |
| 2539 | // Construct an unresolved result if we in fact got an unresolved |
| 2540 | // result. |
| 2541 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2542 | bool Dependent = |
John McCall | 410a3f3 | 2009-12-19 02:05:44 +0000 | [diff] [blame] | 2543 | BaseExprType->isDependentType() || |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2544 | R.isUnresolvableResult() || |
| 2545 | UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2546 | |
| 2547 | UnresolvedMemberExpr *MemExpr |
| 2548 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2549 | R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2550 | BaseExpr, BaseExprType, |
| 2551 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2552 | Qualifier, SS.getRange(), |
| 2553 | MemberName, MemberLoc, |
| 2554 | TemplateArgs); |
| 2555 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2556 | MemExpr->addDecl(*I); |
| 2557 | |
| 2558 | return Owned(MemExpr); |
| 2559 | } |
| 2560 | |
| 2561 | assert(R.isSingleResult()); |
| 2562 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2563 | |
| 2564 | // FIXME: diagnose the presence of template arguments now. |
| 2565 | |
| 2566 | // If the decl being referenced had an error, return an error for this |
| 2567 | // sub-expr without emitting another error, in order to avoid cascading |
| 2568 | // error cases. |
| 2569 | if (MemberDecl->isInvalidDecl()) |
| 2570 | return ExprError(); |
| 2571 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2572 | // Handle the implicit-member-access case. |
| 2573 | if (!BaseExpr) { |
| 2574 | // If this is not an instance member, convert to a non-member access. |
| 2575 | if (!IsInstanceMember(MemberDecl)) |
| 2576 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl); |
| 2577 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2578 | SourceLocation Loc = R.getNameLoc(); |
| 2579 | if (SS.getRange().isValid()) |
| 2580 | Loc = SS.getRange().getBegin(); |
| 2581 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2584 | bool ShouldCheckUse = true; |
| 2585 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2586 | // Don't diagnose the use of a virtual member function unless it's |
| 2587 | // explicitly qualified. |
| 2588 | if (MD->isVirtual() && !SS.isSet()) |
| 2589 | ShouldCheckUse = false; |
| 2590 | } |
| 2591 | |
| 2592 | // Check the use of this member. |
| 2593 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2594 | Owned(BaseExpr); |
| 2595 | return ExprError(); |
| 2596 | } |
| 2597 | |
| 2598 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2599 | // We may have found a field within an anonymous union or struct |
| 2600 | // (C++ [class.union]). |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2601 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2602 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2603 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2604 | BaseExpr, OpLoc); |
| 2605 | |
| 2606 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2607 | QualType MemberType = FD->getType(); |
| 2608 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2609 | MemberType = Ref->getPointeeType(); |
| 2610 | else { |
| 2611 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2612 | BaseQuals.removeObjCGCAttr(); |
| 2613 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2614 | |
| 2615 | Qualifiers MemberQuals |
| 2616 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2617 | |
| 2618 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2619 | if (Combined != MemberQuals) |
| 2620 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2621 | } |
| 2622 | |
| 2623 | MarkDeclarationReferenced(MemberLoc, FD); |
| 2624 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2625 | return ExprError(); |
| 2626 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2627 | FD, MemberLoc, MemberType)); |
| 2628 | } |
| 2629 | |
| 2630 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2631 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2632 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2633 | Var, MemberLoc, |
| 2634 | Var->getType().getNonReferenceType())); |
| 2635 | } |
| 2636 | |
| 2637 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2638 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2639 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2640 | MemberFn, MemberLoc, |
| 2641 | MemberFn->getType())); |
| 2642 | } |
| 2643 | |
| 2644 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2645 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2646 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2647 | Enum, MemberLoc, Enum->getType())); |
| 2648 | } |
| 2649 | |
| 2650 | Owned(BaseExpr); |
| 2651 | |
| 2652 | if (isa<TypeDecl>(MemberDecl)) |
| 2653 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2654 | << MemberName << int(IsArrow)); |
| 2655 | |
| 2656 | // We found a declaration kind that we didn't expect. This is a |
| 2657 | // generic error message that tells the user that she can't refer |
| 2658 | // to this member with '.' or '->'. |
| 2659 | return ExprError(Diag(MemberLoc, |
| 2660 | diag::err_typecheck_member_reference_unknown) |
| 2661 | << MemberName << int(IsArrow)); |
| 2662 | } |
| 2663 | |
| 2664 | /// Look up the given member of the given non-type-dependent |
| 2665 | /// expression. This can return in one of two ways: |
| 2666 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2667 | /// assume that lookup was performed and the results written into |
| 2668 | /// the provided structure. It will take over from there. |
| 2669 | /// * Otherwise, the returned expression will be produced in place of |
| 2670 | /// an ordinary member expression. |
| 2671 | /// |
| 2672 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2673 | /// fixed for ObjC++. |
| 2674 | Sema::OwningExprResult |
| 2675 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2676 | bool &IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2677 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2678 | DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2679 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2680 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2681 | // Perform default conversions. |
| 2682 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2683 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2684 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2685 | assert(!BaseType->isDependentType()); |
| 2686 | |
| 2687 | DeclarationName MemberName = R.getLookupName(); |
| 2688 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2689 | |
| 2690 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2691 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2692 | // function. Suggest the addition of those parentheses, build the |
| 2693 | // call, and continue on. |
| 2694 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 2695 | if (const FunctionProtoType *Fun |
| 2696 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 2697 | QualType ResultTy = Fun->getResultType(); |
| 2698 | if (Fun->getNumArgs() == 0 && |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2699 | ((!IsArrow && ResultTy->isRecordType()) || |
| 2700 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2701 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 2702 | ->isRecordType()))) { |
| 2703 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2704 | Diag(Loc, diag::err_member_reference_needs_call) |
| 2705 | << QualType(Fun, 0) |
| 2706 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2707 | |
| 2708 | OwningExprResult NewBase |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2709 | = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc, |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2710 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 2711 | if (NewBase.isInvalid()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2712 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2713 | |
| 2714 | BaseExpr = NewBase.takeAs<Expr>(); |
| 2715 | DefaultFunctionArrayConversion(BaseExpr); |
| 2716 | BaseType = BaseExpr->getType(); |
| 2717 | } |
| 2718 | } |
| 2719 | } |
| 2720 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2721 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2722 | // use that. |
| 2723 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2724 | if (IsArrow) { |
| 2725 | // Handle the following exceptional case PObj->isa. |
| 2726 | if (const ObjCObjectPointerType *OPT = |
| 2727 | BaseType->getAs<ObjCObjectPointerType>()) { |
| 2728 | if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && |
| 2729 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2730 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 2731 | Context.getObjCClassType())); |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2732 | } |
| 2733 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2734 | // We have an 'id' type. Rather than fall through, we check if this |
| 2735 | // is a reference to 'isa'. |
| 2736 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2737 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2738 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2739 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2740 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2741 | |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 2742 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2743 | // use that. |
| 2744 | if (Context.isObjCSelType(BaseType)) { |
| 2745 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 2746 | // is a reference to 'sel_id'. |
| 2747 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 2748 | BaseType = Context.ObjCSelRedefinitionType; |
| 2749 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
| 2750 | } |
| 2751 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2752 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2753 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2754 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2755 | // Handle properties on ObjC 'Class' types. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2756 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2757 | // Also must look for a getter name which uses property syntax. |
| 2758 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2759 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 2760 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2761 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2762 | ObjCMethodDecl *Getter; |
| 2763 | // FIXME: need to also look locally in the implementation. |
| 2764 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2765 | // Check the use of this method. |
| 2766 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2767 | return ExprError(); |
| 2768 | } |
| 2769 | // If we found a getter then this may be a valid dot-reference, we |
| 2770 | // will look for the matching setter, in case it is needed. |
| 2771 | Selector SetterSel = |
| 2772 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2773 | PP.getSelectorTable(), Member); |
| 2774 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2775 | if (!Setter) { |
| 2776 | // If this reference is in an @implementation, also check for 'private' |
| 2777 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2778 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2779 | } |
| 2780 | // Look through local category implementations associated with the class. |
| 2781 | if (!Setter) |
| 2782 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 2783 | |
| 2784 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2785 | return ExprError(); |
| 2786 | |
| 2787 | if (Getter || Setter) { |
| 2788 | QualType PType; |
| 2789 | |
| 2790 | if (Getter) |
| 2791 | PType = Getter->getResultType(); |
| 2792 | else |
| 2793 | // Get the expression type from Setter's incoming parameter. |
| 2794 | PType = (*(Setter->param_end() -1))->getType(); |
| 2795 | // FIXME: we must check that the setter has property type. |
| 2796 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 2797 | PType, |
| 2798 | Setter, MemberLoc, BaseExpr)); |
| 2799 | } |
| 2800 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2801 | << MemberName << BaseType); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | if (BaseType->isObjCClassType() && |
| 2806 | BaseType != Context.ObjCClassRedefinitionType) { |
| 2807 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2808 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2811 | if (IsArrow) { |
| 2812 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2813 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2814 | else if (BaseType->isObjCObjectPointerType()) |
| 2815 | ; |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2816 | else if (BaseType->isRecordType()) { |
| 2817 | // Recover from arrow accesses to records, e.g.: |
| 2818 | // struct MyRecord foo; |
| 2819 | // foo->bar |
| 2820 | // This is actually well-formed in C++ if MyRecord has an |
| 2821 | // overloaded operator->, but that should have been dealt with |
| 2822 | // by now. |
| 2823 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2824 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2825 | << CodeModificationHint::CreateReplacement(OpLoc, "."); |
| 2826 | IsArrow = false; |
| 2827 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2828 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 2829 | << BaseType << BaseExpr->getSourceRange(); |
| 2830 | return ExprError(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2831 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2832 | } else { |
| 2833 | // Recover from dot accesses to pointers, e.g.: |
| 2834 | // type *foo; |
| 2835 | // foo.bar |
| 2836 | // This is actually well-formed in two cases: |
| 2837 | // - 'type' is an Objective C type |
| 2838 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 2839 | // the appropriate pointer type |
| 2840 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 2841 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 2842 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 2843 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2844 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2845 | << CodeModificationHint::CreateReplacement(OpLoc, "->"); |
| 2846 | BaseType = PT->getPointeeType(); |
| 2847 | IsArrow = true; |
| 2848 | } |
| 2849 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2850 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2851 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2852 | // Handle field access to simple records. This also handles access |
| 2853 | // to fields of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2854 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2855 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 2856 | RTy, OpLoc, SS)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2857 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2858 | return Owned((Expr*) 0); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2859 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2861 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2862 | // into a record type was handled above, any destructor we see here is a |
| 2863 | // pseudo-destructor. |
| 2864 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2865 | // C++ [expr.pseudo]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | // The left hand side of the dot operator shall be of scalar type. The |
| 2867 | // 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] | 2868 | // type. |
| 2869 | if (!BaseType->isScalarType()) |
| 2870 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2871 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2873 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2874 | // same as the object type. |
| 2875 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2876 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2877 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2878 | << BaseType << MemberName.getCXXNameType() |
| 2879 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | |
| 2881 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2882 | // the form |
| 2883 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2885 | // |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2886 | // shall designate the same scalar type. |
| 2887 | // |
| 2888 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2889 | // isn't checked here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2891 | // FIXME: We've lost the precise spelling of the type by going through |
| 2892 | // DeclarationName. Can we do better? |
| 2893 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2894 | IsArrow, OpLoc, |
| 2895 | (NestedNameSpecifier *) SS.getScopeRep(), |
| 2896 | SS.getRange(), |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2897 | MemberName.getCXXNameType(), |
| 2898 | MemberLoc)); |
| 2899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2901 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2902 | // (*Obj).ivar. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2903 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2904 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2905 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | const ObjCInterfaceType *IFaceT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2907 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2908 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2909 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2910 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2911 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2912 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2913 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2915 | if (!IV) { |
| 2916 | // Attempt to correct for typos in ivar names. |
| 2917 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 2918 | LookupMemberName); |
| 2919 | if (CorrectTypo(Res, 0, 0, IDecl) && |
| 2920 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 2921 | Diag(R.getNameLoc(), |
| 2922 | diag::err_typecheck_member_reference_ivar_suggest) |
| 2923 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 2924 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2925 | IV->getNameAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2926 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 2927 | << IV->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2931 | if (IV) { |
| 2932 | // If the decl being referenced had an error, return an error for this |
| 2933 | // sub-expr without emitting another error, in order to avoid cascading |
| 2934 | // error cases. |
| 2935 | if (IV->isInvalidDecl()) |
| 2936 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2937 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2938 | // Check whether we can reference this field. |
| 2939 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2940 | return ExprError(); |
| 2941 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2942 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2943 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2944 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2945 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2946 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2947 | // Case of a c-function declared inside an objc implementation. |
| 2948 | // FIXME: For a c-style function nested inside an objc implementation |
| 2949 | // class, there is no implementation context available, so we pass |
| 2950 | // down the context as argument to this routine. Ideally, this context |
| 2951 | // need be passed down in the AST node and somehow calculated from the |
| 2952 | // AST for a function decl. |
| 2953 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2955 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2956 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2957 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2958 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2959 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | |
| 2962 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2963 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2964 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2966 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2967 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2968 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2970 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2971 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2972 | |
| 2973 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2974 | MemberLoc, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2975 | IsArrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2976 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2977 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2978 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2979 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2980 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2981 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2982 | // Handle properties on 'id' and qualified "id". |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2983 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 2984 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2985 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2986 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2988 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2989 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2990 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2991 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2992 | // Check the use of this declaration |
| 2993 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2994 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2996 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2997 | MemberLoc, BaseExpr)); |
| 2998 | } |
| 2999 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3000 | // Check the use of this method. |
| 3001 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3002 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3004 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | OMD->getResultType(), |
| 3006 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3007 | NULL, 0)); |
| 3008 | } |
| 3009 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3010 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3011 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3012 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3013 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 3014 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 3015 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3016 | const ObjCObjectPointerType *OPT; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3017 | if (!IsArrow && (OPT = BaseType->getAsObjCInterfacePointerType())) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3018 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 3019 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3020 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3021 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3022 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3023 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3024 | // Check whether we can reference this property. |
| 3025 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3026 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 3027 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3028 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3029 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 3030 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 3031 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 3032 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3033 | MemberLoc, BaseExpr)); |
| 3034 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3035 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3036 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3037 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3038 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3039 | // Check whether we can reference this property. |
| 3040 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3041 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3042 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3043 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3044 | MemberLoc, BaseExpr)); |
| 3045 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3046 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 3047 | // selector is implemented. |
| 3048 | |
| 3049 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 3050 | // shared with the code in ActOnInstanceMessage. |
| 3051 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3052 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3053 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3054 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3055 | // If this reference is in an @implementation, check for 'private' methods. |
| 3056 | if (!Getter) |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3057 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3058 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 3059 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3060 | if (!Getter) |
| 3061 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3062 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3063 | // Check if we can reference this property. |
| 3064 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3065 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3066 | } |
| 3067 | // If we found a getter then this may be a valid dot-reference, we |
| 3068 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | Selector SetterSel = |
| 3070 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3071 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3072 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3073 | if (!Setter) { |
| 3074 | // If this reference is in an @implementation, also check for 'private' |
| 3075 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3076 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3077 | } |
| 3078 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3079 | if (!Setter) |
| 3080 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3081 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3082 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3083 | return ExprError(); |
| 3084 | |
| 3085 | if (Getter || Setter) { |
| 3086 | QualType PType; |
| 3087 | |
| 3088 | if (Getter) |
| 3089 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 3090 | else |
| 3091 | // Get the expression type from Setter's incoming parameter. |
| 3092 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3093 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 3094 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3095 | Setter, MemberLoc, BaseExpr)); |
| 3096 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Attempt to correct for typos in property names. |
| 3099 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3100 | LookupOrdinaryName); |
| 3101 | if (CorrectTypo(Res, 0, 0, IFace, false, OPT) && |
| 3102 | Res.getAsSingle<ObjCPropertyDecl>()) { |
| 3103 | Diag(R.getNameLoc(), diag::err_property_not_found_suggest) |
| 3104 | << MemberName << BaseType << Res.getLookupName() |
| 3105 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 3106 | Res.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3107 | ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>(); |
| 3108 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 3109 | << Property->getDeclName(); |
| 3110 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3111 | return LookupMemberExpr(Res, BaseExpr, IsArrow, OpLoc, SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3112 | ObjCImpDecl); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3115 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3116 | << MemberName << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 3117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3119 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3120 | if (!IsArrow && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3121 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3122 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3123 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 3124 | Context.getObjCClassType())); |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3125 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3126 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3127 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3128 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3129 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 3130 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3131 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3132 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3133 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3134 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3136 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 3137 | << BaseType << BaseExpr->getSourceRange(); |
| 3138 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3139 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3142 | static Sema::OwningExprResult DiagnoseDtorReference(Sema &SemaRef, |
| 3143 | SourceLocation NameLoc, |
| 3144 | Sema::ExprArg MemExpr) { |
| 3145 | Expr *E = (Expr *) MemExpr.get(); |
| 3146 | SourceLocation ExpectedLParenLoc = SemaRef.PP.getLocForEndOfToken(NameLoc); |
| 3147 | SemaRef.Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 3148 | << isa<CXXPseudoDestructorExpr>(E) |
| 3149 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 3150 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3151 | return SemaRef.ActOnCallExpr(/*Scope*/ 0, |
| 3152 | move(MemExpr), |
| 3153 | /*LPLoc*/ ExpectedLParenLoc, |
| 3154 | Sema::MultiExprArg(SemaRef, 0, 0), |
| 3155 | /*CommaLocs*/ 0, |
| 3156 | /*RPLoc*/ ExpectedLParenLoc); |
| 3157 | } |
| 3158 | |
| 3159 | /// The main callback when the parser finds something like |
| 3160 | /// expression . [nested-name-specifier] identifier |
| 3161 | /// expression -> [nested-name-specifier] identifier |
| 3162 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3163 | /// possibilities, including destructor and operator references. |
| 3164 | /// |
| 3165 | /// \param OpKind either tok::arrow or tok::period |
| 3166 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3167 | /// is used to diagnose mis-uses of special members that can |
| 3168 | /// only be called |
| 3169 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3170 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3171 | /// aren't properly put in the context chain |
| 3172 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg BaseArg, |
| 3173 | SourceLocation OpLoc, |
| 3174 | tok::TokenKind OpKind, |
| 3175 | const CXXScopeSpec &SS, |
| 3176 | UnqualifiedId &Id, |
| 3177 | DeclPtrTy ObjCImpDecl, |
| 3178 | bool HasTrailingLParen) { |
| 3179 | if (SS.isSet() && SS.isInvalid()) |
| 3180 | return ExprError(); |
| 3181 | |
| 3182 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3183 | |
| 3184 | // Decompose the name into its component parts. |
| 3185 | DeclarationName Name; |
| 3186 | SourceLocation NameLoc; |
| 3187 | const TemplateArgumentListInfo *TemplateArgs; |
| 3188 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 3189 | Name, NameLoc, TemplateArgs); |
| 3190 | |
| 3191 | bool IsArrow = (OpKind == tok::arrow); |
| 3192 | |
| 3193 | NamedDecl *FirstQualifierInScope |
| 3194 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3195 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3196 | |
| 3197 | // This is a postfix expression, so get rid of ParenListExprs. |
| 3198 | BaseArg = MaybeConvertParenListExprToParenExpr(S, move(BaseArg)); |
| 3199 | |
| 3200 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 3201 | OwningExprResult Result(*this); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3202 | if (Base->getType()->isDependentType() || Name.isDependentName()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3203 | Result = ActOnDependentMemberExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3204 | IsArrow, OpLoc, |
| 3205 | SS, FirstQualifierInScope, |
| 3206 | Name, NameLoc, |
| 3207 | TemplateArgs); |
| 3208 | } else { |
| 3209 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
| 3210 | if (TemplateArgs) { |
| 3211 | // Re-use the lookup done for the template name. |
| 3212 | DecomposeTemplateName(R, Id); |
| 3213 | } else { |
| 3214 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3215 | SS, ObjCImpDecl); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3216 | |
| 3217 | if (Result.isInvalid()) { |
| 3218 | Owned(Base); |
| 3219 | return ExprError(); |
| 3220 | } |
| 3221 | |
| 3222 | if (Result.get()) { |
| 3223 | // The only way a reference to a destructor can be used is to |
| 3224 | // immediately call it, which falls into this case. If the |
| 3225 | // next token is not a '(', produce a diagnostic and build the |
| 3226 | // call now. |
| 3227 | if (!HasTrailingLParen && |
| 3228 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| 3229 | return DiagnoseDtorReference(*this, NameLoc, move(Result)); |
| 3230 | |
| 3231 | return move(Result); |
| 3232 | } |
| 3233 | } |
| 3234 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3235 | Result = BuildMemberReferenceExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3236 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3237 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3243 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 3244 | FunctionDecl *FD, |
| 3245 | ParmVarDecl *Param) { |
| 3246 | if (Param->hasUnparsedDefaultArg()) { |
| 3247 | Diag (CallLoc, |
| 3248 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3249 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3250 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3251 | diag::note_default_argument_declared_here); |
| 3252 | } else { |
| 3253 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3254 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 3255 | |
| 3256 | // Instantiate the expression. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3257 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3258 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3260 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3261 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3262 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3263 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3265 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3267 | // Check the expression as an initializer for the parameter. |
| 3268 | InitializedEntity Entity |
| 3269 | = InitializedEntity::InitializeParameter(Param); |
| 3270 | InitializationKind Kind |
| 3271 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3272 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3273 | Expr *ResultE = Result.takeAs<Expr>(); |
| 3274 | |
| 3275 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3276 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3277 | MultiExprArg(*this, (void**)&ResultE, 1)); |
| 3278 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3279 | return ExprError(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3280 | |
| 3281 | // Build the default argument expression. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3282 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3283 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3286 | // If the default expression creates temporaries, we need to |
| 3287 | // push them to the current stack of expression temporaries so they'll |
| 3288 | // be properly destroyed. |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3289 | // FIXME: We should really be rebuilding the default argument with new |
| 3290 | // bound temporaries; see the comment in PR5810. |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 3291 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3292 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3296 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3299 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3300 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3301 | /// function prototype Proto. Call is the call expression itself, and |
| 3302 | /// Fn is the function expression. For a C++ member function, this |
| 3303 | /// routine does not attempt to convert the object argument. Returns |
| 3304 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3305 | bool |
| 3306 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3307 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3308 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3309 | Expr **Args, unsigned NumArgs, |
| 3310 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3311 | // 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] | 3312 | // assignment, to the types of the corresponding parameter, ... |
| 3313 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3314 | bool Invalid = false; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3316 | // If too few arguments are available (and we don't have default |
| 3317 | // arguments for the remaining parameters), don't make the call. |
| 3318 | if (NumArgs < NumArgsInProto) { |
| 3319 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3320 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 3321 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3322 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | // If too many are passed and not variadic, error on the extras and drop |
| 3326 | // them. |
| 3327 | if (NumArgs > NumArgsInProto) { |
| 3328 | if (!Proto->isVariadic()) { |
| 3329 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3330 | diag::err_typecheck_call_too_many_args) |
| 3331 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 3332 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3333 | Args[NumArgs-1]->getLocEnd()); |
| 3334 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3335 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3336 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3337 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3338 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3339 | llvm::SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3340 | VariadicCallType CallType = |
| 3341 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3342 | if (Fn->getType()->isBlockPointerType()) |
| 3343 | CallType = VariadicBlock; // Block |
| 3344 | else if (isa<MemberExpr>(Fn)) |
| 3345 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3346 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3347 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3348 | if (Invalid) |
| 3349 | return true; |
| 3350 | unsigned TotalNumArgs = AllArgs.size(); |
| 3351 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3352 | Call->setArg(i, AllArgs[i]); |
| 3353 | |
| 3354 | return false; |
| 3355 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3356 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3357 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3358 | FunctionDecl *FDecl, |
| 3359 | const FunctionProtoType *Proto, |
| 3360 | unsigned FirstProtoArg, |
| 3361 | Expr **Args, unsigned NumArgs, |
| 3362 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3363 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3364 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3365 | unsigned NumArgsToCheck = NumArgs; |
| 3366 | bool Invalid = false; |
| 3367 | if (NumArgs != NumArgsInProto) |
| 3368 | // Use default arguments for missing arguments |
| 3369 | NumArgsToCheck = NumArgsInProto; |
| 3370 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3371 | // 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] | 3372 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3373 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3375 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3376 | if (ArgIx < NumArgs) { |
| 3377 | Arg = Args[ArgIx++]; |
| 3378 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3379 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3380 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3381 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3382 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3383 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3385 | // Pass the argument |
| 3386 | ParmVarDecl *Param = 0; |
| 3387 | if (FDecl && i < FDecl->getNumParams()) |
| 3388 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3390 | |
| 3391 | InitializedEntity Entity = |
| 3392 | Param? InitializedEntity::InitializeParameter(Param) |
| 3393 | : InitializedEntity::InitializeParameter(ProtoArgType); |
| 3394 | OwningExprResult ArgE = PerformCopyInitialization(Entity, |
| 3395 | SourceLocation(), |
| 3396 | Owned(Arg)); |
| 3397 | if (ArgE.isInvalid()) |
| 3398 | return true; |
| 3399 | |
| 3400 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3401 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3402 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3403 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3404 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3405 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3406 | if (ArgExpr.isInvalid()) |
| 3407 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3408 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3409 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3410 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3411 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3412 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3414 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3415 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3416 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3417 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3418 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 3419 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3420 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3423 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3426 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3427 | /// This provides the location of the left/right parens and a list of comma |
| 3428 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3429 | Action::OwningExprResult |
| 3430 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 3431 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3432 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3433 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 3436 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 3438 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3439 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 3440 | assert(Fn && "no function call expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3442 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3443 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3444 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3445 | if (NumArgs > 0) { |
| 3446 | // Pseudo-destructor calls should not have any arguments. |
| 3447 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 3448 | << CodeModificationHint::CreateRemoval( |
| 3449 | SourceRange(Args[0]->getLocStart(), |
| 3450 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3452 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3453 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3455 | NumArgs = 0; |
| 3456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3458 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3459 | RParenLoc)); |
| 3460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3462 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3463 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3464 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3465 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3466 | bool Dependent = false; |
| 3467 | if (Fn->isTypeDependent()) |
| 3468 | Dependent = true; |
| 3469 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3470 | Dependent = true; |
| 3471 | |
| 3472 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3473 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3474 | Context.DependentTy, RParenLoc)); |
| 3475 | |
| 3476 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3477 | if (Fn->getType()->isRecordType()) |
| 3478 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 3479 | CommaLocs, RParenLoc)); |
| 3480 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3481 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3482 | |
| 3483 | // Determine whether this is a call to an unresolved member function. |
| 3484 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3485 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3486 | // an unresolved using declaration), it has to be an overloaded |
| 3487 | // function set, which means it must contain either multiple |
| 3488 | // declarations (all methods or method templates) or a single |
| 3489 | // method template. |
| 3490 | assert((MemE->getNumDecls() > 1) || |
| 3491 | isa<FunctionTemplateDecl>(*MemE->decls_begin())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3492 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3493 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3494 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3495 | CommaLocs, RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3498 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3499 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3500 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3501 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3502 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3503 | CommaLocs, RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3504 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3505 | |
| 3506 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3507 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3508 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 3509 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3510 | if (const FunctionProtoType *FPT = |
| 3511 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 3512 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3513 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3514 | ExprOwningPtr<CXXMemberCallExpr> |
| 3515 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3516 | NumArgs, ResultTy, |
| 3517 | RParenLoc)); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3518 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3519 | if (CheckCallReturnType(FPT->getResultType(), |
| 3520 | BO->getRHS()->getSourceRange().getBegin(), |
| 3521 | TheCall.get(), 0)) |
| 3522 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3523 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3524 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 3525 | RParenLoc)) |
| 3526 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3527 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3528 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 3529 | } |
| 3530 | return ExprError(Diag(Fn->getLocStart(), |
| 3531 | diag::err_typecheck_call_not_function) |
| 3532 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3533 | } |
| 3534 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3537 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3539 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3540 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3541 | Expr *NakedFn = Fn->IgnoreParens(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3542 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 3543 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 3544 | return BuildOverloadedCallExpr(Fn, ULE, LParenLoc, Args, NumArgs, |
| 3545 | CommaLocs, RParenLoc); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3546 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3547 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3548 | NamedDecl *NDecl = 0; |
| 3549 | if (isa<DeclRefExpr>(NakedFn)) |
| 3550 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 3551 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3552 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3553 | } |
| 3554 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3555 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3556 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3557 | /// unary-convert to an expression of function-pointer or |
| 3558 | /// block-pointer type. |
| 3559 | /// |
| 3560 | /// \param NDecl the declaration being called, if available |
| 3561 | Sema::OwningExprResult |
| 3562 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3563 | SourceLocation LParenLoc, |
| 3564 | Expr **Args, unsigned NumArgs, |
| 3565 | SourceLocation RParenLoc) { |
| 3566 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3567 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3568 | // Promote the function operand. |
| 3569 | UsualUnaryConversions(Fn); |
| 3570 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3571 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3572 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3573 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 3574 | Args, NumArgs, |
| 3575 | Context.BoolTy, |
| 3576 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3577 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3578 | const FunctionType *FuncT; |
| 3579 | if (!Fn->getType()->isBlockPointerType()) { |
| 3580 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3581 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3582 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3583 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3584 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3585 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3586 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3587 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3588 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3589 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3590 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3591 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3592 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3593 | << Fn->getType() << Fn->getSourceRange()); |
| 3594 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3595 | // Check for a valid return type |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3596 | if (CheckCallReturnType(FuncT->getResultType(), |
| 3597 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 3598 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3599 | return ExprError(); |
| 3600 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3601 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3602 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3603 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3604 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3605 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3606 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3607 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3608 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3609 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3610 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3611 | if (FDecl) { |
| 3612 | // Check if we have too few/too many template arguments, based |
| 3613 | // on our knowledge of the function definition. |
| 3614 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3615 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3616 | const FunctionProtoType *Proto = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3617 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3618 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3619 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3620 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3621 | } |
| 3622 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3625 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3626 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3627 | Expr *Arg = Args[i]; |
| 3628 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3629 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3630 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3631 | PDiag(diag::err_call_incomplete_argument) |
| 3632 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3633 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3634 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3635 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3636 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3637 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3638 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3639 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3640 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3641 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3642 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3643 | // Check for sentinels |
| 3644 | if (NDecl) |
| 3645 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3647 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3648 | if (FDecl) { |
| 3649 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3650 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3652 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3653 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3654 | } else if (NDecl) { |
| 3655 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3656 | return ExprError(); |
| 3657 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3658 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3659 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3662 | Action::OwningExprResult |
| 3663 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3664 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3665 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3666 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3667 | QualType literalType = GetTypeFromParser(Ty); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3668 | |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3669 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3670 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3671 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3672 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3673 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3674 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3675 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3676 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3677 | } else if (!literalType->isDependentType() && |
| 3678 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3679 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3681 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3682 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3684 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3685 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3686 | InitializationKind Kind |
| 3687 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| 3688 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3689 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
| 3690 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 3691 | MultiExprArg(*this, (void**)&literalExpr, 1), |
| 3692 | &literalType); |
| 3693 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3694 | return ExprError(); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3695 | InitExpr.release(); |
| 3696 | literalExpr = static_cast<Expr*>(Result.get()); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3697 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3698 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3699 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3700 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3701 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3702 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3703 | |
| 3704 | Result.release(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3705 | |
| 3706 | // FIXME: Store the TInfo to preserve type information better. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3707 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3708 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3711 | Action::OwningExprResult |
| 3712 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3713 | SourceLocation RBraceLoc) { |
| 3714 | unsigned NumInit = initlist.size(); |
| 3715 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3716 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3717 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3718 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3719 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3720 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3721 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3722 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3723 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3726 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3727 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3728 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3729 | return CastExpr::CK_NoOp; |
| 3730 | |
| 3731 | if (SrcTy->hasPointerRepresentation()) { |
| 3732 | if (DestTy->hasPointerRepresentation()) |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 3733 | return DestTy->isObjCObjectPointerType() ? |
| 3734 | CastExpr::CK_AnyPointerToObjCPointerCast : |
| 3735 | CastExpr::CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3736 | if (DestTy->isIntegerType()) |
| 3737 | return CastExpr::CK_PointerToIntegral; |
| 3738 | } |
| 3739 | |
| 3740 | if (SrcTy->isIntegerType()) { |
| 3741 | if (DestTy->isIntegerType()) |
| 3742 | return CastExpr::CK_IntegralCast; |
| 3743 | if (DestTy->hasPointerRepresentation()) |
| 3744 | return CastExpr::CK_IntegralToPointer; |
| 3745 | if (DestTy->isRealFloatingType()) |
| 3746 | return CastExpr::CK_IntegralToFloating; |
| 3747 | } |
| 3748 | |
| 3749 | if (SrcTy->isRealFloatingType()) { |
| 3750 | if (DestTy->isRealFloatingType()) |
| 3751 | return CastExpr::CK_FloatingCast; |
| 3752 | if (DestTy->isIntegerType()) |
| 3753 | return CastExpr::CK_FloatingToIntegral; |
| 3754 | } |
| 3755 | |
| 3756 | // FIXME: Assert here. |
| 3757 | // assert(false && "Unhandled cast combination!"); |
| 3758 | return CastExpr::CK_Unknown; |
| 3759 | } |
| 3760 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3761 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3762 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3763 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3764 | CXXMethodDecl *& ConversionDecl, |
| 3765 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3766 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3767 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3768 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3769 | |
Eli Friedman | 199ea95 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3770 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3771 | |
| 3772 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3773 | // type needs to be scalar. |
| 3774 | if (castType->isVoidType()) { |
| 3775 | // Cast to void allows any expr type. |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3776 | Kind = CastExpr::CK_ToVoid; |
| 3777 | return false; |
| 3778 | } |
| 3779 | |
| 3780 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3781 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3782 | (castType->isStructureType() || castType->isUnionType())) { |
| 3783 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3784 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3785 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3786 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3787 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3788 | return false; |
| 3789 | } |
| 3790 | |
| 3791 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3792 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3793 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3794 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3795 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3796 | Field != FieldEnd; ++Field) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3797 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3798 | castExpr->getType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3799 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3800 | << castExpr->getSourceRange(); |
| 3801 | break; |
| 3802 | } |
| 3803 | } |
| 3804 | if (Field == FieldEnd) |
| 3805 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3806 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3807 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3808 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3809 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3810 | |
| 3811 | // Reject any other conversions to non-scalar types. |
| 3812 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3813 | << castType << castExpr->getSourceRange(); |
| 3814 | } |
| 3815 | |
| 3816 | if (!castExpr->getType()->isScalarType() && |
| 3817 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3818 | return Diag(castExpr->getLocStart(), |
| 3819 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3820 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3823 | if (castType->isExtVectorType()) |
| 3824 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3825 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3826 | if (castType->isVectorType()) |
| 3827 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3828 | if (castExpr->getType()->isVectorType()) |
| 3829 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3830 | |
| 3831 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3832 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3833 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3834 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3835 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3836 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3837 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3838 | QualType castExprType = castExpr->getType(); |
| 3839 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3840 | return Diag(castExpr->getLocStart(), |
| 3841 | diag::err_cast_pointer_from_non_pointer_int) |
| 3842 | << castExprType << castExpr->getSourceRange(); |
| 3843 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3844 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3845 | return Diag(castExpr->getLocStart(), |
| 3846 | diag::err_cast_pointer_to_non_pointer_int) |
| 3847 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3848 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3849 | |
| 3850 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3851 | return false; |
| 3852 | } |
| 3853 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3854 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3855 | CastExpr::CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3856 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3857 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3858 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3859 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3860 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3861 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3862 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3863 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3864 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3865 | } else |
| 3866 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3867 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3868 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3869 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3870 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3871 | return false; |
| 3872 | } |
| 3873 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3874 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3875 | CastExpr::CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3876 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3877 | |
| 3878 | QualType SrcTy = CastExpr->getType(); |
| 3879 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3880 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3881 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3882 | if (SrcTy->isVectorType()) { |
| 3883 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3884 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3885 | << DestTy << SrcTy << R; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3886 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3887 | return false; |
| 3888 | } |
| 3889 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3890 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3891 | // conversion will take place first from scalar to elt type, and then |
| 3892 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3893 | if (SrcTy->isPointerType()) |
| 3894 | return Diag(R.getBegin(), |
| 3895 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3896 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3897 | |
| 3898 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3899 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3900 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3901 | |
| 3902 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3903 | return false; |
| 3904 | } |
| 3905 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3906 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3907 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3908 | SourceLocation RParenLoc, ExprArg Op) { |
| 3909 | assert((Ty != 0) && (Op.get() != 0) && |
| 3910 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3911 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3912 | TypeSourceInfo *castTInfo; |
| 3913 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 3914 | if (!castTInfo) |
| 3915 | castTInfo = Context.getTrivialTypeSourceInfo(castType, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3917 | // If the Expr being casted is a ParenListExpr, handle it specially. |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3918 | // FIXME: preserve type source info. |
| 3919 | Expr *castExpr = (Expr *)Op.get(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3920 | if (isa<ParenListExpr>(castExpr)) |
| 3921 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3922 | |
| 3923 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, move(Op)); |
| 3924 | } |
| 3925 | |
| 3926 | Action::OwningExprResult |
| 3927 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
| 3928 | SourceLocation RParenLoc, ExprArg Op) { |
| 3929 | Expr *castExpr = static_cast<Expr*>(Op.get()); |
| 3930 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3931 | CXXMethodDecl *Method = 0; |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3932 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
| 3933 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3934 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3935 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3936 | |
| 3937 | if (Method) { |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3938 | // FIXME: preserve type source info here |
| 3939 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, Ty->getType(), |
| 3940 | Kind, Method, move(Op)); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3941 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3942 | if (CastArg.isInvalid()) |
| 3943 | return ExprError(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3944 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3945 | castExpr = CastArg.takeAs<Expr>(); |
| 3946 | } else { |
| 3947 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3948 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3950 | return Owned(new (Context) CStyleCastExpr(Ty->getType().getNonReferenceType(), |
| 3951 | Kind, castExpr, Ty, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3952 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3955 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3956 | /// of comma binary operators. |
| 3957 | Action::OwningExprResult |
| 3958 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3959 | Expr *expr = EA.takeAs<Expr>(); |
| 3960 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3961 | if (!E) |
| 3962 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3964 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3965 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3966 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3967 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3968 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3970 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3971 | } |
| 3972 | |
| 3973 | Action::OwningExprResult |
| 3974 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3975 | SourceLocation RParenLoc, ExprArg Op, |
| 3976 | QualType Ty) { |
| 3977 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | |
| 3979 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3980 | // then handle it as such. |
| 3981 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3982 | if (PE->getNumExprs() == 0) { |
| 3983 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3984 | return ExprError(); |
| 3985 | } |
| 3986 | |
| 3987 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3988 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3989 | initExprs.push_back(PE->getExpr(i)); |
| 3990 | |
| 3991 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3992 | // braces instead of the original commas. |
| 3993 | Op.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3995 | initExprs.size(), RParenLoc); |
| 3996 | E->setType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3998 | Owned(E)); |
| 3999 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | // 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] | 4001 | // sequence of BinOp comma operators. |
| 4002 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 4003 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 4004 | } |
| 4005 | } |
| 4006 | |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4007 | Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4008 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4009 | MultiExprArg Val, |
| 4010 | TypeTy *TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4011 | unsigned nexprs = Val.size(); |
| 4012 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4013 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4014 | Expr *expr; |
| 4015 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 4016 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4017 | else |
| 4018 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4019 | return Owned(expr); |
| 4020 | } |
| 4021 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4022 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4023 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4024 | /// C99 6.5.15 |
| 4025 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 4026 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4027 | // C++ is sufficiently different to merit its own checker. |
| 4028 | if (getLangOptions().CPlusPlus) |
| 4029 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 4030 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4031 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 4032 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4033 | UsualUnaryConversions(Cond); |
| 4034 | UsualUnaryConversions(LHS); |
| 4035 | UsualUnaryConversions(RHS); |
| 4036 | QualType CondTy = Cond->getType(); |
| 4037 | QualType LHSTy = LHS->getType(); |
| 4038 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4039 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4040 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4041 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 4042 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4043 | << CondTy; |
| 4044 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4045 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4046 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4047 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4048 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 4049 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4051 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4052 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4053 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4054 | UsualArithmeticConversions(LHS, RHS); |
| 4055 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4056 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4057 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4058 | // If both operands are the same structure or union type, the result is that |
| 4059 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4060 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4061 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4062 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4063 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4064 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4065 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4066 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4068 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4069 | // 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] | 4070 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4071 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4072 | if (!LHSTy->isVoidType()) |
| 4073 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4074 | << RHS->getSourceRange(); |
| 4075 | if (!RHSTy->isVoidType()) |
| 4076 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4077 | << LHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4078 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 4079 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4080 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4081 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4082 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4083 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4084 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4085 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4086 | // promote the null to a pointer. |
| 4087 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4088 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4089 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4090 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4091 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4092 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4093 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4094 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4095 | |
| 4096 | // All objective-c pointer type analysis is done here. |
| 4097 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4098 | QuestionLoc); |
| 4099 | if (!compositeType.isNull()) |
| 4100 | return compositeType; |
| 4101 | |
| 4102 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4103 | // Handle block pointer types. |
| 4104 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4105 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4106 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4107 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4108 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4109 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4110 | return destType; |
| 4111 | } |
| 4112 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4113 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4114 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4115 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4116 | // We have 2 block pointer types. |
| 4117 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4118 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4119 | return LHSTy; |
| 4120 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4121 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4122 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4123 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4124 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4125 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4126 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4127 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4128 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4129 | // In this situation, we assume void* type. No especially good |
| 4130 | // reason, but this is what gcc does, and we do have to pick |
| 4131 | // to get a consistent AST. |
| 4132 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4133 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4134 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4135 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4136 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4137 | // The block pointer types are compatible. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4138 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4139 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4140 | return LHSTy; |
| 4141 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4142 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4143 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4144 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4145 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4146 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4147 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4148 | |
| 4149 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4150 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4151 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4152 | QualType destPointee |
| 4153 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4154 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4155 | // Add qualifiers if necessary. |
| 4156 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4157 | // Promote to void*. |
| 4158 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4159 | return destType; |
| 4160 | } |
| 4161 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4162 | QualType destPointee |
| 4163 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4164 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4165 | // Add qualifiers if necessary. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4166 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4167 | // Promote to void*. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4168 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4169 | return destType; |
| 4170 | } |
| 4171 | |
| 4172 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4173 | // Two identical pointer types are always compatible. |
| 4174 | return LHSTy; |
| 4175 | } |
| 4176 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4177 | rhptee.getUnqualifiedType())) { |
| 4178 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4179 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4180 | // In this situation, we assume void* type. No especially good |
| 4181 | // reason, but this is what gcc does, and we do have to pick |
| 4182 | // to get a consistent AST. |
| 4183 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4184 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4185 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4186 | return incompatTy; |
| 4187 | } |
| 4188 | // The pointer types are compatible. |
| 4189 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4190 | // differently qualified versions of compatible types, the result type is |
| 4191 | // a pointer to an appropriately qualified version of the *composite* |
| 4192 | // type. |
| 4193 | // FIXME: Need to calculate the composite type. |
| 4194 | // FIXME: Need to add qualifiers |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4195 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4196 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4197 | return LHSTy; |
| 4198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4199 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4200 | // GCC compatibility: soften pointer/integer mismatch. |
| 4201 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4202 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4203 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4204 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4205 | return RHSTy; |
| 4206 | } |
| 4207 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4208 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4209 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4210 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4211 | return LHSTy; |
| 4212 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4213 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4214 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4215 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4216 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4217 | return QualType(); |
| 4218 | } |
| 4219 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4220 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4221 | /// two objective-c pointer types of the two input expressions. |
| 4222 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4223 | SourceLocation QuestionLoc) { |
| 4224 | QualType LHSTy = LHS->getType(); |
| 4225 | QualType RHSTy = RHS->getType(); |
| 4226 | |
| 4227 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4228 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4229 | // redefinition type if an attempt is made to access its fields. |
| 4230 | if (LHSTy->isObjCClassType() && |
| 4231 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4232 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4233 | return LHSTy; |
| 4234 | } |
| 4235 | if (RHSTy->isObjCClassType() && |
| 4236 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4237 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4238 | return RHSTy; |
| 4239 | } |
| 4240 | // And the same for struct objc_object* / id |
| 4241 | if (LHSTy->isObjCIdType() && |
| 4242 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4243 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4244 | return LHSTy; |
| 4245 | } |
| 4246 | if (RHSTy->isObjCIdType() && |
| 4247 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4248 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4249 | return RHSTy; |
| 4250 | } |
| 4251 | // And the same for struct objc_selector* / SEL |
| 4252 | if (Context.isObjCSelType(LHSTy) && |
| 4253 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4254 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4255 | return LHSTy; |
| 4256 | } |
| 4257 | if (Context.isObjCSelType(RHSTy) && |
| 4258 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4259 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4260 | return RHSTy; |
| 4261 | } |
| 4262 | // Check constraints for Objective-C object pointers types. |
| 4263 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4264 | |
| 4265 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4266 | // Two identical object pointer types are always compatible. |
| 4267 | return LHSTy; |
| 4268 | } |
| 4269 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4270 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4271 | QualType compositeType = LHSTy; |
| 4272 | |
| 4273 | // If both operands are interfaces and either operand can be |
| 4274 | // assigned to the other, use that type as the composite |
| 4275 | // type. This allows |
| 4276 | // xxx ? (A*) a : (B*) b |
| 4277 | // where B is a subclass of A. |
| 4278 | // |
| 4279 | // Additionally, as for assignment, if either type is 'id' |
| 4280 | // allow silent coercion. Finally, if the types are |
| 4281 | // incompatible then make sure to use 'id' as the composite |
| 4282 | // type so the result is acceptable for sending messages to. |
| 4283 | |
| 4284 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4285 | // It could return the composite type. |
| 4286 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4287 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4288 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4289 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4290 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4291 | RHSTy->isObjCQualifiedIdType()) && |
| 4292 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4293 | // Need to handle "id<xx>" explicitly. |
| 4294 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4295 | // id. Currently localizing to here until clear this should be |
| 4296 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4297 | compositeType = Context.getObjCIdType(); |
| 4298 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4299 | compositeType = Context.getObjCIdType(); |
| 4300 | } else if (!(compositeType = |
| 4301 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4302 | ; |
| 4303 | else { |
| 4304 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4305 | << LHSTy << RHSTy |
| 4306 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4307 | QualType incompatTy = Context.getObjCIdType(); |
| 4308 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4309 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
| 4310 | return incompatTy; |
| 4311 | } |
| 4312 | // The object pointer types are compatible. |
| 4313 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 4314 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
| 4315 | return compositeType; |
| 4316 | } |
| 4317 | // Check Objective-C object pointer types and 'void *' |
| 4318 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4319 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4320 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4321 | QualType destPointee |
| 4322 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4323 | QualType destType = Context.getPointerType(destPointee); |
| 4324 | // Add qualifiers if necessary. |
| 4325 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4326 | // Promote to void*. |
| 4327 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
| 4328 | return destType; |
| 4329 | } |
| 4330 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4331 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4332 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4333 | QualType destPointee |
| 4334 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4335 | QualType destType = Context.getPointerType(destPointee); |
| 4336 | // Add qualifiers if necessary. |
| 4337 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 4338 | // Promote to void*. |
| 4339 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4340 | return destType; |
| 4341 | } |
| 4342 | return QualType(); |
| 4343 | } |
| 4344 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4345 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4346 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4347 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 4348 | SourceLocation ColonLoc, |
| 4349 | ExprArg Cond, ExprArg LHS, |
| 4350 | ExprArg RHS) { |
| 4351 | Expr *CondExpr = (Expr *) Cond.get(); |
| 4352 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4353 | |
| 4354 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4355 | // was the condition. |
| 4356 | bool isLHSNull = LHSExpr == 0; |
| 4357 | if (isLHSNull) |
| 4358 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4359 | |
| 4360 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4361 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4362 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4363 | return ExprError(); |
| 4364 | |
| 4365 | Cond.release(); |
| 4366 | LHS.release(); |
| 4367 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4368 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4369 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4370 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4373 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4374 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4375 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4376 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4377 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4378 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4379 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4380 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4381 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4382 | if ((lhsType->isObjCClassType() && |
| 4383 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4384 | (rhsType->isObjCClassType() && |
| 4385 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4386 | return Compatible; |
| 4387 | } |
| 4388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4389 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4390 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4391 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4393 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4394 | lhptee = Context.getCanonicalType(lhptee); |
| 4395 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4396 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4397 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4398 | |
| 4399 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4400 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4401 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4402 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4403 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4404 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4405 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4406 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4407 | // 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] | 4408 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4409 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4410 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4411 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4412 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4413 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4414 | assert(rhptee->isFunctionType()); |
| 4415 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4416 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4417 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4418 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4419 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4420 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4421 | |
| 4422 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4423 | assert(lhptee->isFunctionType()); |
| 4424 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4425 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4426 | // 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] | 4427 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4428 | lhptee = lhptee.getUnqualifiedType(); |
| 4429 | rhptee = rhptee.getUnqualifiedType(); |
| 4430 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4431 | // Check if the pointee types are compatible ignoring the sign. |
| 4432 | // We explicitly check for char so that we catch "char" vs |
| 4433 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4434 | if (lhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4435 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4436 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4437 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4438 | |
| 4439 | if (rhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4440 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4441 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4442 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4443 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4444 | if (lhptee == rhptee) { |
| 4445 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4446 | // takes priority over sign incompatibility because the sign |
| 4447 | // warning can be disabled. |
| 4448 | if (ConvTy != Compatible) |
| 4449 | return ConvTy; |
| 4450 | return IncompatiblePointerSign; |
| 4451 | } |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4452 | |
| 4453 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4454 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4455 | // the eventual target type is the same and the pointers have the same |
| 4456 | // level of indirection, this must be the issue. |
| 4457 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4458 | do { |
| 4459 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4460 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 4461 | |
| 4462 | lhptee = Context.getCanonicalType(lhptee); |
| 4463 | rhptee = Context.getCanonicalType(rhptee); |
| 4464 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 4465 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4466 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4467 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4470 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4471 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4472 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4473 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4476 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4477 | /// block pointer types are compatible or whether a block and normal pointer |
| 4478 | /// are compatible. It is more restrict than comparing two function pointer |
| 4479 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4480 | Sema::AssignConvertType |
| 4481 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4482 | QualType rhsType) { |
| 4483 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4484 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4485 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4486 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4487 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4488 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4489 | // make sure we operate on the canonical type |
| 4490 | lhptee = Context.getCanonicalType(lhptee); |
| 4491 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4492 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4493 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4494 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4495 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4496 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4497 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4498 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4499 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4500 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4501 | return ConvTy; |
| 4502 | } |
| 4503 | |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4504 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4505 | /// for assignment compatibility. |
| 4506 | Sema::AssignConvertType |
| 4507 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4508 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 4509 | return Compatible; |
| 4510 | QualType lhptee = |
| 4511 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4512 | QualType rhptee = |
| 4513 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4514 | // make sure we operate on the canonical type |
| 4515 | lhptee = Context.getCanonicalType(lhptee); |
| 4516 | rhptee = Context.getCanonicalType(rhptee); |
| 4517 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4518 | return CompatiblePointerDiscardsQualifiers; |
| 4519 | |
| 4520 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4521 | return Compatible; |
| 4522 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4523 | return IncompatibleObjCQualifiedId; |
| 4524 | return IncompatiblePointer; |
| 4525 | } |
| 4526 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4527 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4528 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4529 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4530 | /// |
| 4531 | /// int a, *pint; |
| 4532 | /// short *pshort; |
| 4533 | /// struct foo *pfoo; |
| 4534 | /// |
| 4535 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4536 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4537 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4538 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4539 | /// |
| 4540 | /// 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] | 4541 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4542 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4543 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4544 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4545 | // Get canonical types. We're not formatting these types, just comparing |
| 4546 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4547 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4548 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4549 | |
| 4550 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4551 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4552 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4553 | if ((lhsType->isObjCClassType() && |
| 4554 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4555 | (rhsType->isObjCClassType() && |
| 4556 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4557 | return Compatible; |
| 4558 | } |
| 4559 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4560 | // If the left-hand side is a reference type, then we are in a |
| 4561 | // (rare!) case where we've allowed the use of references in C, |
| 4562 | // e.g., as a parameter type in a built-in function. In this case, |
| 4563 | // just make sure that the type referenced is compatible with the |
| 4564 | // right-hand side type. The caller is responsible for adjusting |
| 4565 | // lhsType so that the resulting expression does not have reference |
| 4566 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4567 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4568 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4569 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4570 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4571 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4572 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4573 | // to the same ExtVector type. |
| 4574 | if (lhsType->isExtVectorType()) { |
| 4575 | if (rhsType->isExtVectorType()) |
| 4576 | return lhsType == rhsType ? Compatible : Incompatible; |
| 4577 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 4578 | return Compatible; |
| 4579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4580 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4581 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4582 | // 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] | 4583 | // 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] | 4584 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4585 | if (getLangOptions().LaxVectorConversions && |
| 4586 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4587 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4588 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4589 | } |
| 4590 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4591 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4592 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4593 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4594 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4595 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4596 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4597 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4598 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4599 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4600 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4601 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4602 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4603 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4604 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4605 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4606 | return Compatible; |
| 4607 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4608 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4609 | if (rhsType->getAs<BlockPointerType>()) { |
| 4610 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4611 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4612 | |
| 4613 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4614 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4615 | return Compatible; |
| 4616 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4617 | return Incompatible; |
| 4618 | } |
| 4619 | |
| 4620 | if (isa<BlockPointerType>(lhsType)) { |
| 4621 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4622 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4623 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4624 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4625 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4626 | return Compatible; |
| 4627 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4628 | if (rhsType->isBlockPointerType()) |
| 4629 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4630 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4631 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4632 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4633 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4634 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4635 | return Incompatible; |
| 4636 | } |
| 4637 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4638 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4639 | if (rhsType->isIntegerType()) |
| 4640 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4642 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4643 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4644 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4645 | return Compatible; |
| 4646 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4647 | } |
| 4648 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4649 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4650 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4651 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4652 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4653 | return Compatible; |
| 4654 | } |
| 4655 | // Treat block pointers as objects. |
| 4656 | if (rhsType->isBlockPointerType()) |
| 4657 | return Compatible; |
| 4658 | return Incompatible; |
| 4659 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4660 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4661 | // 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] | 4662 | if (lhsType == Context.BoolTy) |
| 4663 | return Compatible; |
| 4664 | |
| 4665 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4666 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4667 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4668 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4669 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4670 | |
| 4671 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4672 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4673 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4674 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4675 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4676 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4677 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4678 | if (lhsType == Context.BoolTy) |
| 4679 | return Compatible; |
| 4680 | |
| 4681 | if (lhsType->isIntegerType()) |
| 4682 | return PointerToInt; |
| 4683 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4684 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4685 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4686 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4687 | return Compatible; |
| 4688 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4689 | } |
| 4690 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4691 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4692 | return Compatible; |
| 4693 | return Incompatible; |
| 4694 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4695 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4696 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4697 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4698 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4699 | } |
| 4700 | return Incompatible; |
| 4701 | } |
| 4702 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4703 | /// \brief Constructs a transparent union from an expression that is |
| 4704 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4706 | QualType UnionType, FieldDecl *Field) { |
| 4707 | // Build an initializer list that designates the appropriate member |
| 4708 | // of the transparent union. |
| 4709 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 4710 | &E, 1, |
| 4711 | SourceLocation()); |
| 4712 | Initializer->setType(UnionType); |
| 4713 | Initializer->setInitializedFieldInUnion(Field); |
| 4714 | |
| 4715 | // Build a compound literal constructing a value of the transparent |
| 4716 | // union type from this initializer list. |
| 4717 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 4718 | false); |
| 4719 | } |
| 4720 | |
| 4721 | Sema::AssignConvertType |
| 4722 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4723 | QualType FromType = rExpr->getType(); |
| 4724 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | // 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] | 4726 | // transparent_union GCC extension. |
| 4727 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4728 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4729 | return Incompatible; |
| 4730 | |
| 4731 | // The field to initialize within the transparent union. |
| 4732 | RecordDecl *UD = UT->getDecl(); |
| 4733 | FieldDecl *InitField = 0; |
| 4734 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4735 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4736 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4737 | it != itend; ++it) { |
| 4738 | if (it->getType()->isPointerType()) { |
| 4739 | // If the transparent union contains a pointer type, we allow: |
| 4740 | // 1) void pointer |
| 4741 | // 2) null pointer constant |
| 4742 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4743 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4744 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4745 | InitField = *it; |
| 4746 | break; |
| 4747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4749 | if (rExpr->isNullPointerConstant(Context, |
| 4750 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4751 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4752 | InitField = *it; |
| 4753 | break; |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4758 | == Compatible) { |
| 4759 | InitField = *it; |
| 4760 | break; |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | if (!InitField) |
| 4765 | return Incompatible; |
| 4766 | |
| 4767 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4768 | return Compatible; |
| 4769 | } |
| 4770 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4771 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4772 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4773 | if (getLangOptions().CPlusPlus) { |
| 4774 | if (!lhsType->isRecordType()) { |
| 4775 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4776 | // expression is implicitly converted (C++ 4) to the |
| 4777 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4778 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4779 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4780 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4781 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4785 | // structures. |
| 4786 | } |
| 4787 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4788 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4789 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4790 | if ((lhsType->isPointerType() || |
| 4791 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4792 | lhsType->isBlockPointerType()) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4793 | && rExpr->isNullPointerConstant(Context, |
| 4794 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4795 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4796 | return Compatible; |
| 4797 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4798 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4799 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4800 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4801 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4802 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4803 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4804 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4805 | if (!lhsType->isReferenceType()) |
| 4806 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4807 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4808 | Sema::AssignConvertType result = |
| 4809 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4810 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4811 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4812 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4813 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4814 | // so that we can use references in built-in functions even in C. |
| 4815 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4816 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4817 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4818 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4819 | CastExpr::CK_Unknown); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4820 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4823 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4824 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4825 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4826 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4827 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4830 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4831 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4832 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4833 | QualType lhsType = |
| 4834 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4835 | QualType rhsType = |
| 4836 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4837 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4838 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4839 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4840 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4841 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4842 | // Handle the case of a vector & extvector type of the same size and element |
| 4843 | // 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] | 4844 | if (getLangOptions().LaxVectorConversions) { |
| 4845 | // FIXME: Should we warn here? |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4846 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4847 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4848 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4849 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4850 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } |
| 4853 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4854 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4855 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4856 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4857 | bool swapped = false; |
| 4858 | if (rhsType->isExtVectorType()) { |
| 4859 | swapped = true; |
| 4860 | std::swap(rex, lex); |
| 4861 | std::swap(rhsType, lhsType); |
| 4862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4863 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4864 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4865 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4866 | QualType EltTy = LV->getElementType(); |
| 4867 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4868 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4869 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4870 | if (swapped) std::swap(rex, lex); |
| 4871 | return lhsType; |
| 4872 | } |
| 4873 | } |
| 4874 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4875 | rhsType->isRealFloatingType()) { |
| 4876 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4877 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4878 | if (swapped) std::swap(rex, lex); |
| 4879 | return lhsType; |
| 4880 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4881 | } |
| 4882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4884 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4885 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4886 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4887 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4888 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4891 | QualType Sema::CheckMultiplyDivideOperands( |
| 4892 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4893 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4894 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4895 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4896 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4897 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4898 | if (!lex->getType()->isArithmeticType() || |
| 4899 | !rex->getType()->isArithmeticType()) |
| 4900 | return InvalidOperands(Loc, lex, rex); |
| 4901 | |
| 4902 | // Check for division by zero. |
| 4903 | if (isDiv && |
| 4904 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 4905 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_division_by_zero) |
| 4906 | << rex->getSourceRange()); |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4907 | |
| 4908 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4911 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4912 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4913 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4914 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4915 | return CheckVectorOperands(Loc, lex, rex); |
| 4916 | return InvalidOperands(Loc, lex, rex); |
| 4917 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4918 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4919 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4920 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4921 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 4922 | return InvalidOperands(Loc, lex, rex); |
| 4923 | |
| 4924 | // Check for remainder by zero. |
| 4925 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 4926 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_remainder_by_zero) |
| 4927 | << rex->getSourceRange()); |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4928 | |
| 4929 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4932 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4933 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4934 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4935 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4936 | if (CompLHSTy) *CompLHSTy = compType; |
| 4937 | return compType; |
| 4938 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4939 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4940 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4942 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4943 | if (lex->getType()->isArithmeticType() && |
| 4944 | rex->getType()->isArithmeticType()) { |
| 4945 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4946 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4947 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4948 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4949 | // Put any potential pointer into PExp |
| 4950 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4951 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4952 | std::swap(PExp, IExp); |
| 4953 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4954 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4955 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4956 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4957 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4958 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4959 | // Check for arithmetic on pointers to incomplete types. |
| 4960 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4961 | if (getLangOptions().CPlusPlus) { |
| 4962 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4963 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4964 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4965 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4966 | |
| 4967 | // GNU extension: arithmetic on pointer to void |
| 4968 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4969 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4970 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4971 | if (getLangOptions().CPlusPlus) { |
| 4972 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4973 | << lex->getType() << lex->getSourceRange(); |
| 4974 | return QualType(); |
| 4975 | } |
| 4976 | |
| 4977 | // GNU extension: arithmetic on pointer to function |
| 4978 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4979 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4980 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4981 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4982 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4983 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4984 | PExp->getType()->isObjCObjectPointerType()) && |
| 4985 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4987 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4988 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4989 | return QualType(); |
| 4990 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4991 | // Diagnose bad cases where we step over interface counts. |
| 4992 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4993 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4994 | << PointeeTy << PExp->getSourceRange(); |
| 4995 | return QualType(); |
| 4996 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4998 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4999 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5000 | if (LHSTy.isNull()) { |
| 5001 | LHSTy = lex->getType(); |
| 5002 | if (LHSTy->isPromotableIntegerType()) |
| 5003 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5004 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5005 | *CompLHSTy = LHSTy; |
| 5006 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5007 | return PExp->getType(); |
| 5008 | } |
| 5009 | } |
| 5010 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5011 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5014 | // C99 6.5.6 |
| 5015 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5016 | SourceLocation Loc, QualType* CompLHSTy) { |
| 5017 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 5018 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 5019 | if (CompLHSTy) *CompLHSTy = compType; |
| 5020 | return compType; |
| 5021 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5022 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5023 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5024 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5025 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5026 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5027 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5028 | if (lex->getType()->isArithmeticType() |
| 5029 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5030 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5031 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5032 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5034 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5035 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 5036 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5037 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5038 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5040 | bool ComplainAboutVoid = false; |
| 5041 | Expr *ComplainAboutFunc = 0; |
| 5042 | if (lpointee->isVoidType()) { |
| 5043 | if (getLangOptions().CPlusPlus) { |
| 5044 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5045 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5046 | return QualType(); |
| 5047 | } |
| 5048 | |
| 5049 | // GNU C extension: arithmetic on pointer to void |
| 5050 | ComplainAboutVoid = true; |
| 5051 | } else if (lpointee->isFunctionType()) { |
| 5052 | if (getLangOptions().CPlusPlus) { |
| 5053 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5054 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5055 | return QualType(); |
| 5056 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5057 | |
| 5058 | // GNU C extension: arithmetic on pointer to function |
| 5059 | ComplainAboutFunc = lex; |
| 5060 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5061 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5062 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5063 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5064 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5065 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5066 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5067 | // Diagnose bad cases where we step over interface counts. |
| 5068 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5069 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5070 | << lpointee << lex->getSourceRange(); |
| 5071 | return QualType(); |
| 5072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5073 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5074 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5075 | if (rex->getType()->isIntegerType()) { |
| 5076 | if (ComplainAboutVoid) |
| 5077 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5078 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5079 | if (ComplainAboutFunc) |
| 5080 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5081 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5082 | << ComplainAboutFunc->getSourceRange(); |
| 5083 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5084 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5085 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5086 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5087 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5088 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5089 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 5090 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5092 | // RHS must be a completely-type object type. |
| 5093 | // Handle the GNU void* extension. |
| 5094 | if (rpointee->isVoidType()) { |
| 5095 | if (getLangOptions().CPlusPlus) { |
| 5096 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5097 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5098 | return QualType(); |
| 5099 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5101 | ComplainAboutVoid = true; |
| 5102 | } else if (rpointee->isFunctionType()) { |
| 5103 | if (getLangOptions().CPlusPlus) { |
| 5104 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5105 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5106 | return QualType(); |
| 5107 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5108 | |
| 5109 | // GNU extension: arithmetic on pointer to function |
| 5110 | if (!ComplainAboutFunc) |
| 5111 | ComplainAboutFunc = rex; |
| 5112 | } else if (!rpointee->isDependentType() && |
| 5113 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5114 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 5115 | << rex->getSourceRange() |
| 5116 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5117 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5118 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5119 | if (getLangOptions().CPlusPlus) { |
| 5120 | // Pointee types must be the same: C++ [expr.add] |
| 5121 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 5122 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5123 | << lex->getType() << rex->getType() |
| 5124 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5125 | return QualType(); |
| 5126 | } |
| 5127 | } else { |
| 5128 | // Pointee types must be compatible C99 6.5.6p3 |
| 5129 | if (!Context.typesAreCompatible( |
| 5130 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 5131 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 5132 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5133 | << lex->getType() << rex->getType() |
| 5134 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5135 | return QualType(); |
| 5136 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5137 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5139 | if (ComplainAboutVoid) |
| 5140 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5141 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5142 | if (ComplainAboutFunc) |
| 5143 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5144 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5145 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5146 | |
| 5147 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5148 | return Context.getPointerDiffType(); |
| 5149 | } |
| 5150 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5151 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5152 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5155 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5156 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5157 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5158 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 5159 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5160 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5161 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 5162 | // Vector shifts promote their scalar inputs to vector type. |
| 5163 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 5164 | return CheckVectorOperands(Loc, lex, rex); |
| 5165 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5166 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 5167 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5168 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5169 | if (LHSTy.isNull()) { |
| 5170 | LHSTy = lex->getType(); |
| 5171 | if (LHSTy->isPromotableIntegerType()) |
| 5172 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5173 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 5174 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5175 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5176 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5177 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5178 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5179 | // Sanity-check shift operands |
| 5180 | llvm::APSInt Right; |
| 5181 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 5182 | if (!rex->isValueDependent() && |
| 5183 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 5184 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5185 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 5186 | else { |
| 5187 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 5188 | Context.getTypeSize(lex->getType())); |
| 5189 | if (Right.uge(LeftBits)) |
| 5190 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 5191 | } |
| 5192 | } |
| 5193 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5194 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5195 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5198 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5199 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5200 | unsigned OpaqueOpc, bool isRelational) { |
| 5201 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 5202 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5203 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5204 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5205 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5206 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5207 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 5208 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5209 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5210 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 5211 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5212 | UsualArithmeticConversions(lex, rex); |
| 5213 | else { |
| 5214 | UsualUnaryConversions(lex); |
| 5215 | UsualUnaryConversions(rex); |
| 5216 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5217 | QualType lType = lex->getType(); |
| 5218 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5219 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5220 | if (!lType->isFloatingType() |
| 5221 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5222 | // For non-floating point types, check for self-comparisons of the form |
| 5223 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5224 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5225 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 5226 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5227 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5228 | Expr *RHSStripped = rex->IgnoreParens(); |
| 5229 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 5230 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 5231 | if (DRL->getDecl() == DRR->getDecl() && |
| 5232 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5233 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_selfcomparison)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5234 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5235 | if (isa<CastExpr>(LHSStripped)) |
| 5236 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5237 | if (isa<CastExpr>(RHSStripped)) |
| 5238 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5239 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5240 | // Warn about comparisons against a string constant (unless the other |
| 5241 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5242 | Expr *literalString = 0; |
| 5243 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5244 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5245 | !RHSStripped->isNullPointerConstant(Context, |
| 5246 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5247 | literalString = lex; |
| 5248 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5249 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5250 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5251 | !LHSStripped->isNullPointerConstant(Context, |
| 5252 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5253 | literalString = rex; |
| 5254 | literalStringStripped = RHSStripped; |
| 5255 | } |
| 5256 | |
| 5257 | if (literalString) { |
| 5258 | std::string resultComparison; |
| 5259 | switch (Opc) { |
| 5260 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 5261 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 5262 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 5263 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 5264 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 5265 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 5266 | default: assert(false && "Invalid comparison operator"); |
| 5267 | } |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5268 | |
| 5269 | DiagRuntimeBehavior(Loc, |
| 5270 | PDiag(diag::warn_stringcompare) |
| 5271 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 5272 | << literalString->getSourceRange() |
| 5273 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 5274 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 5275 | "strcmp(") |
| 5276 | << CodeModificationHint::CreateInsertion( |
| 5277 | PP.getLocForEndOfToken(rex->getLocEnd()), |
| 5278 | resultComparison)); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5279 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5280 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5281 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5282 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5283 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5285 | if (isRelational) { |
| 5286 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5287 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5288 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5289 | // Check for comparisons of floating point operands using != and ==. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5290 | if (lType->isFloatingType() && rType->isFloatingType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5291 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5292 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5293 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5294 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5295 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5297 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 5298 | Expr::NPC_ValueDependentIsNull); |
| 5299 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 5300 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5301 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5302 | // All of the following pointer related warnings are GCC extensions, except |
| 5303 | // when handling null pointer constants. One day, we can consider making them |
| 5304 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5305 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5306 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5307 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5308 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5309 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5311 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5312 | if (LCanPointeeTy == RCanPointeeTy) |
| 5313 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5314 | if (!isRelational && |
| 5315 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5316 | // Valid unless comparison between non-null pointer and function pointer |
| 5317 | // This is a gcc extension compatibility comparison. |
| 5318 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5319 | && !LHSIsNull && !RHSIsNull) { |
| 5320 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5321 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5322 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
| 5323 | return ResultTy; |
| 5324 | } |
| 5325 | } |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5326 | // C++ [expr.rel]p2: |
| 5327 | // [...] Pointer conversions (4.10) and qualification |
| 5328 | // conversions (4.4) are performed on pointer operands (or on |
| 5329 | // a pointer operand and a null pointer constant) to bring |
| 5330 | // them to their composite pointer type. [...] |
| 5331 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5332 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5333 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 5334 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5335 | if (T.isNull()) { |
| 5336 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5337 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5338 | return QualType(); |
| 5339 | } |
| 5340 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5341 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5342 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5343 | return ResultTy; |
| 5344 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5345 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5346 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5347 | RCanPointeeTy.getUnqualifiedType())) { |
| 5348 | // Valid unless a relational comparison of function pointers |
| 5349 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5350 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5351 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5352 | } |
| 5353 | } else if (!isRelational && |
| 5354 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5355 | // Valid unless comparison between non-null pointer and function pointer |
| 5356 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5357 | && !LHSIsNull && !RHSIsNull) { |
| 5358 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5359 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5360 | } |
| 5361 | } else { |
| 5362 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5363 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5364 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5365 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5366 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5367 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5368 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5371 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5372 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5373 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5374 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5375 | (lType->isPointerType() || |
| 5376 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5377 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5378 | return ResultTy; |
| 5379 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5380 | if (LHSIsNull && |
| 5381 | (rType->isPointerType() || |
| 5382 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5383 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5384 | return ResultTy; |
| 5385 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5386 | |
| 5387 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5389 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5390 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5391 | // In addition, pointers to members can be compared, or a pointer to |
| 5392 | // member and a null pointer constant. Pointer to member conversions |
| 5393 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5394 | // them to a common type. If one operand is a null pointer constant, |
| 5395 | // the common type is the type of the other operand. Otherwise, the |
| 5396 | // common type is a pointer to member type similar (4.4) to the type |
| 5397 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5398 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5399 | // types. |
| 5400 | QualType T = FindCompositePointerType(lex, rex); |
| 5401 | if (T.isNull()) { |
| 5402 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5403 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5404 | return QualType(); |
| 5405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5406 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5407 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5408 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5409 | return ResultTy; |
| 5410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5411 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5412 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5413 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5414 | return ResultTy; |
| 5415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5417 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5418 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5419 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5420 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5421 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5422 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5423 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5424 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5425 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5426 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5427 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5428 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5429 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5430 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5431 | if (!isRelational |
| 5432 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5433 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5434 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5435 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5436 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5437 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5438 | ->getPointeeType()->isVoidType()))) |
| 5439 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5440 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5441 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5442 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5443 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5444 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5445 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5446 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5447 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5448 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5449 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5450 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5451 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5452 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5453 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5454 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5455 | if (!LPtrToVoid && !RPtrToVoid && |
| 5456 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5457 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5458 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5459 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5460 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5461 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5462 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5463 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5464 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5465 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5466 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5467 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5468 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5469 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5470 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5471 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5472 | unsigned DiagID = 0; |
| 5473 | if (RHSIsNull) { |
| 5474 | if (isRelational) |
| 5475 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5476 | } else if (isRelational) |
| 5477 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5478 | else |
| 5479 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5480 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5481 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5482 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5483 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5484 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5485 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5486 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5487 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5488 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5489 | unsigned DiagID = 0; |
| 5490 | if (LHSIsNull) { |
| 5491 | if (isRelational) |
| 5492 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5493 | } else if (isRelational) |
| 5494 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5495 | else |
| 5496 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5497 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5498 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5499 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5500 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5501 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5502 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5503 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5504 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5505 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5506 | if (!isRelational && RHSIsNull |
| 5507 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5508 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5509 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5510 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5511 | if (!isRelational && LHSIsNull |
| 5512 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5513 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5514 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5515 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5516 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5519 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5520 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5521 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5522 | /// types. |
| 5523 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5524 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5525 | bool isRelational) { |
| 5526 | // Check to make sure we're operating on vectors of the same type and width, |
| 5527 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5528 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5529 | if (vType.isNull()) |
| 5530 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5531 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5532 | QualType lType = lex->getType(); |
| 5533 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5534 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5535 | // For non-floating point types, check for self-comparisons of the form |
| 5536 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5537 | // often indicate logic errors in the program. |
| 5538 | if (!lType->isFloatingType()) { |
| 5539 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5540 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5541 | if (DRL->getDecl() == DRR->getDecl()) |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5542 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_selfcomparison)); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5543 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5544 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5545 | // Check for comparisons of floating point operands using != and ==. |
| 5546 | if (!isRelational && lType->isFloatingType()) { |
| 5547 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5548 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5549 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5550 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5551 | // Return the type for the comparison, which is the same as vector type for |
| 5552 | // integer vectors, or an integer type of identical size and number of |
| 5553 | // elements for floating point vectors. |
| 5554 | if (lType->isIntegerType()) |
| 5555 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5556 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5557 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5558 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5559 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5560 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5561 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5562 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5563 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5564 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5565 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5566 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5567 | } |
| 5568 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5569 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5570 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 5571 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5572 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5573 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5574 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5575 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5576 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5577 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5578 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5582 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5583 | if (!Context.getLangOptions().CPlusPlus) { |
| 5584 | UsualUnaryConversions(lex); |
| 5585 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5586 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5587 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5588 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5589 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5590 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5591 | } |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5592 | |
| 5593 | // C++ [expr.log.and]p1 |
| 5594 | // C++ [expr.log.or]p1 |
| 5595 | // The operands are both implicitly converted to type bool (clause 4). |
| 5596 | StandardConversionSequence LHS; |
| 5597 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 5598 | /*InOverloadResolution=*/false, LHS)) |
| 5599 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5600 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5601 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5602 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5603 | return InvalidOperands(Loc, lex, rex); |
| 5604 | |
| 5605 | StandardConversionSequence RHS; |
| 5606 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 5607 | /*InOverloadResolution=*/false, RHS)) |
| 5608 | return InvalidOperands(Loc, lex, rex); |
| 5609 | |
| 5610 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5611 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5612 | return InvalidOperands(Loc, lex, rex); |
| 5613 | |
| 5614 | // C++ [expr.log.and]p2 |
| 5615 | // C++ [expr.log.or]p2 |
| 5616 | // The result is a bool. |
| 5617 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5620 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5621 | /// is a read-only property; return true if so. A readonly property expression |
| 5622 | /// depends on various declarations and thus must be treated specially. |
| 5623 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5624 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5625 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5626 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5627 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5628 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5629 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5630 | BaseType->getAsObjCInterfacePointerType()) |
| 5631 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5632 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5633 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5634 | } |
| 5635 | } |
| 5636 | return false; |
| 5637 | } |
| 5638 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5639 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5640 | /// emit an error and return true. If so, return false. |
| 5641 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5642 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5643 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5644 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5645 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5646 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5647 | if (IsLV == Expr::MLV_Valid) |
| 5648 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5649 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5650 | unsigned Diag = 0; |
| 5651 | bool NeedType = false; |
| 5652 | switch (IsLV) { // C99 6.5.16p2 |
| 5653 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 5654 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5655 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5656 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5657 | NeedType = true; |
| 5658 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5659 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5660 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5661 | NeedType = true; |
| 5662 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5663 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5664 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5665 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5666 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5667 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5668 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5669 | case Expr::MLV_IncompleteType: |
| 5670 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5671 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5672 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 5673 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5674 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5675 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5676 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5677 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5678 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5679 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5680 | case Expr::MLV_ReadonlyProperty: |
| 5681 | Diag = diag::error_readonly_property_assignment; |
| 5682 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5683 | case Expr::MLV_NoSetterProperty: |
| 5684 | Diag = diag::error_nosetter_property_assignment; |
| 5685 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5686 | case Expr::MLV_SubObjCPropertySetting: |
| 5687 | Diag = diag::error_no_subobject_property_setting; |
| 5688 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5689 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5690 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5691 | SourceRange Assign; |
| 5692 | if (Loc != OrigLoc) |
| 5693 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5694 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5695 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5696 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5697 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5698 | return true; |
| 5699 | } |
| 5700 | |
| 5701 | |
| 5702 | |
| 5703 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5704 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5705 | SourceLocation Loc, |
| 5706 | QualType CompoundType) { |
| 5707 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5708 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5709 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5710 | |
| 5711 | QualType LHSType = LHS->getType(); |
| 5712 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5713 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5714 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5715 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5716 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5717 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5718 | // Special case of NSObject attributes on c-style pointer types. |
| 5719 | if (ConvTy == IncompatiblePointer && |
| 5720 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5721 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5722 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5723 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5724 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5725 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5726 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5727 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5728 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5729 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5730 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5731 | RHSCheck = ICE->getSubExpr(); |
| 5732 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5733 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5734 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5735 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5736 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5737 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5738 | // And there is a space or other character before the subexpr of the |
| 5739 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5740 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5741 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5742 | Diag(Loc, diag::warn_not_compound_assign) |
| 5743 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5744 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5745 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5746 | } |
| 5747 | } else { |
| 5748 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5749 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5750 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5751 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5752 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5753 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5754 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5756 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5757 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5758 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5759 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5760 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5761 | // 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] | 5762 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5763 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5766 | // C99 6.5.17 |
| 5767 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5768 | // 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] | 5769 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5770 | |
| 5771 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5772 | // incomplete in C++). |
| 5773 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5774 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5777 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5778 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5779 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5780 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5781 | if (Op->isTypeDependent()) |
| 5782 | return Context.DependentTy; |
| 5783 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5784 | QualType ResType = Op->getType(); |
| 5785 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5786 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5787 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5788 | // Decrement of bool is not allowed. |
| 5789 | if (!isInc) { |
| 5790 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5791 | return QualType(); |
| 5792 | } |
| 5793 | // Increment of bool sets it to true, but is deprecated. |
| 5794 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5795 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5796 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5797 | } else if (ResType->isAnyPointerType()) { |
| 5798 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5799 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5800 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5801 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5802 | if (getLangOptions().CPlusPlus) { |
| 5803 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5804 | << Op->getSourceRange(); |
| 5805 | return QualType(); |
| 5806 | } |
| 5807 | |
| 5808 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5809 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5810 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5811 | if (getLangOptions().CPlusPlus) { |
| 5812 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5813 | << Op->getType() << Op->getSourceRange(); |
| 5814 | return QualType(); |
| 5815 | } |
| 5816 | |
| 5817 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5818 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5819 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5820 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5821 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5822 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5823 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5824 | // Diagnose bad cases where we step over interface counts. |
| 5825 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5826 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5827 | << PointeeTy << Op->getSourceRange(); |
| 5828 | return QualType(); |
| 5829 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 5830 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5831 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5832 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5833 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5834 | } else { |
| 5835 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 5836 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5837 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5838 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5839 | // 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] | 5840 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5841 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5842 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5843 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5846 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5847 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5848 | /// where the declaration is needed for type checking. We only need to |
| 5849 | /// handle cases when the expression references a function designator |
| 5850 | /// or is an lvalue. Here are some examples: |
| 5851 | /// - &(x) => x |
| 5852 | /// - &*****f => f for f a function designator. |
| 5853 | /// - &s.xx => s |
| 5854 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5855 | /// - *(x + 1) -> x, if x is an array |
| 5856 | /// - &"123"[2] -> 0 |
| 5857 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5858 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5859 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5860 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5861 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5862 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5863 | // If this is an arrow operator, the address is an offset from |
| 5864 | // the base's value, so the object the base refers to is |
| 5865 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5866 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5867 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5868 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5869 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5870 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5871 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5872 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5873 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5874 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5875 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5876 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5877 | } |
| 5878 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5879 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5880 | case Stmt::UnaryOperatorClass: { |
| 5881 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5882 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5883 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5884 | case UnaryOperator::Real: |
| 5885 | case UnaryOperator::Imag: |
| 5886 | case UnaryOperator::Extension: |
| 5887 | return getPrimaryDecl(UO->getSubExpr()); |
| 5888 | default: |
| 5889 | return 0; |
| 5890 | } |
| 5891 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5892 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5893 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5894 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5895 | // If the result of an implicit cast is an l-value, we care about |
| 5896 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5897 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5898 | default: |
| 5899 | return 0; |
| 5900 | } |
| 5901 | } |
| 5902 | |
| 5903 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5904 | /// 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] | 5905 | /// 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] | 5906 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5907 | /// 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] | 5908 | /// 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] | 5909 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5910 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5911 | // Make sure to ignore parentheses in subsequent checks |
| 5912 | op = op->IgnoreParens(); |
| 5913 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5914 | if (op->isTypeDependent()) |
| 5915 | return Context.DependentTy; |
| 5916 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5917 | if (getLangOptions().C99) { |
| 5918 | // Implement C99-only parts of addressof rules. |
| 5919 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5920 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5921 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5922 | // (assuming the deref expression is valid). |
| 5923 | return uOp->getSubExpr()->getType(); |
| 5924 | } |
| 5925 | // Technically, there should be a check for array subscript |
| 5926 | // expressions here, but the result of one is always an lvalue anyway. |
| 5927 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5928 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5929 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5930 | |
Sebastian Redl | e27d87f | 2010-01-11 15:56:56 +0000 | [diff] [blame] | 5931 | MemberExpr *ME = dyn_cast<MemberExpr>(op); |
| 5932 | if (lval == Expr::LV_MemberFunction && ME && |
| 5933 | isa<CXXMethodDecl>(ME->getMemberDecl())) { |
| 5934 | ValueDecl *dcl = cast<MemberExpr>(op)->getMemberDecl(); |
| 5935 | // &f where f is a member of the current object, or &o.f, or &p->f |
| 5936 | // All these are not allowed, and we need to catch them before the dcl |
| 5937 | // branch of the if, below. |
| 5938 | Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 5939 | << dcl; |
| 5940 | // FIXME: Improve this diagnostic and provide a fixit. |
| 5941 | |
| 5942 | // Now recover by acting as if the function had been accessed qualified. |
| 5943 | return Context.getMemberPointerType(op->getType(), |
| 5944 | Context.getTypeDeclType(cast<RecordDecl>(dcl->getDeclContext())) |
| 5945 | .getTypePtr()); |
| 5946 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5947 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5948 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5949 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5950 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5951 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5952 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5953 | return QualType(); |
| 5954 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5955 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5956 | // The operand cannot be a bit-field |
| 5957 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5958 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5959 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5960 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5961 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5962 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5963 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5964 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5965 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5966 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5967 | // cannot take address of a property expression. |
| 5968 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5969 | << "property expression" << op->getSourceRange(); |
| 5970 | return QualType(); |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5971 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5972 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 474e102 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5973 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5974 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5975 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5976 | return Context.OverloadTy; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5977 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5978 | // 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] | 5979 | // with the register storage-class specifier. |
| 5980 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5981 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5982 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5983 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5984 | return QualType(); |
| 5985 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5986 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5987 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5988 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5989 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5990 | // Could be a pointer to member, though, if there is an explicit |
| 5991 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5992 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5993 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5994 | if (Ctx && Ctx->isRecord()) { |
| 5995 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5997 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5998 | << FD->getDeclName() << FD->getType(); |
| 5999 | return QualType(); |
| 6000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6001 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6002 | return Context.getMemberPointerType(op->getType(), |
| 6003 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6004 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6005 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 6006 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 6007 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6008 | // As above. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6009 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 6010 | MD->isInstance()) |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 6011 | return Context.getMemberPointerType(op->getType(), |
| 6012 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 6013 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6014 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6015 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6016 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 6017 | if (lval == Expr::LV_IncompleteVoidType) { |
| 6018 | // Taking the address of a void variable is technically illegal, but we |
| 6019 | // allow it in cases which are otherwise valid. |
| 6020 | // Example: "extern void x; void* y = &x;". |
| 6021 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 6022 | } |
| 6023 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6024 | // If the operand has type "type", the result has type "pointer to type". |
| 6025 | return Context.getPointerType(op->getType()); |
| 6026 | } |
| 6027 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6028 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6029 | if (Op->isTypeDependent()) |
| 6030 | return Context.DependentTy; |
| 6031 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6032 | UsualUnaryConversions(Op); |
| 6033 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6034 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6035 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 6036 | // incomplete type or void. It would be possible to warn about dereferencing |
| 6037 | // a void pointer, but it's completely well-defined, and such a warning is |
| 6038 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6039 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 6040 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6041 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6042 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 6043 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6044 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6045 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6046 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6047 | return QualType(); |
| 6048 | } |
| 6049 | |
| 6050 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 6051 | tok::TokenKind Kind) { |
| 6052 | BinaryOperator::Opcode Opc; |
| 6053 | switch (Kind) { |
| 6054 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6055 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 6056 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6057 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 6058 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 6059 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 6060 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 6061 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 6062 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 6063 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 6064 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 6065 | case tok::less: Opc = BinaryOperator::LT; break; |
| 6066 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 6067 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 6068 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 6069 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 6070 | case tok::amp: Opc = BinaryOperator::And; break; |
| 6071 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 6072 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 6073 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 6074 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 6075 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 6076 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 6077 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 6078 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 6079 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 6080 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 6081 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 6082 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 6083 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 6084 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 6085 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 6086 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 6087 | } |
| 6088 | return Opc; |
| 6089 | } |
| 6090 | |
| 6091 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 6092 | tok::TokenKind Kind) { |
| 6093 | UnaryOperator::Opcode Opc; |
| 6094 | switch (Kind) { |
| 6095 | default: assert(0 && "Unknown unary op!"); |
| 6096 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 6097 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 6098 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 6099 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 6100 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 6101 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 6102 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 6103 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6104 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 6105 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 6106 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 6107 | } |
| 6108 | return Opc; |
| 6109 | } |
| 6110 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6111 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 6112 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 6113 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6114 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 6115 | unsigned Op, |
| 6116 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6117 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6118 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6119 | // The following two variables are used for compound assignment operators |
| 6120 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 6121 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6122 | |
| 6123 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6124 | case BinaryOperator::Assign: |
| 6125 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 6126 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6127 | case BinaryOperator::PtrMemD: |
| 6128 | case BinaryOperator::PtrMemI: |
| 6129 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 6130 | Opc == BinaryOperator::PtrMemI); |
| 6131 | break; |
| 6132 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6133 | case BinaryOperator::Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6134 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
| 6135 | Opc == BinaryOperator::Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6136 | break; |
| 6137 | case BinaryOperator::Rem: |
| 6138 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 6139 | break; |
| 6140 | case BinaryOperator::Add: |
| 6141 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 6142 | break; |
| 6143 | case BinaryOperator::Sub: |
| 6144 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 6145 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6146 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6147 | case BinaryOperator::Shr: |
| 6148 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 6149 | break; |
| 6150 | case BinaryOperator::LE: |
| 6151 | case BinaryOperator::LT: |
| 6152 | case BinaryOperator::GE: |
| 6153 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6154 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6155 | break; |
| 6156 | case BinaryOperator::EQ: |
| 6157 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6158 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6159 | break; |
| 6160 | case BinaryOperator::And: |
| 6161 | case BinaryOperator::Xor: |
| 6162 | case BinaryOperator::Or: |
| 6163 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 6164 | break; |
| 6165 | case BinaryOperator::LAnd: |
| 6166 | case BinaryOperator::LOr: |
| 6167 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 6168 | break; |
| 6169 | case BinaryOperator::MulAssign: |
| 6170 | case BinaryOperator::DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6171 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
| 6172 | Opc == BinaryOperator::DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6173 | CompLHSTy = CompResultTy; |
| 6174 | if (!CompResultTy.isNull()) |
| 6175 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6176 | break; |
| 6177 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6178 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6179 | CompLHSTy = CompResultTy; |
| 6180 | if (!CompResultTy.isNull()) |
| 6181 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6182 | break; |
| 6183 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6184 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6185 | if (!CompResultTy.isNull()) |
| 6186 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6187 | break; |
| 6188 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6189 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6190 | if (!CompResultTy.isNull()) |
| 6191 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6192 | break; |
| 6193 | case BinaryOperator::ShlAssign: |
| 6194 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6195 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6196 | CompLHSTy = CompResultTy; |
| 6197 | if (!CompResultTy.isNull()) |
| 6198 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6199 | break; |
| 6200 | case BinaryOperator::AndAssign: |
| 6201 | case BinaryOperator::XorAssign: |
| 6202 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6203 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6204 | CompLHSTy = CompResultTy; |
| 6205 | if (!CompResultTy.isNull()) |
| 6206 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6207 | break; |
| 6208 | case BinaryOperator::Comma: |
| 6209 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6210 | break; |
| 6211 | } |
| 6212 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6213 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6214 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6215 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6216 | else |
| 6217 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6218 | CompLHSTy, CompResultTy, |
| 6219 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6222 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6223 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6224 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6225 | const PartialDiagnostic &PD, |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6226 | SourceRange ParenRange, |
| 6227 | const PartialDiagnostic &SecondPD = PartialDiagnostic(0), |
| 6228 | SourceRange SecondParenRange = SourceRange()) { |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6229 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6230 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6231 | // We can't display the parentheses, so just dig the |
| 6232 | // warning/error and return. |
| 6233 | Self.Diag(Loc, PD); |
| 6234 | return; |
| 6235 | } |
| 6236 | |
| 6237 | Self.Diag(Loc, PD) |
| 6238 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6239 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6240 | |
| 6241 | if (!SecondPD.getDiagID()) |
| 6242 | return; |
| 6243 | |
| 6244 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6245 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6246 | // We can't display the parentheses, so just dig the |
| 6247 | // warning/error and return. |
| 6248 | Self.Diag(Loc, SecondPD); |
| 6249 | return; |
| 6250 | } |
| 6251 | |
| 6252 | Self.Diag(Loc, SecondPD) |
| 6253 | << CodeModificationHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6254 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6257 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6258 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6259 | /// comparison operators have higher precedence. The most typical example of |
| 6260 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6261 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6262 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6263 | typedef BinaryOperator BinOp; |
| 6264 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6265 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6266 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6267 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6268 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6269 | rhsopc = BO->getOpcode(); |
| 6270 | |
| 6271 | // Subs are not binary operators. |
| 6272 | if (lhsopc == -1 && rhsopc == -1) |
| 6273 | return; |
| 6274 | |
| 6275 | // Bitwise operations are sometimes used as eager logical ops. |
| 6276 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6277 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6278 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6279 | return; |
| 6280 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6281 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6282 | SuggestParentheses(Self, OpLoc, |
| 6283 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6284 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6285 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6286 | lhs->getSourceRange(), |
| 6287 | PDiag(diag::note_precedence_bitwise_first) |
| 6288 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6289 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 6290 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6291 | SuggestParentheses(Self, OpLoc, |
| 6292 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6293 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6294 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6295 | rhs->getSourceRange(), |
| 6296 | PDiag(diag::note_precedence_bitwise_first) |
| 6297 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6298 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6302 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6303 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 6304 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6305 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6306 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6307 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6308 | } |
| 6309 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6310 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6311 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 6312 | tok::TokenKind Kind, |
| 6313 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6314 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6315 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6316 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6317 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6318 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6319 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6320 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6321 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6322 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6323 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6324 | } |
| 6325 | |
| 6326 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 6327 | BinaryOperator::Opcode Opc, |
| 6328 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6329 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6331 | rhs->getType()->isOverloadableType())) { |
| 6332 | // Find all of the overloaded operators visible from this |
| 6333 | // point. We perform both an operator-name lookup from the local |
| 6334 | // scope and an argument-dependent lookup based on the types of |
| 6335 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 6336 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6337 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 6338 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6339 | if (S) |
| 6340 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6341 | Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6342 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6343 | DeclarationName OpName |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6344 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6345 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 6346 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6347 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6348 | // Build the (potentially-overloaded, potentially-dependent) |
| 6349 | // binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6350 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6351 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6353 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6354 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6357 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6358 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6359 | ExprArg InputArg) { |
| 6360 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6361 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6362 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6363 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6364 | QualType resultType; |
| 6365 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6366 | case UnaryOperator::OffsetOf: |
| 6367 | assert(false && "Invalid unary operator"); |
| 6368 | break; |
| 6369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6370 | case UnaryOperator::PreInc: |
| 6371 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6372 | case UnaryOperator::PostInc: |
| 6373 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6374 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6375 | Opc == UnaryOperator::PreInc || |
| 6376 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6377 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6378 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6379 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 6380 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6381 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 6382 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6383 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 6384 | break; |
| 6385 | case UnaryOperator::Plus: |
| 6386 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6387 | UsualUnaryConversions(Input); |
| 6388 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6389 | if (resultType->isDependentType()) |
| 6390 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6391 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 6392 | break; |
| 6393 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6394 | resultType->isEnumeralType()) |
| 6395 | break; |
| 6396 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 6397 | Opc == UnaryOperator::Plus && |
| 6398 | resultType->isPointerType()) |
| 6399 | break; |
| 6400 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6401 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6402 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6403 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6404 | UsualUnaryConversions(Input); |
| 6405 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6406 | if (resultType->isDependentType()) |
| 6407 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6408 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6409 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6410 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6411 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6412 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6413 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6414 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6415 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6416 | break; |
| 6417 | case UnaryOperator::LNot: // logical negation |
| 6418 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6419 | DefaultFunctionArrayConversion(Input); |
| 6420 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6421 | if (resultType->isDependentType()) |
| 6422 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6423 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6424 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6425 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6426 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6427 | // In C++, it's bool. C++ 5.3.1p8 |
| 6428 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6429 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6430 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6431 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 6432 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6433 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6434 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6435 | resultType = Input->getType(); |
| 6436 | break; |
| 6437 | } |
| 6438 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6439 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6440 | |
| 6441 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6442 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6445 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6446 | UnaryOperator::Opcode Opc, |
| 6447 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6448 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6449 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 6450 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6451 | // Find all of the overloaded operators visible from this |
| 6452 | // point. We perform both an operator-name lookup from the local |
| 6453 | // scope and an argument-dependent lookup based on the types of |
| 6454 | // the arguments. |
| 6455 | FunctionSet Functions; |
| 6456 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 6457 | if (OverOp != OO_None) { |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6458 | if (S) |
| 6459 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 6460 | Functions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6461 | DeclarationName OpName |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6462 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 6463 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6464 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6465 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6466 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 6467 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6469 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 6470 | } |
| 6471 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6472 | // Unary Operators. 'Tok' is the token for the operator. |
| 6473 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6474 | tok::TokenKind Op, ExprArg input) { |
| 6475 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 6476 | } |
| 6477 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6478 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6479 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 6480 | SourceLocation LabLoc, |
| 6481 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6482 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 6483 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6484 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6485 | // If we haven't seen this label yet, create a forward reference. It |
| 6486 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6487 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6488 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6489 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6490 | // 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] | 6491 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6492 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6495 | Sema::OwningExprResult |
| 6496 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 6497 | SourceLocation RPLoc) { // "({..})" |
| 6498 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6499 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6500 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6501 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6502 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6503 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6504 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6505 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6506 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6507 | // example, it is not possible to goto into a stmt expression apparently. |
| 6508 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6509 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6510 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6511 | // as the type of the stmtexpr. |
| 6512 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6513 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6514 | if (!Compound->body_empty()) { |
| 6515 | Stmt *LastStmt = Compound->body_back(); |
| 6516 | // If LastStmt is a label, skip down through into the body. |
| 6517 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6518 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6519 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6520 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6521 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6522 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6523 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6524 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6525 | // expressions are not lvalues. |
| 6526 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6527 | substmt.release(); |
| 6528 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6529 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6530 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6531 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 6532 | SourceLocation BuiltinLoc, |
| 6533 | SourceLocation TypeLoc, |
| 6534 | TypeTy *argty, |
| 6535 | OffsetOfComponent *CompPtr, |
| 6536 | unsigned NumComponents, |
| 6537 | SourceLocation RPLoc) { |
| 6538 | // FIXME: This function leaks all expressions in the offset components on |
| 6539 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6540 | // FIXME: Preserve type source info. |
| 6541 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6542 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6543 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6544 | bool Dependent = ArgTy->isDependentType(); |
| 6545 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6546 | // We must have at least one component that refers to the type, and the first |
| 6547 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6548 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6549 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6550 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6551 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6552 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 6553 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 6554 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6555 | // Otherwise, create a null pointer as the base, and iteratively process |
| 6556 | // the offsetof designators. |
| 6557 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 6558 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6559 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6560 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 6561 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6562 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6563 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6564 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6565 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6566 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6567 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6568 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6569 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6570 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 6571 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6572 | |
John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 6573 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 6574 | diag::err_offsetof_incomplete_type)) |
| 6575 | return ExprError(); |
| 6576 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6577 | // FIXME: Dependent case loses a lot of information here. And probably |
| 6578 | // leaks like a sieve. |
| 6579 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6580 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6581 | if (OC.isBrackets) { |
| 6582 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6583 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 6584 | if (!AT) { |
| 6585 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6586 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6587 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 6591 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6592 | // Promote the array so it looks more like a normal array subscript |
| 6593 | // expression. |
| 6594 | DefaultFunctionArrayConversion(Res); |
| 6595 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6596 | // C99 6.5.2.1p1 |
| 6597 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6598 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6599 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6600 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 6601 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6602 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6603 | |
| 6604 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 6605 | OC.LocEnd); |
| 6606 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6607 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6608 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6609 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6610 | if (!RC) { |
| 6611 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6612 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6613 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6614 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 6615 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6616 | // Get the decl corresponding to this. |
| 6617 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6618 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 6619 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| 6620 | DiagRuntimeBehavior(BuiltinLoc, |
| 6621 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6622 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6623 | << Res->getType())) |
| 6624 | DidWarnAboutNonPOD = true; |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6626 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6627 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6628 | LookupQualifiedName(R, RD); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6629 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6630 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6631 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6632 | if (!MemberDecl) |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6633 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6634 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6635 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6636 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 6637 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6638 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 6639 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 09b6d0e | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 6640 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6641 | } else { |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 6642 | PerformObjectMemberConversion(Res, MemberDecl); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6643 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 6644 | // doesn't matter here. |
| 6645 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 6646 | MemberDecl->getType().getNonReferenceType()); |
| 6647 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6648 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6649 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6650 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6651 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 6652 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6656 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 6657 | TypeTy *arg1,TypeTy *arg2, |
| 6658 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6659 | // FIXME: Preserve type source info. |
| 6660 | QualType argT1 = GetTypeFromParser(arg1); |
| 6661 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6662 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6663 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 6665 | if (getLangOptions().CPlusPlus) { |
| 6666 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 6667 | << SourceRange(BuiltinLoc, RPLoc); |
| 6668 | return ExprError(); |
| 6669 | } |
| 6670 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6671 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 6672 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6675 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 6676 | ExprArg cond, |
| 6677 | ExprArg expr1, ExprArg expr2, |
| 6678 | SourceLocation RPLoc) { |
| 6679 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 6680 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 6681 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6682 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6683 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 6684 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6685 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6686 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 6687 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6688 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6689 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6690 | } else { |
| 6691 | // The conditional expression is required to be a constant expression. |
| 6692 | llvm::APSInt condEval(32); |
| 6693 | SourceLocation ExpLoc; |
| 6694 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6695 | return ExprError(Diag(ExpLoc, |
| 6696 | diag::err_typecheck_choose_expr_requires_constant) |
| 6697 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6698 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6699 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 6700 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6701 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 6702 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6703 | } |
| 6704 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6705 | cond.release(); expr1.release(); expr2.release(); |
| 6706 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6707 | resType, RPLoc, |
| 6708 | resType->isDependentType(), |
| 6709 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6712 | //===----------------------------------------------------------------------===// |
| 6713 | // Clang Extensions. |
| 6714 | //===----------------------------------------------------------------------===// |
| 6715 | |
| 6716 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6717 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6718 | // Analyze block parameters. |
| 6719 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6720 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6721 | // Add BSI to CurBlock. |
| 6722 | BSI->PrevBlockInfo = CurBlock; |
| 6723 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6724 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6725 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6726 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 6727 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 6728 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6729 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 6730 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6731 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6732 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Ted Kremenek | 3cdff23 | 2009-12-07 22:01:30 +0000 | [diff] [blame] | 6733 | CurContext->addDecl(BSI->TheDecl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6734 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6737 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6738 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6739 | |
| 6740 | if (ParamInfo.getNumTypeObjects() == 0 |
| 6741 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6742 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6743 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 6744 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6745 | if (T->isArrayType()) { |
| 6746 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6747 | diag::err_block_returns_array); |
| 6748 | return; |
| 6749 | } |
| 6750 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6751 | // The parameter list is optional, if there was none, assume (). |
| 6752 | if (!T->isFunctionType()) |
| 6753 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6754 | |
| 6755 | CurBlock->hasPrototype = true; |
| 6756 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6757 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6758 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6759 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6760 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6761 | // FIXME: remove the attribute. |
| 6762 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6763 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6764 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6765 | // Do not allow returning a objc interface by-value. |
| 6766 | if (RetTy->isObjCInterfaceType()) { |
| 6767 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6768 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6769 | return; |
| 6770 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6771 | return; |
| 6772 | } |
| 6773 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6774 | // Analyze arguments to block. |
| 6775 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6776 | "Not a function declarator!"); |
| 6777 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6778 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6779 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6780 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6781 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6782 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6783 | // no arguments, not a function that takes a single void argument. |
| 6784 | if (FTI.hasPrototype && |
| 6785 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6786 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6787 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6788 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6789 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6790 | } else if (FTI.hasPrototype) { |
| 6791 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6792 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6793 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6794 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6795 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6796 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6797 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6798 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6799 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6800 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6801 | // If this has an identifier, add it to the scope stack. |
| 6802 | if ((*AI)->getIdentifier()) |
| 6803 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6804 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6805 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6806 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6807 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6808 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6809 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6810 | // FIXME: remove the attribute. |
| 6811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6812 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6813 | // Analyze the return type. |
| 6814 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6815 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6816 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6817 | // Do not allow returning a objc interface by-value. |
| 6818 | if (RetTy->isObjCInterfaceType()) { |
| 6819 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6820 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6821 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6822 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6826 | /// is invoked to pop the information about the block from the action impl. |
| 6827 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6828 | // Ensure that CurBlock is deleted. |
| 6829 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6830 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6831 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6832 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6833 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6834 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6835 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6836 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6840 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6841 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6842 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6843 | // If blocks are disabled, emit an error. |
| 6844 | if (!LangOpts.Blocks) |
| 6845 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6846 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6847 | // Ensure that CurBlock is deleted. |
| 6848 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6849 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6850 | PopDeclContext(); |
| 6851 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6852 | // Pop off CurBlock, handle nested blocks. |
| 6853 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6854 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6855 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6856 | if (!BSI->ReturnType.isNull()) |
| 6857 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6858 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6859 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6860 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6861 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6862 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6863 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6864 | QualType BlockTy; |
| 6865 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6866 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6867 | NoReturn); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6868 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6869 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6870 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6871 | NoReturn); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6872 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6873 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6874 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6875 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6876 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6877 | // If needed, diagnose invalid gotos and switches in the block. |
| 6878 | if (CurFunctionNeedsScopeChecking) |
| 6879 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6880 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6881 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6882 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | fa6ef18 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 6883 | AnalysisContext AC(BSI->TheDecl); |
| 6884 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody(), AC); |
| 6885 | CheckUnreachable(AC); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6886 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6887 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6888 | } |
| 6889 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6890 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6891 | ExprArg expr, TypeTy *type, |
| 6892 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6893 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6894 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6895 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6896 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6897 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6898 | |
| 6899 | // Get the va_list type |
| 6900 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6901 | if (VaListType->isArrayType()) { |
| 6902 | // Deal with implicit array decay; for example, on x86-64, |
| 6903 | // va_list is an array, but it's supposed to decay to |
| 6904 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6905 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6906 | // Make sure the input expression also decays appropriately. |
| 6907 | UsualUnaryConversions(E); |
| 6908 | } else { |
| 6909 | // Otherwise, the va_list argument must be an l-value because |
| 6910 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6911 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6912 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6913 | return ExprError(); |
| 6914 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6915 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6916 | if (!E->isTypeDependent() && |
| 6917 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6918 | return ExprError(Diag(E->getLocStart(), |
| 6919 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6920 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6921 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6922 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6923 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6924 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6925 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6926 | expr.release(); |
| 6927 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6928 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6929 | } |
| 6930 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6931 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6932 | // The type of __null will be int or long, depending on the size of |
| 6933 | // pointers on the target. |
| 6934 | QualType Ty; |
| 6935 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6936 | Ty = Context.IntTy; |
| 6937 | else |
| 6938 | Ty = Context.LongTy; |
| 6939 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6940 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6943 | static void |
| 6944 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6945 | QualType DstType, |
| 6946 | Expr *SrcExpr, |
| 6947 | CodeModificationHint &Hint) { |
| 6948 | if (!SemaRef.getLangOptions().ObjC1) |
| 6949 | return; |
| 6950 | |
| 6951 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6952 | if (!PT) |
| 6953 | return; |
| 6954 | |
| 6955 | // Check if the destination is of type 'id'. |
| 6956 | if (!PT->isObjCIdType()) { |
| 6957 | // Check if the destination is the 'NSString' interface. |
| 6958 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6959 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6960 | return; |
| 6961 | } |
| 6962 | |
| 6963 | // Strip off any parens and casts. |
| 6964 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6965 | if (!SL || SL->isWide()) |
| 6966 | return; |
| 6967 | |
| 6968 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6969 | } |
| 6970 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6971 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6972 | SourceLocation Loc, |
| 6973 | QualType DstType, QualType SrcType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6974 | Expr *SrcExpr, AssignmentAction Action) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6975 | // Decode the result (notice that AST's are still created for extensions). |
| 6976 | bool isInvalid = false; |
| 6977 | unsigned DiagKind; |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6978 | CodeModificationHint Hint; |
| 6979 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6980 | switch (ConvTy) { |
| 6981 | default: assert(0 && "Unknown conversion type"); |
| 6982 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6983 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6984 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6985 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6986 | case IntToPointer: |
| 6987 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6988 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6989 | case IncompatiblePointer: |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6990 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6991 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6992 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6993 | case IncompatiblePointerSign: |
| 6994 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6995 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6996 | case FunctionVoidPointer: |
| 6997 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6998 | break; |
| 6999 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 7000 | // If the qualifiers lost were because we were applying the |
| 7001 | // (deprecated) C++ conversion from a string literal to a char* |
| 7002 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 7003 | // Ideally, this check would be performed in |
| 7004 | // CheckPointerTypesForAssignment. However, that would require a |
| 7005 | // bit of refactoring (so that the second argument is an |
| 7006 | // expression, rather than a type), which should be done as part |
| 7007 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 7008 | // C++ semantics. |
| 7009 | if (getLangOptions().CPlusPlus && |
| 7010 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 7011 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7012 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 7013 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 7014 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 7015 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 7016 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7017 | case IntToBlockPointer: |
| 7018 | DiagKind = diag::err_int_to_block_pointer; |
| 7019 | break; |
| 7020 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 7021 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7022 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7023 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7024 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7025 | // it can give a more specific diagnostic. |
| 7026 | DiagKind = diag::warn_incompatible_qualified_id; |
| 7027 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7028 | case IncompatibleVectors: |
| 7029 | DiagKind = diag::warn_incompatible_vectors; |
| 7030 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7031 | case Incompatible: |
| 7032 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 7033 | isInvalid = true; |
| 7034 | break; |
| 7035 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7037 | Diag(Loc, DiagKind) << DstType << SrcType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7038 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7039 | return isInvalid; |
| 7040 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7041 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 7042 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7043 | llvm::APSInt ICEResult; |
| 7044 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 7045 | if (Result) |
| 7046 | *Result = ICEResult; |
| 7047 | return false; |
| 7048 | } |
| 7049 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7050 | Expr::EvalResult EvalResult; |
| 7051 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7052 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7053 | EvalResult.HasSideEffects) { |
| 7054 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 7055 | |
| 7056 | if (EvalResult.Diag) { |
| 7057 | // We only show the note if it's not the usual "invalid subexpression" |
| 7058 | // or if it's actually in a subexpression. |
| 7059 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 7060 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 7061 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 7062 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7063 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7064 | return true; |
| 7065 | } |
| 7066 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7067 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 7068 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7069 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7070 | if (EvalResult.Diag && |
| 7071 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 7072 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7073 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7074 | if (Result) |
| 7075 | *Result = EvalResult.Val.getInt(); |
| 7076 | return false; |
| 7077 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7078 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7079 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7080 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7081 | ExprEvalContexts.push_back( |
| 7082 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7085 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7086 | Sema::PopExpressionEvaluationContext() { |
| 7087 | // Pop the current expression evaluation context off the stack. |
| 7088 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 7089 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7090 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 7091 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 7092 | if (Rec.PotentiallyReferenced) { |
| 7093 | // Mark any remaining declarations in the current position of the stack |
| 7094 | // as "referenced". If they were not meant to be referenced, semantic |
| 7095 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 7096 | for (PotentiallyReferencedDecls::iterator |
| 7097 | I = Rec.PotentiallyReferenced->begin(), |
| 7098 | IEnd = Rec.PotentiallyReferenced->end(); |
| 7099 | I != IEnd; ++I) |
| 7100 | MarkDeclarationReferenced(I->first, I->second); |
| 7101 | } |
| 7102 | |
| 7103 | if (Rec.PotentiallyDiagnosed) { |
| 7104 | // Emit any pending diagnostics. |
| 7105 | for (PotentiallyEmittedDiagnostics::iterator |
| 7106 | I = Rec.PotentiallyDiagnosed->begin(), |
| 7107 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 7108 | I != IEnd; ++I) |
| 7109 | Diag(I->first, I->second); |
| 7110 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | // When are coming out of an unevaluated context, clear out any |
| 7114 | // temporaries that we may have created as part of the evaluation of |
| 7115 | // the expression in that context: they aren't relevant because they |
| 7116 | // will never be constructed. |
| 7117 | if (Rec.Context == Unevaluated && |
| 7118 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 7119 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 7120 | ExprTemporaries.end()); |
| 7121 | |
| 7122 | // Destroy the popped expression evaluation record. |
| 7123 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7124 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7125 | |
| 7126 | /// \brief Note that the given declaration was referenced in the source code. |
| 7127 | /// |
| 7128 | /// This routine should be invoke whenever a given declaration is referenced |
| 7129 | /// in the source code, and where that reference occurred. If this declaration |
| 7130 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 7131 | /// C99 6.9p3), then the declaration will be marked as used. |
| 7132 | /// |
| 7133 | /// \param Loc the location where the declaration was referenced. |
| 7134 | /// |
| 7135 | /// \param D the declaration that has been referenced by the source code. |
| 7136 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 7137 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7138 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7139 | if (D->isUsed()) |
| 7140 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7141 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 7142 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 7143 | // template or not. The reason for this is that unevaluated expressions |
| 7144 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 7145 | // -Wunused-parameters) |
| 7146 | if (isa<ParmVarDecl>(D) || |
| 7147 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7148 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7149 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7150 | // Do not mark anything as "used" within a dependent context; wait for |
| 7151 | // an instantiation. |
| 7152 | if (CurContext->isDependentContext()) |
| 7153 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7154 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7155 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7156 | case Unevaluated: |
| 7157 | // We are in an expression that is not potentially evaluated; do nothing. |
| 7158 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7159 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7160 | case PotentiallyEvaluated: |
| 7161 | // We are in a potentially-evaluated expression, so this declaration is |
| 7162 | // "used"; handle this below. |
| 7163 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7164 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7165 | case PotentiallyPotentiallyEvaluated: |
| 7166 | // We are in an expression that may be potentially evaluated; queue this |
| 7167 | // declaration reference until we know whether the expression is |
| 7168 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7169 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7170 | return; |
| 7171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7172 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7173 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 7174 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7175 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7176 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 7177 | if (!Constructor->isUsed()) |
| 7178 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7179 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 7180 | Constructor->isCopyConstructor(TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7181 | if (!Constructor->isUsed()) |
| 7182 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7183 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7184 | |
| 7185 | MaybeMarkVirtualMembersReferenced(Loc, Constructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7186 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 7187 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 7188 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7189 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7190 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7191 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7192 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 7193 | if (!MethodDecl->isUsed()) |
| 7194 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 7195 | } |
| 7196 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7197 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7198 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7199 | // class templates. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7200 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7201 | bool AlreadyInstantiated = false; |
| 7202 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7203 | = Function->getTemplateSpecializationInfo()) { |
| 7204 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7205 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7206 | else if (SpecInfo->getTemplateSpecializationKind() |
| 7207 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7208 | AlreadyInstantiated = true; |
| 7209 | } else if (MemberSpecializationInfo *MSInfo |
| 7210 | = Function->getMemberSpecializationInfo()) { |
| 7211 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7212 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7213 | else if (MSInfo->getTemplateSpecializationKind() |
| 7214 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7215 | AlreadyInstantiated = true; |
| 7216 | } |
| 7217 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame^] | 7218 | if (!AlreadyInstantiated) { |
| 7219 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 7220 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 7221 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 7222 | Loc)); |
| 7223 | else |
| 7224 | PendingImplicitInstantiations.push_back(std::make_pair(Function, |
| 7225 | Loc)); |
| 7226 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7229 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7230 | Function->setUsed(true); |
| 7231 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7233 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7234 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7235 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7236 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7237 | Var->getInstantiatedFromStaticDataMember()) { |
| 7238 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7239 | assert(MSInfo && "Missing member specialization information?"); |
| 7240 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7241 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7242 | MSInfo->setPointOfInstantiation(Loc); |
| 7243 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 7244 | } |
| 7245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7246 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7247 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7248 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7249 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7250 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7251 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7252 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7253 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7254 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 7255 | /// of the program being compiled. |
| 7256 | /// |
| 7257 | /// This routine emits the given diagnostic when the code currently being |
| 7258 | /// type-checked is "potentially evaluated", meaning that there is a |
| 7259 | /// possibility that the code will actually be executable. Code in sizeof() |
| 7260 | /// expressions, code used only during overload resolution, etc., are not |
| 7261 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 7262 | /// in the absolutely nutty case of potentially potentially evaluated |
| 7263 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
| 7264 | /// later. |
| 7265 | /// |
| 7266 | /// This routine should be used for all diagnostics that describe the run-time |
| 7267 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 7268 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 7269 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
| 7270 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, |
| 7271 | const PartialDiagnostic &PD) { |
| 7272 | switch (ExprEvalContexts.back().Context ) { |
| 7273 | case Unevaluated: |
| 7274 | // The argument will never be evaluated, so don't complain. |
| 7275 | break; |
| 7276 | |
| 7277 | case PotentiallyEvaluated: |
| 7278 | Diag(Loc, PD); |
| 7279 | return true; |
| 7280 | |
| 7281 | case PotentiallyPotentiallyEvaluated: |
| 7282 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 7283 | break; |
| 7284 | } |
| 7285 | |
| 7286 | return false; |
| 7287 | } |
| 7288 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7289 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7290 | CallExpr *CE, FunctionDecl *FD) { |
| 7291 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7292 | return false; |
| 7293 | |
| 7294 | PartialDiagnostic Note = |
| 7295 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7296 | << FD->getDeclName() : PDiag(); |
| 7297 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 7298 | |
| 7299 | if (RequireCompleteType(Loc, ReturnType, |
| 7300 | FD ? |
| 7301 | PDiag(diag::err_call_function_incomplete_return) |
| 7302 | << CE->getSourceRange() << FD->getDeclName() : |
| 7303 | PDiag(diag::err_call_incomplete_return) |
| 7304 | << CE->getSourceRange(), |
| 7305 | std::make_pair(NoteLoc, Note))) |
| 7306 | return true; |
| 7307 | |
| 7308 | return false; |
| 7309 | } |
| 7310 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7311 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7312 | // will prevent this condition from triggering, which is what we want. |
| 7313 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7314 | SourceLocation Loc; |
| 7315 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7316 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7317 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7318 | if (isa<BinaryOperator>(E)) { |
| 7319 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 7320 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 7321 | return; |
| 7322 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7323 | // Greylist some idioms by putting them into a warning subcategory. |
| 7324 | if (ObjCMessageExpr *ME |
| 7325 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7326 | Selector Sel = ME->getSelector(); |
| 7327 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7328 | // self = [<foo> init...] |
| 7329 | if (isSelfExpr(Op->getLHS()) |
| 7330 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7331 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7332 | |
| 7333 | // <foo> = [<bar> nextObject] |
| 7334 | else if (Sel.isUnarySelector() && |
| 7335 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7336 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7337 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7338 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7339 | Loc = Op->getOperatorLoc(); |
| 7340 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7341 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7342 | if (Op->getOperator() != OO_Equal) |
| 7343 | return; |
| 7344 | |
| 7345 | Loc = Op->getOperatorLoc(); |
| 7346 | } else { |
| 7347 | // Not an assignment. |
| 7348 | return; |
| 7349 | } |
| 7350 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7351 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7352 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7353 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7354 | Diag(Loc, diagnostic) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7355 | << E->getSourceRange() |
| 7356 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 7357 | << CodeModificationHint::CreateInsertion(Close, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7358 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 7359 | << CodeModificationHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
| 7362 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7363 | DiagnoseAssignmentAsCondition(E); |
| 7364 | |
| 7365 | if (!E->isTypeDependent()) { |
| 7366 | DefaultFunctionArrayConversion(E); |
| 7367 | |
| 7368 | QualType T = E->getType(); |
| 7369 | |
| 7370 | if (getLangOptions().CPlusPlus) { |
| 7371 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7372 | return true; |
| 7373 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7374 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7375 | << T << E->getSourceRange(); |
| 7376 | return true; |
| 7377 | } |
| 7378 | } |
| 7379 | |
| 7380 | return false; |
| 7381 | } |