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 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
| 17 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/LiteralSupport.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/Designator.h" |
| 35 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 38 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the use of this declaration is valid, and |
| 44 | /// emit any corresponding diagnostics. |
| 45 | /// |
| 46 | /// This routine diagnoses various problems with referencing |
| 47 | /// declarations that can occur when using a declaration. For example, |
| 48 | /// it might warn if a deprecated or unavailable declaration is being |
| 49 | /// used, or produce an error (and return true) if a C++0x deleted |
| 50 | /// function is being used. |
| 51 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 52 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 53 | /// decls. |
| 54 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 55 | /// \returns true if there was an error (this declaration cannot be |
| 56 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 57 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 58 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 59 | bool UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 60 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 61 | // If there were any diagnostics suppressed by template argument deduction, |
| 62 | // emit them now. |
| 63 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 64 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 65 | if (Pos != SuppressedDiagnostics.end()) { |
| 66 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
| 67 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 68 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 69 | |
| 70 | // Clear out the list of suppressed diagnostics, so that we don't emit |
| 71 | // them again for this specialization. However, we don't remove this |
| 72 | // entry from the table, because we want to avoid ever emitting these |
| 73 | // diagnostics again. |
| 74 | Suppressed.clear(); |
| 75 | } |
| 76 | } |
| 77 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 78 | // See if this is an auto-typed variable whose initializer we are parsing. |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 79 | if (ParsingInitForAutoVars.count(D)) { |
| 80 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 81 | << D->getDeclName(); |
| 82 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 85 | // See if the decl is deprecated. |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 86 | if (const DeprecatedAttr *DA = D->getAttr<DeprecatedAttr>()) |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 87 | EmitDeprecationWarning(D, DA->getMessage(), Loc, UnknownObjCClass); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 88 | |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 89 | // See if the decl is unavailable |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 90 | if (const UnavailableAttr *UA = D->getAttr<UnavailableAttr>()) { |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 91 | if (UA->getMessage().empty()) { |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 92 | if (!UnknownObjCClass) |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 93 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 94 | else |
| 95 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 96 | << D->getDeclName(); |
| 97 | } |
| 98 | else |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 99 | Diag(Loc, diag::err_unavailable_message) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 100 | << D->getDeclName() << UA->getMessage(); |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 101 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 102 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 104 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 105 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 106 | if (FD->isDeleted()) { |
| 107 | Diag(Loc, diag::err_deleted_function_use); |
| 108 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 109 | return true; |
| 110 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 111 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 112 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 113 | // Warn if this is used but marked unused. |
| 114 | if (D->hasAttr<UnusedAttr>()) |
| 115 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 116 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 117 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 120 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 122 | /// attribute. It warns if call does not have the sentinel argument. |
| 123 | /// |
| 124 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 126 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 128 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 129 | |
| 130 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 131 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 132 | int sentinelPos = attr->getSentinel(); |
| 133 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 135 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 136 | // 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] | 137 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 138 | bool warnNotEnoughArgs = false; |
| 139 | int isMethod = 0; |
| 140 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 141 | // skip over named parameters. |
| 142 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 143 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 144 | if (nullPos) |
| 145 | --nullPos; |
| 146 | else |
| 147 | ++i; |
| 148 | } |
| 149 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 150 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 151 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 152 | // skip over named parameters. |
| 153 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 154 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 155 | if (nullPos) |
| 156 | --nullPos; |
| 157 | else |
| 158 | ++i; |
| 159 | } |
| 160 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 161 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 162 | // block or function pointer call. |
| 163 | QualType Ty = V->getType(); |
| 164 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 166 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 167 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 168 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 169 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 170 | unsigned k; |
| 171 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 172 | if (nullPos) |
| 173 | --nullPos; |
| 174 | else |
| 175 | ++i; |
| 176 | } |
| 177 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 178 | } |
| 179 | if (Ty->isBlockPointerType()) |
| 180 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 181 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 182 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 183 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 184 | return; |
| 185 | |
| 186 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 187 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 188 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | int sentinel = i; |
| 192 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 193 | --sentinelPos; |
| 194 | ++i; |
| 195 | } |
| 196 | if (sentinelPos > 0) { |
| 197 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 198 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | while (i < NumArgs-1) { |
| 202 | ++i; |
| 203 | ++sentinel; |
| 204 | } |
| 205 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 206 | if (!sentinelExpr) return; |
| 207 | if (sentinelExpr->isTypeDependent()) return; |
| 208 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 209 | |
| 210 | // nullptr_t is always treated as null. |
| 211 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 212 | |
Fariborz Jahanian | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 213 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 214 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 215 | Expr::NPC_ValueDependentIsNull)) |
| 216 | return; |
| 217 | |
| 218 | // Unfortunately, __null has type 'int'. |
| 219 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 220 | |
| 221 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 222 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 225 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 226 | Expr *Ex = (Expr *)E; |
| 227 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 228 | } |
| 229 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
| 231 | // Standard Promotions and Conversions |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 234 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 235 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 236 | QualType Ty = E->getType(); |
| 237 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 238 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 239 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | ImpCastExprToType(E, Context.getPointerType(Ty), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 241 | CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 242 | else if (Ty->isArrayType()) { |
| 243 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 244 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 245 | // type 'array of type' is converted to an expression that has type 'pointer |
| 246 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 247 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 248 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 249 | // |
| 250 | // C++ 4.2p1: |
| 251 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 252 | // T" can be converted to an rvalue of type "pointer to T". |
| 253 | // |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 254 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 255 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 256 | CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 258 | } |
| 259 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 260 | void Sema::DefaultLvalueConversion(Expr *&E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 261 | // C++ [conv.lval]p1: |
| 262 | // A glvalue of a non-function, non-array type T can be |
| 263 | // converted to a prvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 264 | if (!E->isGLValue()) return; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 265 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 266 | QualType T = E->getType(); |
| 267 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 268 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 269 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 270 | if (E->getObjectKind() == OK_ObjCProperty) { |
| 271 | ConvertPropertyForRValue(E); |
| 272 | if (!E->isGLValue()) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 273 | return; |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 274 | } |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 275 | |
| 276 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 277 | // expressions of certain types in C++. |
| 278 | if (getLangOptions().CPlusPlus && |
| 279 | (E->getType() == Context.OverloadTy || |
| 280 | T->isDependentType() || |
| 281 | T->isRecordType())) |
| 282 | return; |
| 283 | |
| 284 | // The C standard is actually really unclear on this point, and |
| 285 | // DR106 tells us what the result should be but not why. It's |
| 286 | // generally best to say that void types just doesn't undergo |
| 287 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 288 | // 'void' type are never l-values, but qualified void can be. |
| 289 | if (T->isVoidType()) |
| 290 | return; |
| 291 | |
| 292 | // C++ [conv.lval]p1: |
| 293 | // [...] If T is a non-class type, the type of the prvalue is the |
| 294 | // cv-unqualified version of T. Otherwise, the type of the |
| 295 | // rvalue is T. |
| 296 | // |
| 297 | // C99 6.3.2.1p2: |
| 298 | // If the lvalue has qualified type, the value has the unqualified |
| 299 | // version of the type of the lvalue; otherwise, the value has the |
| 300 | // type of the lvalue. |
| 301 | if (T.hasQualifiers()) |
| 302 | T = T.getUnqualifiedType(); |
| 303 | |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 304 | if (const ArraySubscriptExpr *ae = dyn_cast<ArraySubscriptExpr>(E)) |
| 305 | CheckArrayAccess(ae); |
| 306 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 307 | E = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 308 | E, 0, VK_RValue); |
| 309 | } |
| 310 | |
| 311 | void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) { |
| 312 | DefaultFunctionArrayConversion(E); |
| 313 | DefaultLvalueConversion(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 317 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 319 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 320 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 321 | /// In these instances, this routine should *not* be called. |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 322 | Expr *Sema::UsualUnaryConversions(Expr *&E) { |
| 323 | // First, convert to an r-value. |
| 324 | DefaultFunctionArrayLvalueConversion(E); |
| 325 | |
| 326 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 327 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 328 | |
| 329 | // Try to perform integral promotions if the object has a theoretically |
| 330 | // promotable type. |
| 331 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 332 | // C99 6.3.1.1p2: |
| 333 | // |
| 334 | // The following may be used in an expression wherever an int or |
| 335 | // unsigned int may be used: |
| 336 | // - an object or expression with an integer type whose integer |
| 337 | // conversion rank is less than or equal to the rank of int |
| 338 | // and unsigned int. |
| 339 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 340 | // |
| 341 | // If an int can represent all values of the original type, the |
| 342 | // value is converted to an int; otherwise, it is converted to an |
| 343 | // unsigned int. These are called the integer promotions. All |
| 344 | // other types are unchanged by the integer promotions. |
| 345 | |
| 346 | QualType PTy = Context.isPromotableBitField(E); |
| 347 | if (!PTy.isNull()) { |
| 348 | ImpCastExprToType(E, PTy, CK_IntegralCast); |
| 349 | return E; |
| 350 | } |
| 351 | if (Ty->isPromotableIntegerType()) { |
| 352 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 353 | ImpCastExprToType(E, PT, CK_IntegralCast); |
| 354 | return E; |
| 355 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 356 | } |
| 357 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 358 | return E; |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 361 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | /// 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] | 363 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 364 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 365 | QualType Ty = Expr->getType(); |
| 366 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 368 | UsualUnaryConversions(Expr); |
| 369 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 370 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 371 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 372 | return ImpCastExprToType(Expr, Context.DoubleTy, CK_FloatingCast); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 375 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 376 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 377 | /// interfaces passed by value. This returns true if the argument type is |
| 378 | /// completely illegal. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 379 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT, |
| 380 | FunctionDecl *FDecl) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 381 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 383 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 384 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 385 | // etc. |
| 386 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
| 387 | return false; |
| 388 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 389 | if (Expr->getType()->isObjCObjectType() && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 390 | DiagRuntimeBehavior(Expr->getLocStart(), Expr, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 391 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 392 | << Expr->getType() << CT)) |
| 393 | return true; |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 395 | if (!Expr->getType()->isPODType() && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 396 | DiagRuntimeBehavior(Expr->getLocStart(), Expr, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 397 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 398 | << Expr->getType() << CT)) |
| 399 | return true; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 400 | |
| 401 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 404 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 405 | /// 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] | 406 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 407 | /// responsible for emitting appropriate error diagnostics. |
| 408 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 409 | /// GCC. |
| 410 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 411 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 412 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 413 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 414 | |
| 415 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 418 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 419 | QualType lhs = |
| 420 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 422 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 423 | |
| 424 | // If both types are identical, no conversion is needed. |
| 425 | if (lhs == rhs) |
| 426 | return lhs; |
| 427 | |
| 428 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 429 | // The caller can deal with this (e.g. pointer + int). |
| 430 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 431 | return lhs; |
| 432 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 433 | // Apply unary and bitfield promotions to the LHS's type. |
| 434 | QualType lhs_unpromoted = lhs; |
| 435 | if (lhs->isPromotableIntegerType()) |
| 436 | lhs = Context.getPromotedIntegerType(lhs); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 437 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 438 | if (!LHSBitfieldPromoteTy.isNull()) |
| 439 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 440 | if (lhs != lhs_unpromoted && !isCompAssign) |
| 441 | ImpCastExprToType(lhsExpr, lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 442 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 443 | // If both types are identical, no conversion is needed. |
| 444 | if (lhs == rhs) |
| 445 | return lhs; |
| 446 | |
| 447 | // At this point, we have two different arithmetic types. |
| 448 | |
| 449 | // Handle complex types first (C99 6.3.1.8p1). |
| 450 | bool LHSComplexFloat = lhs->isComplexType(); |
| 451 | bool RHSComplexFloat = rhs->isComplexType(); |
| 452 | if (LHSComplexFloat || RHSComplexFloat) { |
| 453 | // if we have an integer operand, the result is the complex type. |
| 454 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 455 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 456 | if (rhs->isIntegerType()) { |
| 457 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 458 | ImpCastExprToType(rhsExpr, fp, CK_IntegralToFloating); |
| 459 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
| 460 | } else { |
| 461 | assert(rhs->isComplexIntegerType()); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 462 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 463 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 464 | return lhs; |
| 465 | } |
| 466 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 467 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 468 | if (!isCompAssign) { |
| 469 | // int -> float -> _Complex float |
| 470 | if (lhs->isIntegerType()) { |
| 471 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 472 | ImpCastExprToType(lhsExpr, fp, CK_IntegralToFloating); |
| 473 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 474 | } else { |
| 475 | assert(lhs->isComplexIntegerType()); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 476 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 479 | return rhs; |
| 480 | } |
| 481 | |
| 482 | // This handles complex/complex, complex/float, or float/complex. |
| 483 | // When both operands are complex, the shorter operand is converted to the |
| 484 | // type of the longer, and that is the type of the result. This corresponds |
| 485 | // to what is done when combining two real floating-point operands. |
| 486 | // The fun begins when size promotion occur across type domains. |
| 487 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 488 | // floating-point type, the less precise type is converted, within it's |
| 489 | // real or complex domain, to the precision of the other type. For example, |
| 490 | // when combining a "long double" with a "double _Complex", the |
| 491 | // "double _Complex" is promoted to "long double _Complex". |
| 492 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 493 | |
| 494 | // If both are complex, just cast to the more precise type. |
| 495 | if (LHSComplexFloat && RHSComplexFloat) { |
| 496 | if (order > 0) { |
| 497 | // _Complex float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 498 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 499 | return lhs; |
| 500 | |
| 501 | } else if (order < 0) { |
| 502 | // _Complex float -> _Complex double |
| 503 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 504 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 505 | return rhs; |
| 506 | } |
| 507 | return lhs; |
| 508 | } |
| 509 | |
| 510 | // If just the LHS is complex, the RHS needs to be converted, |
| 511 | // and the LHS might need to be promoted. |
| 512 | if (LHSComplexFloat) { |
| 513 | if (order > 0) { // LHS is wider |
| 514 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 515 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 516 | ImpCastExprToType(rhsExpr, fp, CK_FloatingCast); |
| 517 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 518 | return lhs; |
| 519 | } |
| 520 | |
| 521 | // RHS is at least as wide. Find its corresponding complex type. |
| 522 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 523 | |
| 524 | // double -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 525 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 526 | |
| 527 | // _Complex float -> _Complex double |
| 528 | if (!isCompAssign && order < 0) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 529 | ImpCastExprToType(lhsExpr, result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 530 | |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | // Just the RHS is complex, so the LHS needs to be converted |
| 535 | // and the RHS might need to be promoted. |
| 536 | assert(RHSComplexFloat); |
| 537 | |
| 538 | if (order < 0) { // RHS is wider |
| 539 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 540 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 541 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 542 | ImpCastExprToType(lhsExpr, fp, CK_FloatingCast); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 543 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 544 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 545 | return rhs; |
| 546 | } |
| 547 | |
| 548 | // LHS is at least as wide. Find its corresponding complex type. |
| 549 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 550 | |
| 551 | // double -> _Complex double |
| 552 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 553 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 554 | |
| 555 | // _Complex float -> _Complex double |
| 556 | if (order > 0) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 557 | ImpCastExprToType(rhsExpr, result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 558 | |
| 559 | return result; |
| 560 | } |
| 561 | |
| 562 | // Now handle "real" floating types (i.e. float, double, long double). |
| 563 | bool LHSFloat = lhs->isRealFloatingType(); |
| 564 | bool RHSFloat = rhs->isRealFloatingType(); |
| 565 | if (LHSFloat || RHSFloat) { |
| 566 | // If we have two real floating types, convert the smaller operand |
| 567 | // to the bigger result. |
| 568 | if (LHSFloat && RHSFloat) { |
| 569 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 570 | if (order > 0) { |
| 571 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingCast); |
| 572 | return lhs; |
| 573 | } |
| 574 | |
| 575 | assert(order < 0 && "illegal float comparison"); |
| 576 | if (!isCompAssign) |
| 577 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingCast); |
| 578 | return rhs; |
| 579 | } |
| 580 | |
| 581 | // If we have an integer operand, the result is the real floating type. |
| 582 | if (LHSFloat) { |
| 583 | if (rhs->isIntegerType()) { |
| 584 | // Convert rhs to the lhs floating point type. |
| 585 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralToFloating); |
| 586 | return lhs; |
| 587 | } |
| 588 | |
| 589 | // Convert both sides to the appropriate complex float. |
| 590 | assert(rhs->isComplexIntegerType()); |
| 591 | QualType result = Context.getComplexType(lhs); |
| 592 | |
| 593 | // _Complex int -> _Complex float |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 594 | ImpCastExprToType(rhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 595 | |
| 596 | // float -> _Complex float |
| 597 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 598 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 599 | |
| 600 | return result; |
| 601 | } |
| 602 | |
| 603 | assert(RHSFloat); |
| 604 | if (lhs->isIntegerType()) { |
| 605 | // Convert lhs to the rhs floating point type. |
| 606 | if (!isCompAssign) |
| 607 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralToFloating); |
| 608 | return rhs; |
| 609 | } |
| 610 | |
| 611 | // Convert both sides to the appropriate complex float. |
| 612 | assert(lhs->isComplexIntegerType()); |
| 613 | QualType result = Context.getComplexType(rhs); |
| 614 | |
| 615 | // _Complex int -> _Complex float |
| 616 | if (!isCompAssign) |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 617 | ImpCastExprToType(lhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 618 | |
| 619 | // float -> _Complex float |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 620 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 621 | |
| 622 | return result; |
| 623 | } |
| 624 | |
| 625 | // Handle GCC complex int extension. |
| 626 | // FIXME: if the operands are (int, _Complex long), we currently |
| 627 | // don't promote the complex. Also, signedness? |
| 628 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 629 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 630 | if (lhsComplexInt && rhsComplexInt) { |
| 631 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 632 | rhsComplexInt->getElementType()); |
| 633 | assert(order && "inequal types with equal element ordering"); |
| 634 | if (order > 0) { |
| 635 | // _Complex int -> _Complex long |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 636 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 637 | return lhs; |
| 638 | } |
| 639 | |
| 640 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 641 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 642 | return rhs; |
| 643 | } else if (lhsComplexInt) { |
| 644 | // int -> _Complex int |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 645 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 646 | return lhs; |
| 647 | } else if (rhsComplexInt) { |
| 648 | // int -> _Complex int |
| 649 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 650 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 651 | return rhs; |
| 652 | } |
| 653 | |
| 654 | // Finally, we have two differing integer types. |
| 655 | // The rules for this case are in C99 6.3.1.8 |
| 656 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 657 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 658 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 659 | if (lhsSigned == rhsSigned) { |
| 660 | // Same signedness; use the higher-ranked type |
| 661 | if (compare >= 0) { |
| 662 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 663 | return lhs; |
| 664 | } else if (!isCompAssign) |
| 665 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 666 | return rhs; |
| 667 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 668 | // The unsigned type has greater than or equal rank to the |
| 669 | // signed type, so use the unsigned type |
| 670 | if (rhsSigned) { |
| 671 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 672 | return lhs; |
| 673 | } else if (!isCompAssign) |
| 674 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 675 | return rhs; |
| 676 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 677 | // The two types are different widths; if we are here, that |
| 678 | // means the signed type is larger than the unsigned type, so |
| 679 | // use the signed type. |
| 680 | if (lhsSigned) { |
| 681 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 682 | return lhs; |
| 683 | } else if (!isCompAssign) |
| 684 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 685 | return rhs; |
| 686 | } else { |
| 687 | // The signed type is higher-ranked than the unsigned type, |
| 688 | // but isn't actually any bigger (like unsigned int and long |
| 689 | // on most 32-bit systems). Use the unsigned type corresponding |
| 690 | // to the signed type. |
| 691 | QualType result = |
| 692 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 693 | ImpCastExprToType(rhsExpr, result, CK_IntegralCast); |
| 694 | if (!isCompAssign) |
| 695 | ImpCastExprToType(lhsExpr, result, CK_IntegralCast); |
| 696 | return result; |
| 697 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 700 | //===----------------------------------------------------------------------===// |
| 701 | // Semantic Analysis for various Expression Types |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | |
| 704 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 705 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 707 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 708 | /// multiple tokens. However, the common case is that StringToks points to one |
| 709 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 710 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 711 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 712 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | assert(NumStringToks && "Must have at least one string!"); |
| 714 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 715 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 717 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | |
| 719 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 720 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 721 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 722 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 723 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 724 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 725 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 726 | |
| 727 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
Chris Lattner | 7dc480f | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 728 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 729 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 730 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 731 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 732 | // the nul terminator character as well as the string length for pascal |
| 733 | // strings. |
| 734 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 735 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 736 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 739 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 740 | Literal.GetStringLength(), |
| 741 | Literal.AnyWide, StrTy, |
| 742 | &StringTokLocs[0], |
| 743 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 744 | } |
| 745 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 746 | enum CaptureResult { |
| 747 | /// No capture is required. |
| 748 | CR_NoCapture, |
| 749 | |
| 750 | /// A capture is required. |
| 751 | CR_Capture, |
| 752 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 753 | /// A by-ref capture is required. |
| 754 | CR_CaptureByRef, |
| 755 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 756 | /// An error occurred when trying to capture the given variable. |
| 757 | CR_Error |
| 758 | }; |
| 759 | |
| 760 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 761 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 762 | /// \param var - the variable referenced |
| 763 | /// \param DC - the context which we couldn't capture through |
| 764 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 765 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 766 | VarDecl *var, DeclContext *DC) { |
| 767 | switch (S.ExprEvalContexts.back().Context) { |
| 768 | case Sema::Unevaluated: |
| 769 | // The argument will never be evaluated, so don't complain. |
| 770 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 772 | case Sema::PotentiallyEvaluated: |
| 773 | case Sema::PotentiallyEvaluatedIfUsed: |
| 774 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 775 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 776 | case Sema::PotentiallyPotentiallyEvaluated: |
| 777 | // FIXME: delay these! |
| 778 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 781 | // Don't diagnose about capture if we're not actually in code right |
| 782 | // now; in general, there are more appropriate places that will |
| 783 | // diagnose this. |
| 784 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 785 | |
| 786 | // This particular madness can happen in ill-formed default |
| 787 | // arguments; claim it's okay and let downstream code handle it. |
| 788 | if (isa<ParmVarDecl>(var) && |
| 789 | S.CurContext == var->getDeclContext()->getParent()) |
| 790 | return CR_NoCapture; |
| 791 | |
| 792 | DeclarationName functionName; |
| 793 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 794 | functionName = fn->getDeclName(); |
| 795 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 796 | |
| 797 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 798 | << var->getIdentifier() << functionName; |
| 799 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 800 | << var->getIdentifier(); |
| 801 | |
| 802 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | } |
| 804 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 805 | /// There is a well-formed capture at a particular scope level; |
| 806 | /// propagate it through all the nested blocks. |
| 807 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 808 | const BlockDecl::Capture &capture) { |
| 809 | VarDecl *var = capture.getVariable(); |
| 810 | |
| 811 | // Update all the inner blocks with the capture information. |
| 812 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 813 | i != e; ++i) { |
| 814 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 815 | innerBlock->Captures.push_back( |
| 816 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 817 | /*nested*/ true, capture.getCopyExpr())); |
| 818 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 819 | } |
| 820 | |
| 821 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 822 | } |
| 823 | |
| 824 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 825 | /// given value in the current context requires a variable capture. |
| 826 | /// |
| 827 | /// This also keeps the captures set in the BlockScopeInfo records |
| 828 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 829 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 830 | ValueDecl *value) { |
| 831 | // Only variables ever require capture. |
| 832 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 833 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 834 | |
| 835 | // Fast path: variables from the current context never require capture. |
| 836 | DeclContext *DC = S.CurContext; |
| 837 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 838 | |
| 839 | // Only variables with local storage require capture. |
| 840 | // FIXME: What about 'const' variables in C++? |
| 841 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 842 | |
| 843 | // Otherwise, we need to capture. |
| 844 | |
| 845 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 846 | do { |
| 847 | // Only blocks (and eventually C++0x closures) can capture; other |
| 848 | // scopes don't work. |
| 849 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 850 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 851 | |
| 852 | BlockScopeInfo *blockScope = |
| 853 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 854 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 855 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 856 | // Check whether we've already captured it in this block. If so, |
| 857 | // we're done. |
| 858 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 859 | return propagateCapture(S, functionScopesIndex, |
| 860 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 861 | |
| 862 | functionScopesIndex--; |
| 863 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 864 | } while (var->getDeclContext() != DC); |
| 865 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 866 | // Okay, we descended all the way to the block that defines the variable. |
| 867 | // Actually try to capture it. |
| 868 | QualType type = var->getType(); |
| 869 | |
| 870 | // Prohibit variably-modified types. |
| 871 | if (type->isVariablyModifiedType()) { |
| 872 | S.Diag(loc, diag::err_ref_vm_type); |
| 873 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 874 | return CR_Error; |
| 875 | } |
| 876 | |
| 877 | // Prohibit arrays, even in __block variables, but not references to |
| 878 | // them. |
| 879 | if (type->isArrayType()) { |
| 880 | S.Diag(loc, diag::err_ref_array_type); |
| 881 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 882 | return CR_Error; |
| 883 | } |
| 884 | |
| 885 | S.MarkDeclarationReferenced(loc, var); |
| 886 | |
| 887 | // The BlocksAttr indicates the variable is bound by-reference. |
| 888 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 889 | |
| 890 | // Build a copy expression. |
| 891 | Expr *copyExpr = 0; |
| 892 | if (!byRef && S.getLangOptions().CPlusPlus && |
| 893 | !type->isDependentType() && type->isStructureOrClassType()) { |
| 894 | // According to the blocks spec, the capture of a variable from |
| 895 | // the stack requires a const copy constructor. This is not true |
| 896 | // of the copy/move done to move a __block variable to the heap. |
| 897 | type.addConst(); |
| 898 | |
| 899 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 900 | ExprResult result = |
| 901 | S.PerformCopyInitialization( |
| 902 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 903 | type, false), |
| 904 | loc, S.Owned(declRef)); |
| 905 | |
| 906 | // Build a full-expression copy expression if initialization |
| 907 | // succeeded and used a non-trivial constructor. Recover from |
| 908 | // errors by pretending that the copy isn't necessary. |
| 909 | if (!result.isInvalid() && |
| 910 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 911 | result = S.MaybeCreateExprWithCleanups(result); |
| 912 | copyExpr = result.take(); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // We're currently at the declarer; go back to the closure. |
| 917 | functionScopesIndex++; |
| 918 | BlockScopeInfo *blockScope = |
| 919 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 920 | |
| 921 | // Build a valid capture in this scope. |
| 922 | blockScope->Captures.push_back( |
| 923 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 924 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 925 | |
| 926 | // Propagate that to inner captures if necessary. |
| 927 | return propagateCapture(S, functionScopesIndex, |
| 928 | blockScope->Captures.back()); |
| 929 | } |
| 930 | |
| 931 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 932 | const DeclarationNameInfo &NameInfo, |
| 933 | bool byRef) { |
| 934 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 935 | |
| 936 | VarDecl *var = cast<VarDecl>(vd); |
| 937 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 938 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 939 | |
| 940 | QualType exprType = var->getType().getNonReferenceType(); |
| 941 | |
| 942 | BlockDeclRefExpr *BDRE; |
| 943 | if (!byRef) { |
| 944 | // The variable will be bound by copy; make it const within the |
| 945 | // closure, but record that this was done in the expression. |
| 946 | bool constAdded = !exprType.isConstQualified(); |
| 947 | exprType.addConst(); |
| 948 | |
| 949 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 950 | NameInfo.getLoc(), false, |
| 951 | constAdded); |
| 952 | } else { |
| 953 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 954 | NameInfo.getLoc(), true); |
| 955 | } |
| 956 | |
| 957 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 958 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 959 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 960 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 961 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 962 | SourceLocation Loc, |
| 963 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 964 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 965 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 966 | } |
| 967 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 968 | /// BuildDeclRefExpr - Build an expression that references a |
| 969 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 970 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 971 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 972 | const DeclarationNameInfo &NameInfo, |
| 973 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 974 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 976 | Expr *E = DeclRefExpr::Create(Context, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 977 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 978 | SS? SS->getRange() : SourceRange(), |
| 979 | D, NameInfo, Ty, VK); |
| 980 | |
| 981 | // Just in case we're building an illegal pointer-to-member. |
| 982 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 983 | E->setObjectKind(OK_BitField); |
| 984 | |
| 985 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 986 | } |
| 987 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 988 | static ExprResult |
| 989 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 990 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 991 | DeclAccessPair FoundDecl, |
| 992 | const DeclarationNameInfo &MemberNameInfo); |
| 993 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 994 | ExprResult |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 995 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 996 | SourceLocation loc, |
| 997 | IndirectFieldDecl *indirectField, |
| 998 | Expr *baseObjectExpr, |
| 999 | SourceLocation opLoc) { |
| 1000 | // First, build the expression that refers to the base object. |
| 1001 | |
| 1002 | bool baseObjectIsPointer = false; |
| 1003 | Qualifiers baseQuals; |
| 1004 | |
| 1005 | // Case 1: the base of the indirect field is not a field. |
| 1006 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1007 | CXXScopeSpec EmptySS; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1008 | if (baseVariable) { |
| 1009 | assert(baseVariable->getType()->isRecordType()); |
| 1010 | |
| 1011 | // In principle we could have a member access expression that |
| 1012 | // accesses an anonymous struct/union that's a static member of |
| 1013 | // the base object's class. However, under the current standard, |
| 1014 | // static data members cannot be anonymous structs or unions. |
| 1015 | // Supporting this is as easy as building a MemberExpr here. |
| 1016 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1017 | |
| 1018 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1019 | |
| 1020 | ExprResult result = |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1021 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1022 | if (result.isInvalid()) return ExprError(); |
| 1023 | |
| 1024 | baseObjectExpr = result.take(); |
| 1025 | baseObjectIsPointer = false; |
| 1026 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1027 | |
| 1028 | // Case 2: the base of the indirect field is a field and the user |
| 1029 | // wrote a member expression. |
| 1030 | } else if (baseObjectExpr) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1031 | // The caller provided the base object expression. Determine |
| 1032 | // whether its a pointer and whether it adds any qualifiers to the |
| 1033 | // anonymous struct/union fields we're looking into. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1034 | QualType objectType = baseObjectExpr->getType(); |
| 1035 | |
| 1036 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1037 | baseObjectIsPointer = true; |
| 1038 | objectType = ptr->getPointeeType(); |
| 1039 | } else { |
| 1040 | baseObjectIsPointer = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1041 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1042 | baseQuals = objectType.getQualifiers(); |
| 1043 | |
| 1044 | // Case 3: the base of the indirect field is a field and we should |
| 1045 | // build an implicit member access. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1046 | } else { |
| 1047 | // We've found a member of an anonymous struct/union that is |
| 1048 | // inside a non-anonymous struct/union, so in a well-formed |
| 1049 | // program our base object expression is "this". |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1050 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 1051 | if (!method) { |
| 1052 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1053 | << indirectField->getDeclName(); |
| 1054 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1057 | // Our base object expression is "this". |
| 1058 | baseObjectExpr = |
| 1059 | new (Context) CXXThisExpr(loc, method->getThisType(Context), |
| 1060 | /*isImplicit=*/ true); |
| 1061 | baseObjectIsPointer = true; |
| 1062 | baseQuals = Qualifiers::fromCVRMask(method->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | // Build the implicit member references to the field of the |
| 1066 | // anonymous struct/union. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1067 | Expr *result = baseObjectExpr; |
| 1068 | IndirectFieldDecl::chain_iterator |
| 1069 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1070 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1071 | // Build the first member access in the chain with full information. |
| 1072 | if (!baseVariable) { |
| 1073 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1074 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1075 | // FIXME: use the real found-decl info! |
| 1076 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1077 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1078 | // Make a nameInfo that properly uses the anonymous name. |
| 1079 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1080 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1081 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1082 | EmptySS, field, foundDecl, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1083 | memberNameInfo).take(); |
| 1084 | baseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1085 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1086 | // FIXME: check qualified member access |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1089 | // In all cases, we should now skip the first declaration in the chain. |
| 1090 | ++FI; |
| 1091 | |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1092 | while (FI != FEnd) { |
| 1093 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1094 | |
| 1095 | // FIXME: these are somewhat meaningless |
| 1096 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1097 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1098 | |
| 1099 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1100 | (FI == FEnd? SS : EmptySS), field, |
| 1101 | foundDecl, memberNameInfo) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1102 | .take(); |
| 1103 | } |
| 1104 | |
| 1105 | return Owned(result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1108 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1109 | /// possibly a list of template arguments. |
| 1110 | /// |
| 1111 | /// If this produces template arguments, it is permitted to call |
| 1112 | /// DecomposeTemplateName. |
| 1113 | /// |
| 1114 | /// This actually loses a lot of source location information for |
| 1115 | /// non-standard name kinds; we should consider preserving that in |
| 1116 | /// some way. |
| 1117 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1118 | const UnqualifiedId &Id, |
| 1119 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1120 | DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1121 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1122 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1123 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1124 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1125 | |
| 1126 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1127 | Id.TemplateId->getTemplateArgs(), |
| 1128 | Id.TemplateId->NumArgs); |
| 1129 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1130 | TemplateArgsPtr.release(); |
| 1131 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1132 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1133 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1134 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1135 | TemplateArgs = &Buffer; |
| 1136 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1137 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1138 | TemplateArgs = 0; |
| 1139 | } |
| 1140 | } |
| 1141 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1142 | /// Determines if the given class is provably not derived from all of |
| 1143 | /// the prospective base classes. |
| 1144 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1145 | CXXRecordDecl *Record, |
| 1146 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1147 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1148 | return false; |
| 1149 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1150 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1151 | if (!RD) return false; |
| 1152 | Record = cast<CXXRecordDecl>(RD); |
| 1153 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1154 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1155 | E = Record->bases_end(); I != E; ++I) { |
| 1156 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1157 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1158 | if (!BaseRT) return false; |
| 1159 | |
| 1160 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1161 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1162 | return false; |
| 1163 | } |
| 1164 | |
| 1165 | return true; |
| 1166 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1167 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1168 | enum IMAKind { |
| 1169 | /// The reference is definitely not an instance member access. |
| 1170 | IMA_Static, |
| 1171 | |
| 1172 | /// The reference may be an implicit instance member access. |
| 1173 | IMA_Mixed, |
| 1174 | |
| 1175 | /// The reference may be to an instance member, but it is invalid if |
| 1176 | /// so, because the context is not an instance method. |
| 1177 | IMA_Mixed_StaticContext, |
| 1178 | |
| 1179 | /// The reference may be to an instance member, but it is invalid if |
| 1180 | /// so, because the context is from an unrelated class. |
| 1181 | IMA_Mixed_Unrelated, |
| 1182 | |
| 1183 | /// The reference is definitely an implicit instance member access. |
| 1184 | IMA_Instance, |
| 1185 | |
| 1186 | /// The reference may be to an unresolved using declaration. |
| 1187 | IMA_Unresolved, |
| 1188 | |
| 1189 | /// The reference may be to an unresolved using declaration and the |
| 1190 | /// context is not an instance method. |
| 1191 | IMA_Unresolved_StaticContext, |
| 1192 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1193 | /// All possible referrents are instance members and the current |
| 1194 | /// context is not an instance method. |
| 1195 | IMA_Error_StaticContext, |
| 1196 | |
| 1197 | /// All possible referrents are instance members of an unrelated |
| 1198 | /// class. |
| 1199 | IMA_Error_Unrelated |
| 1200 | }; |
| 1201 | |
| 1202 | /// The given lookup names class member(s) and is not being used for |
| 1203 | /// an address-of-member expression. Classify the type of access |
| 1204 | /// according to whether it's possible that this reference names an |
| 1205 | /// instance member. This is best-effort; it is okay to |
| 1206 | /// conservatively answer "yes", in which case some errors will simply |
| 1207 | /// not be caught until template-instantiation. |
| 1208 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1209 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1210 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1211 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1212 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1213 | bool isStaticContext = |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1214 | (!isa<CXXMethodDecl>(DC) || |
| 1215 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1216 | |
| 1217 | if (R.isUnresolvableResult()) |
| 1218 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1219 | |
| 1220 | // Collect all the declaring classes of instance members we find. |
| 1221 | bool hasNonInstance = false; |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1222 | bool hasField = false; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1223 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1224 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1225 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1226 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1227 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1228 | if (dyn_cast<FieldDecl>(D)) |
| 1229 | hasField = true; |
| 1230 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1231 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1232 | Classes.insert(R->getCanonicalDecl()); |
| 1233 | } |
| 1234 | else |
| 1235 | hasNonInstance = true; |
| 1236 | } |
| 1237 | |
| 1238 | // If we didn't find any instance members, it can't be an implicit |
| 1239 | // member reference. |
| 1240 | if (Classes.empty()) |
| 1241 | return IMA_Static; |
| 1242 | |
| 1243 | // If the current context is not an instance method, it can't be |
| 1244 | // an implicit member reference. |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1245 | if (isStaticContext) { |
| 1246 | if (hasNonInstance) |
| 1247 | return IMA_Mixed_StaticContext; |
| 1248 | |
| 1249 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1250 | // C++0x [expr.prim.general]p10: |
| 1251 | // An id-expression that denotes a non-static data member or non-static |
| 1252 | // member function of a class can only be used: |
| 1253 | // (...) |
| 1254 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1255 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1256 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1257 | if (isUnevaluatedExpression) |
| 1258 | return IMA_Mixed_StaticContext; |
| 1259 | } |
| 1260 | |
| 1261 | return IMA_Error_StaticContext; |
| 1262 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1263 | |
| 1264 | // If we can prove that the current context is unrelated to all the |
| 1265 | // declaring classes, it can't be an implicit member reference (in |
| 1266 | // which case it's an error if any of those members are selected). |
| 1267 | if (IsProvablyNotDerivedFrom(SemaRef, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1268 | cast<CXXMethodDecl>(DC)->getParent(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1269 | Classes)) |
| 1270 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1271 | |
| 1272 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1273 | } |
| 1274 | |
| 1275 | /// Diagnose a reference to a field with no object available. |
| 1276 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1277 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1278 | NamedDecl *rep, |
| 1279 | const DeclarationNameInfo &nameInfo) { |
| 1280 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1281 | SourceRange Range(Loc); |
| 1282 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1283 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1284 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1285 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1286 | if (MD->isStatic()) { |
| 1287 | // "invalid use of member 'x' in static member function" |
| 1288 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1289 | << Range << nameInfo.getName(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1295 | << nameInfo.getName() << Range; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1302 | /// Diagnose an empty lookup. |
| 1303 | /// |
| 1304 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1305 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1306 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1307 | DeclarationName Name = R.getLookupName(); |
| 1308 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1309 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1310 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1311 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1312 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1313 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1314 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1315 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1316 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1318 | // If the original lookup was an unqualified lookup, fake an |
| 1319 | // unqualified lookup. This is useful when (for example) the |
| 1320 | // original lookup would not have found something because it was a |
| 1321 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1322 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1323 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1324 | if (isa<CXXRecordDecl>(DC)) { |
| 1325 | LookupQualifiedName(R, DC); |
| 1326 | |
| 1327 | if (!R.empty()) { |
| 1328 | // Don't give errors about ambiguities in this lookup. |
| 1329 | R.suppressDiagnostics(); |
| 1330 | |
| 1331 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1332 | bool isInstance = CurMethod && |
| 1333 | CurMethod->isInstance() && |
| 1334 | DC == CurMethod->getParent(); |
| 1335 | |
| 1336 | // Give a code modification hint to insert 'this->'. |
| 1337 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1338 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1339 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1340 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1341 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1342 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1343 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1344 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1345 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1346 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1347 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1348 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1349 | R.getNameLoc(), DepThisType, false); |
| 1350 | TemplateArgumentListInfo TList; |
| 1351 | if (ULE->hasExplicitTemplateArgs()) |
| 1352 | ULE->copyTemplateArgumentsInto(TList); |
| 1353 | CXXDependentScopeMemberExpr *DepExpr = |
| 1354 | CXXDependentScopeMemberExpr::Create( |
| 1355 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 1356 | ULE->getQualifier(), ULE->getQualifierRange(), NULL, |
| 1357 | R.getLookupNameInfo(), &TList); |
| 1358 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1359 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1360 | // FIXME: we should be able to handle this case too. It is correct |
| 1361 | // to add this-> here. This is a workaround for PR7947. |
| 1362 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1363 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1364 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1365 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1366 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Do we really want to note all of these? |
| 1369 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1370 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1371 | |
| 1372 | // Tell the callee to try to recover. |
| 1373 | return false; |
| 1374 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1375 | |
| 1376 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1380 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1381 | DeclarationName Corrected; |
Daniel Dunbar | dc32cdf | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1382 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1383 | if (!R.empty()) { |
| 1384 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1385 | if (SS.isEmpty()) |
| 1386 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1387 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1388 | R.getLookupName().getAsString()); |
| 1389 | else |
| 1390 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1391 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1392 | << SS.getRange() |
| 1393 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1394 | R.getLookupName().getAsString()); |
| 1395 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1396 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1397 | << ND->getDeclName(); |
| 1398 | |
| 1399 | // Tell the callee to try to recover. |
| 1400 | return false; |
| 1401 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1403 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1404 | // FIXME: If we ended up with a typo for a type name or |
| 1405 | // Objective-C class name, we're in trouble because the parser |
| 1406 | // is in the wrong place to recover. Suggest the typo |
| 1407 | // correction, but don't make it a fix-it since we're not going |
| 1408 | // to recover well anyway. |
| 1409 | if (SS.isEmpty()) |
| 1410 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1411 | else |
| 1412 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1413 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1414 | << SS.getRange(); |
| 1415 | |
| 1416 | // Don't try to recover; it won't work. |
| 1417 | return true; |
| 1418 | } |
| 1419 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1420 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1421 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1422 | if (SS.isEmpty()) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1423 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1424 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1425 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1426 | << Name << computeDeclContext(SS, false) << Corrected |
| 1427 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1428 | return true; |
| 1429 | } |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1430 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // Emit a special diagnostic for failed member lookups. |
| 1434 | // FIXME: computing the declaration context might fail here (?) |
| 1435 | if (!SS.isEmpty()) { |
| 1436 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1437 | << Name << computeDeclContext(SS, false) |
| 1438 | << SS.getRange(); |
| 1439 | return true; |
| 1440 | } |
| 1441 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1442 | // Give up, we can't recover. |
| 1443 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1447 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1448 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1449 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1450 | if (!IDecl) |
| 1451 | return 0; |
| 1452 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1453 | if (!ClassImpDecl) |
| 1454 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1455 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1456 | if (!property) |
| 1457 | return 0; |
| 1458 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1459 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1460 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1461 | return 0; |
| 1462 | return property; |
| 1463 | } |
| 1464 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1465 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1466 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1467 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1468 | if (!IDecl) |
| 1469 | return false; |
| 1470 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1471 | if (!ClassImpDecl) |
| 1472 | return false; |
| 1473 | if (ObjCPropertyImplDecl *PIDecl |
| 1474 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1475 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1476 | PIDecl->getPropertyIvarDecl()) |
| 1477 | return false; |
| 1478 | |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1482 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1483 | LookupResult &Lookup, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1484 | IdentifierInfo *II, |
| 1485 | SourceLocation NameLoc) { |
| 1486 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1487 | bool LookForIvars; |
| 1488 | if (Lookup.empty()) |
| 1489 | LookForIvars = true; |
| 1490 | else if (CurMeth->isClassMethod()) |
| 1491 | LookForIvars = false; |
| 1492 | else |
| 1493 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1494 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1495 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1496 | if (!LookForIvars) |
| 1497 | return 0; |
| 1498 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1499 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1500 | if (!IDecl) |
| 1501 | return 0; |
| 1502 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1503 | if (!ClassImpDecl) |
| 1504 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1505 | bool DynamicImplSeen = false; |
| 1506 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1507 | if (!property) |
| 1508 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1509 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1510 | DynamicImplSeen = |
| 1511 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1512 | // property implementation has a designated ivar. No need to assume a new |
| 1513 | // one. |
| 1514 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1515 | return 0; |
| 1516 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1517 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1518 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1519 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1520 | NameLoc, |
| 1521 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1522 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1523 | (Expr *)0, true); |
| 1524 | ClassImpDecl->addDecl(Ivar); |
| 1525 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1526 | property->setPropertyIvarDecl(Ivar); |
| 1527 | return Ivar; |
| 1528 | } |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1532 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1533 | CXXScopeSpec &SS, |
| 1534 | UnqualifiedId &Id, |
| 1535 | bool HasTrailingLParen, |
| 1536 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1537 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1538 | "cannot be direct & operand and have a trailing lparen"); |
| 1539 | |
| 1540 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1541 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1542 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1543 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1546 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1547 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1548 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1549 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1550 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1551 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1552 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1553 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1554 | // C++ [temp.dep.expr]p3: |
| 1555 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1556 | // -- an identifier that was declared with a dependent type, |
| 1557 | // (note: handled after lookup) |
| 1558 | // -- a template-id that is dependent, |
| 1559 | // (note: handled in BuildTemplateIdExpr) |
| 1560 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1561 | // -- a nested-name-specifier that contains a class-name that |
| 1562 | // names a dependent type. |
| 1563 | // Determine whether this is a member of an unknown specialization; |
| 1564 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1565 | bool DependentID = false; |
| 1566 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1567 | Name.getCXXNameType()->isDependentType()) { |
| 1568 | DependentID = true; |
| 1569 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1570 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1571 | if (RequireCompleteDeclContext(SS, DC)) |
| 1572 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1573 | } else { |
| 1574 | DependentID = true; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1578 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1579 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1580 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1581 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1582 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1583 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1584 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1585 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1586 | // Lookup the template name again to correctly establish the context in |
| 1587 | // which it was found. This is really unfortunate as we already did the |
| 1588 | // lookup to determine that it was a template name in the first place. If |
| 1589 | // this becomes a performance hit, we can work harder to preserve those |
| 1590 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1591 | bool MemberOfUnknownSpecialization; |
| 1592 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1593 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (MemberOfUnknownSpecialization || |
| 1596 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1597 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1598 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1599 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1600 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1601 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1603 | // If the result might be in a dependent base class, this is a dependent |
| 1604 | // id-expression. |
| 1605 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1606 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1607 | TemplateArgs); |
| 1608 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1609 | // If this reference is in an Objective-C method, then we need to do |
| 1610 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1611 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1612 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1613 | if (E.isInvalid()) |
| 1614 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1616 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1617 | return Owned(Ex); |
| 1618 | |
| 1619 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1620 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1621 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1622 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) { |
| 1623 | if (const ObjCPropertyDecl *Property = |
| 1624 | canSynthesizeProvisionalIvar(II)) { |
| 1625 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1626 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1627 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1628 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1629 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1630 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1631 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1632 | // for further use, this must be set to false if in class method. |
| 1633 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1634 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1635 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1636 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1637 | if (R.isAmbiguous()) |
| 1638 | return ExprError(); |
| 1639 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1640 | // Determine whether this name might be a candidate for |
| 1641 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1642 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1643 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1644 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1646 | // in C90, extension in C99, forbidden in C++). |
| 1647 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1648 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1649 | if (D) R.addDecl(D); |
| 1650 | } |
| 1651 | |
| 1652 | // If this name wasn't predeclared and if this is not a function |
| 1653 | // call, diagnose the problem. |
| 1654 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1655 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1656 | return ExprError(); |
| 1657 | |
| 1658 | assert(!R.empty() && |
| 1659 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1660 | |
| 1661 | // If we found an Objective-C instance variable, let |
| 1662 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1663 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1664 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1665 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1666 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1667 | assert(E.isInvalid() || E.get()); |
| 1668 | return move(E); |
| 1669 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1673 | // This is guaranteed from this point on. |
| 1674 | assert(!R.empty() || ADL); |
| 1675 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1676 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1677 | // C++ [class.mfct.non-static]p3: |
| 1678 | // When an id-expression that is not part of a class member access |
| 1679 | // syntax and not used to form a pointer to member is used in the |
| 1680 | // body of a non-static member function of class X, if name lookup |
| 1681 | // resolves the name in the id-expression to a non-static non-type |
| 1682 | // member of some class C, the id-expression is transformed into a |
| 1683 | // class member access expression using (*this) as the |
| 1684 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1685 | // |
| 1686 | // But we don't actually need to do this for '&' operands if R |
| 1687 | // resolved to a function or overloaded function set, because the |
| 1688 | // expression is ill-formed if it actually works out to be a |
| 1689 | // non-static member function: |
| 1690 | // |
| 1691 | // C++ [expr.ref]p4: |
| 1692 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1693 | // [t]he expression can be used only as the left-hand operand of a |
| 1694 | // member function call. |
| 1695 | // |
| 1696 | // There are other safeguards against such uses, but it's important |
| 1697 | // to get this right here so that we don't end up making a |
| 1698 | // spuriously dependent expression if we're inside a dependent |
| 1699 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1700 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1701 | bool MightBeImplicitMember; |
| 1702 | if (!isAddressOfOperand) |
| 1703 | MightBeImplicitMember = true; |
| 1704 | else if (!SS.isEmpty()) |
| 1705 | MightBeImplicitMember = false; |
| 1706 | else if (R.isOverloadedResult()) |
| 1707 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1708 | else if (R.isUnresolvableResult()) |
| 1709 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1710 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1711 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1712 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1713 | |
| 1714 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1715 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1718 | if (TemplateArgs) |
| 1719 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1720 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1721 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1722 | } |
| 1723 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1724 | /// Builds an expression which might be an implicit member expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1725 | ExprResult |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1726 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1727 | LookupResult &R, |
| 1728 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1729 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1730 | case IMA_Instance: |
| 1731 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1732 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1733 | case IMA_Mixed: |
| 1734 | case IMA_Mixed_Unrelated: |
| 1735 | case IMA_Unresolved: |
| 1736 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1737 | |
| 1738 | case IMA_Static: |
| 1739 | case IMA_Mixed_StaticContext: |
| 1740 | case IMA_Unresolved_StaticContext: |
| 1741 | if (TemplateArgs) |
| 1742 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1743 | return BuildDeclarationNameExpr(SS, R, false); |
| 1744 | |
| 1745 | case IMA_Error_StaticContext: |
| 1746 | case IMA_Error_Unrelated: |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1747 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 1748 | R.getLookupNameInfo()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1749 | return ExprError(); |
| 1750 | } |
| 1751 | |
| 1752 | llvm_unreachable("unexpected instance member access kind"); |
| 1753 | return ExprError(); |
| 1754 | } |
| 1755 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1756 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1757 | /// declaration name, generally during template instantiation. |
| 1758 | /// There's a large number of things which don't need to be done along |
| 1759 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1760 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1761 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1762 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1763 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1764 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1765 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1766 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1767 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1768 | return ExprError(); |
| 1769 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1770 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1771 | LookupQualifiedName(R, DC); |
| 1772 | |
| 1773 | if (R.isAmbiguous()) |
| 1774 | return ExprError(); |
| 1775 | |
| 1776 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1777 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1778 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1779 | return ExprError(); |
| 1780 | } |
| 1781 | |
| 1782 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1783 | } |
| 1784 | |
| 1785 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1786 | /// detected that we're currently inside an ObjC method. Perform some |
| 1787 | /// additional lookup. |
| 1788 | /// |
| 1789 | /// Ideally, most of this would be done by lookup, but there's |
| 1790 | /// actually quite a lot of extra work involved. |
| 1791 | /// |
| 1792 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1793 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1794 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1795 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1796 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1797 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1798 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1799 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1800 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1801 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1802 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1803 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1804 | |
| 1805 | // If we're in a class method, we don't normally want to look for |
| 1806 | // ivars. But if we don't find anything else, and there's an |
| 1807 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1808 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1809 | |
| 1810 | bool LookForIvars; |
| 1811 | if (Lookup.empty()) |
| 1812 | LookForIvars = true; |
| 1813 | else if (IsClassMethod) |
| 1814 | LookForIvars = false; |
| 1815 | else |
| 1816 | LookForIvars = (Lookup.isSingleResult() && |
| 1817 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1818 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1819 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1820 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1821 | ObjCInterfaceDecl *ClassDeclared; |
| 1822 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1823 | // Diagnose using an ivar in a class method. |
| 1824 | if (IsClassMethod) |
| 1825 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1826 | << IV->getDeclName()); |
| 1827 | |
| 1828 | // If we're referencing an invalid decl, just return this as a silent |
| 1829 | // error node. The error diagnostic was already emitted on the decl. |
| 1830 | if (IV->isInvalidDecl()) |
| 1831 | return ExprError(); |
| 1832 | |
| 1833 | // Check if referencing a field with __attribute__((deprecated)). |
| 1834 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1835 | return ExprError(); |
| 1836 | |
| 1837 | // Diagnose the use of an ivar outside of the declaring class. |
| 1838 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1839 | ClassDeclared != IFace) |
| 1840 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1841 | |
| 1842 | // FIXME: This should use a new expr for a direct reference, don't |
| 1843 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1844 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1845 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1846 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1847 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1848 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1849 | SelfName, false, false); |
| 1850 | if (SelfExpr.isInvalid()) |
| 1851 | return ExprError(); |
| 1852 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1853 | Expr *SelfE = SelfExpr.take(); |
| 1854 | DefaultLvalueConversion(SelfE); |
| 1855 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1856 | MarkDeclarationReferenced(Loc, IV); |
| 1857 | return Owned(new (Context) |
| 1858 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1859 | SelfE, true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1860 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1861 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1862 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1863 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1864 | ObjCInterfaceDecl *ClassDeclared; |
| 1865 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1866 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1867 | IFace == ClassDeclared) |
| 1868 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1869 | } |
| 1870 | } |
| 1871 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1872 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1873 | // FIXME. Consolidate this with similar code in LookupName. |
| 1874 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1875 | if (!(getLangOptions().CPlusPlus && |
| 1876 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1877 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1878 | S, Lookup.isForRedeclaration(), |
| 1879 | Lookup.getNameLoc()); |
| 1880 | if (D) Lookup.addDecl(D); |
| 1881 | } |
| 1882 | } |
| 1883 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1884 | // Sentinel value saying that we didn't do anything special. |
| 1885 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1886 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1887 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1888 | /// \brief Cast a base object to a member's actual type. |
| 1889 | /// |
| 1890 | /// Logically this happens in three phases: |
| 1891 | /// |
| 1892 | /// * First we cast from the base type to the naming class. |
| 1893 | /// The naming class is the class into which we were looking |
| 1894 | /// when we found the member; it's the qualifier type if a |
| 1895 | /// qualifier was provided, and otherwise it's the base type. |
| 1896 | /// |
| 1897 | /// * Next we cast from the naming class to the declaring class. |
| 1898 | /// If the member we found was brought into a class's scope by |
| 1899 | /// a using declaration, this is that class; otherwise it's |
| 1900 | /// the class declaring the member. |
| 1901 | /// |
| 1902 | /// * Finally we cast from the declaring class to the "true" |
| 1903 | /// declaring class of the member. This conversion does not |
| 1904 | /// obey access control. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1905 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1906 | Sema::PerformObjectMemberConversion(Expr *&From, |
| 1907 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1908 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1909 | NamedDecl *Member) { |
| 1910 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1911 | if (!RD) |
| 1912 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1913 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1914 | QualType DestRecordType; |
| 1915 | QualType DestType; |
| 1916 | QualType FromRecordType; |
| 1917 | QualType FromType = From->getType(); |
| 1918 | bool PointerConversions = false; |
| 1919 | if (isa<FieldDecl>(Member)) { |
| 1920 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1922 | if (FromType->getAs<PointerType>()) { |
| 1923 | DestType = Context.getPointerType(DestRecordType); |
| 1924 | FromRecordType = FromType->getPointeeType(); |
| 1925 | PointerConversions = true; |
| 1926 | } else { |
| 1927 | DestType = DestRecordType; |
| 1928 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1929 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1930 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1931 | if (Method->isStatic()) |
| 1932 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1934 | DestType = Method->getThisType(Context); |
| 1935 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1937 | if (FromType->getAs<PointerType>()) { |
| 1938 | FromRecordType = FromType->getPointeeType(); |
| 1939 | PointerConversions = true; |
| 1940 | } else { |
| 1941 | FromRecordType = FromType; |
| 1942 | DestType = DestRecordType; |
| 1943 | } |
| 1944 | } else { |
| 1945 | // No conversion necessary. |
| 1946 | return false; |
| 1947 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1949 | if (DestType->isDependentType() || FromType->isDependentType()) |
| 1950 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1952 | // If the unqualified types are the same, no conversion is necessary. |
| 1953 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1954 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1955 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1956 | SourceRange FromRange = From->getSourceRange(); |
| 1957 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1958 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1959 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1961 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1962 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1963 | // class name. |
| 1964 | // |
| 1965 | // If the member was a qualified name and the qualified referred to a |
| 1966 | // specific base subobject type, we'll cast to that intermediate type |
| 1967 | // first and then to the object in which the member is declared. That allows |
| 1968 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1969 | // |
| 1970 | // class Base { public: int x; }; |
| 1971 | // class Derived1 : public Base { }; |
| 1972 | // class Derived2 : public Base { }; |
| 1973 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1974 | // |
| 1975 | // void VeryDerived::f() { |
| 1976 | // x = 17; // error: ambiguous base subobjects |
| 1977 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1978 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1979 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1980 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1981 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1982 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1983 | |
| 1984 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1985 | |
| 1986 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1987 | // type of the object type, in which case we just ignore it. |
| 1988 | // Otherwise build the appropriate casts. |
| 1989 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1990 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1991 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1992 | FromLoc, FromRange, &BasePath)) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1993 | return true; |
| 1994 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1995 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1996 | QType = Context.getPointerType(QType); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1997 | ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 1998 | VK, &BasePath); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1999 | |
| 2000 | FromType = QType; |
| 2001 | FromRecordType = QRecordType; |
| 2002 | |
| 2003 | // If the qualifier type was the same as the destination type, |
| 2004 | // we're done. |
| 2005 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 2006 | return false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2009 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2010 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2011 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2012 | // If we actually found the member through a using declaration, cast |
| 2013 | // down to the using declaration's type. |
| 2014 | // |
| 2015 | // Pointer equality is fine here because only one declaration of a |
| 2016 | // class ever has member declarations. |
| 2017 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2018 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2019 | QualType URecordType = Context.getTypeDeclType( |
| 2020 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2021 | |
| 2022 | // We only need to do this if the naming-class to declaring-class |
| 2023 | // conversion is non-trivial. |
| 2024 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2025 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2026 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2027 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2028 | FromLoc, FromRange, &BasePath)) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2029 | return true; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2030 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2031 | QualType UType = URecordType; |
| 2032 | if (PointerConversions) |
| 2033 | UType = Context.getPointerType(UType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2034 | ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2035 | VK, &BasePath); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2036 | FromType = UType; |
| 2037 | FromRecordType = URecordType; |
| 2038 | } |
| 2039 | |
| 2040 | // We don't do access control for the conversion from the |
| 2041 | // declaring class to the true declaring class. |
| 2042 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2043 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2044 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2045 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2046 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2047 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2048 | IgnoreAccess)) |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2049 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2050 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2051 | ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2052 | VK, &BasePath); |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2053 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2054 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2056 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2058 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2059 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2060 | const DeclarationNameInfo &MemberNameInfo, |
| 2061 | QualType Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2062 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2063 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 2064 | NestedNameSpecifier *Qualifier = 0; |
| 2065 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2066 | if (SS.isSet()) { |
| 2067 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 2068 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2071 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2072 | Member, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2073 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2076 | static ExprResult |
| 2077 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2078 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2079 | DeclAccessPair FoundDecl, |
| 2080 | const DeclarationNameInfo &MemberNameInfo) { |
| 2081 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2082 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2083 | // that *x is always an l-value), except that if the base isn't |
| 2084 | // an ordinary object then we must have an rvalue. |
| 2085 | ExprValueKind VK = VK_LValue; |
| 2086 | ExprObjectKind OK = OK_Ordinary; |
| 2087 | if (!IsArrow) { |
| 2088 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2089 | VK = BaseExpr->getValueKind(); |
| 2090 | else |
| 2091 | VK = VK_RValue; |
| 2092 | } |
| 2093 | if (VK != VK_RValue && Field->isBitField()) |
| 2094 | OK = OK_BitField; |
| 2095 | |
| 2096 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2097 | QualType MemberType = Field->getType(); |
| 2098 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2099 | MemberType = Ref->getPointeeType(); |
| 2100 | VK = VK_LValue; |
| 2101 | } else { |
| 2102 | QualType BaseType = BaseExpr->getType(); |
| 2103 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2104 | |
| 2105 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2106 | |
| 2107 | // GC attributes are never picked up by members. |
| 2108 | BaseQuals.removeObjCGCAttr(); |
| 2109 | |
| 2110 | // CVR attributes from the base are picked up by members, |
| 2111 | // except that 'mutable' members don't pick up 'const'. |
| 2112 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2113 | |
| 2114 | Qualifiers MemberQuals |
| 2115 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2116 | |
| 2117 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2118 | assert(!MemberQuals.hasAddressSpace()); |
| 2119 | |
| 2120 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2121 | if (Combined != MemberQuals) |
| 2122 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2123 | } |
| 2124 | |
| 2125 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
| 2126 | if (S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2127 | FoundDecl, Field)) |
| 2128 | return ExprError(); |
| 2129 | return S.Owned(BuildMemberExpr(S.Context, BaseExpr, IsArrow, SS, |
| 2130 | Field, FoundDecl, MemberNameInfo, |
| 2131 | MemberType, VK, OK)); |
| 2132 | } |
| 2133 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2134 | /// Builds an implicit member access expression. The current context |
| 2135 | /// is known to be an instance method, and the given unqualified lookup |
| 2136 | /// set is known to contain only instance members, at least one of which |
| 2137 | /// is from an appropriate type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2138 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2139 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2140 | LookupResult &R, |
| 2141 | const TemplateArgumentListInfo *TemplateArgs, |
| 2142 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2143 | assert(!R.empty() && !R.isAmbiguous()); |
| 2144 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2145 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2146 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2147 | // We may have found a field within an anonymous union or struct |
| 2148 | // (C++ [class.union]). |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2149 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2150 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2151 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2152 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2153 | // If this is known to be an instance access, go ahead and build an |
| 2154 | // implicit 'this' expression now. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2155 | // 'this' expression now. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2156 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 2157 | assert(method && "didn't correctly pre-flight capture of 'this'"); |
| 2158 | |
| 2159 | QualType thisType = method->getThisType(Context); |
| 2160 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2161 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2162 | SourceLocation Loc = R.getNameLoc(); |
| 2163 | if (SS.getRange().isValid()) |
| 2164 | Loc = SS.getRange().getBegin(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2165 | baseExpr = new (Context) CXXThisExpr(loc, thisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2168 | return BuildMemberReferenceExpr(baseExpr, thisType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2169 | /*OpLoc*/ SourceLocation(), |
| 2170 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2171 | SS, |
| 2172 | /*FirstQualifierInScope*/ 0, |
| 2173 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2176 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2177 | const LookupResult &R, |
| 2178 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2179 | // Only when used directly as the postfix-expression of a call. |
| 2180 | if (!HasTrailingLParen) |
| 2181 | return false; |
| 2182 | |
| 2183 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2184 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2185 | return false; |
| 2186 | |
| 2187 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2188 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2189 | return false; |
| 2190 | |
| 2191 | // Turn off ADL when we find certain kinds of declarations during |
| 2192 | // normal lookup: |
| 2193 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2194 | NamedDecl *D = *I; |
| 2195 | |
| 2196 | // C++0x [basic.lookup.argdep]p3: |
| 2197 | // -- a declaration of a class member |
| 2198 | // Since using decls preserve this property, we check this on the |
| 2199 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2200 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2201 | return false; |
| 2202 | |
| 2203 | // C++0x [basic.lookup.argdep]p3: |
| 2204 | // -- a block-scope function declaration that is not a |
| 2205 | // using-declaration |
| 2206 | // NOTE: we also trigger this for function templates (in fact, we |
| 2207 | // don't check the decl type at all, since all other decl types |
| 2208 | // turn off ADL anyway). |
| 2209 | if (isa<UsingShadowDecl>(D)) |
| 2210 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2211 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2212 | return false; |
| 2213 | |
| 2214 | // C++0x [basic.lookup.argdep]p3: |
| 2215 | // -- a declaration that is neither a function or a function |
| 2216 | // template |
| 2217 | // And also for builtin functions. |
| 2218 | if (isa<FunctionDecl>(D)) { |
| 2219 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2220 | |
| 2221 | // But also builtin functions. |
| 2222 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2223 | return false; |
| 2224 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2225 | return false; |
| 2226 | } |
| 2227 | |
| 2228 | return true; |
| 2229 | } |
| 2230 | |
| 2231 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2232 | /// Diagnoses obvious problems with the use of the given declaration |
| 2233 | /// as an expression. This is only actually called for lookups that |
| 2234 | /// were not overloaded, and it doesn't promise that the declaration |
| 2235 | /// will in fact be used. |
| 2236 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 2237 | if (isa<TypedefDecl>(D)) { |
| 2238 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2239 | return true; |
| 2240 | } |
| 2241 | |
| 2242 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2243 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2244 | return true; |
| 2245 | } |
| 2246 | |
| 2247 | if (isa<NamespaceDecl>(D)) { |
| 2248 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2249 | return true; |
| 2250 | } |
| 2251 | |
| 2252 | return false; |
| 2253 | } |
| 2254 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2255 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2256 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2257 | LookupResult &R, |
| 2258 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2259 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2260 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2261 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2262 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2263 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2264 | |
| 2265 | // We only need to check the declaration if there's exactly one |
| 2266 | // result, because in the overloaded case the results can only be |
| 2267 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2268 | if (R.isSingleResult() && |
| 2269 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2270 | return ExprError(); |
| 2271 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2272 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2273 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2274 | // we've picked a target. |
| 2275 | R.suppressDiagnostics(); |
| 2276 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2277 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2278 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2279 | (NestedNameSpecifier*) SS.getScopeRep(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2280 | SS.getRange(), R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2281 | NeedsADL, R.isOverloadedResult(), |
| 2282 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2283 | |
| 2284 | return Owned(ULE); |
| 2285 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2286 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2287 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2288 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2289 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2290 | const DeclarationNameInfo &NameInfo, |
| 2291 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2292 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2293 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2294 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2295 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2296 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2297 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2298 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2299 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2300 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2301 | // Specifically diagnose references to class templates that are missing |
| 2302 | // a template argument list. |
| 2303 | Diag(Loc, diag::err_template_decl_ref) |
| 2304 | << Template << SS.getRange(); |
| 2305 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2306 | return ExprError(); |
| 2307 | } |
| 2308 | |
| 2309 | // Make sure that we're referring to a value. |
| 2310 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2311 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2312 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2313 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2314 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2315 | return ExprError(); |
| 2316 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2317 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2318 | // Check whether this declaration can be used. Note that we suppress |
| 2319 | // this check when we're going to perform argument-dependent lookup |
| 2320 | // on this function name, because this might not be the function |
| 2321 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2322 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2323 | return ExprError(); |
| 2324 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2325 | // Only create DeclRefExpr's for valid Decl's. |
| 2326 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2327 | return ExprError(); |
| 2328 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2329 | // Handle members of anonymous structs and unions. If we got here, |
| 2330 | // and the reference is to a class member indirect field, then this |
| 2331 | // must be the subject of a pointer-to-member expression. |
| 2332 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2333 | if (!indirectField->isCXXClassMember()) |
| 2334 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2335 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2336 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2337 | // If the identifier reference is inside a block, and it refers to a value |
| 2338 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2339 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2340 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2341 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2342 | // We do not do this for things like enum constants, global variables, etc, |
| 2343 | // as they do not get snapshotted. |
| 2344 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2345 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2346 | case CR_Error: |
| 2347 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2348 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2349 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2350 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2351 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2352 | |
| 2353 | case CR_CaptureByRef: |
| 2354 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2355 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2356 | |
| 2357 | case CR_NoCapture: { |
| 2358 | // If this reference is not in a block or if the referenced |
| 2359 | // variable is within the block, create a normal DeclRefExpr. |
| 2360 | |
| 2361 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2362 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2363 | |
| 2364 | switch (D->getKind()) { |
| 2365 | // Ignore all the non-ValueDecl kinds. |
| 2366 | #define ABSTRACT_DECL(kind) |
| 2367 | #define VALUE(type, base) |
| 2368 | #define DECL(type, base) \ |
| 2369 | case Decl::type: |
| 2370 | #include "clang/AST/DeclNodes.inc" |
| 2371 | llvm_unreachable("invalid value decl kind"); |
| 2372 | return ExprError(); |
| 2373 | |
| 2374 | // These shouldn't make it here. |
| 2375 | case Decl::ObjCAtDefsField: |
| 2376 | case Decl::ObjCIvar: |
| 2377 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2378 | return ExprError(); |
| 2379 | |
| 2380 | // Enum constants are always r-values and never references. |
| 2381 | // Unresolved using declarations are dependent. |
| 2382 | case Decl::EnumConstant: |
| 2383 | case Decl::UnresolvedUsingValue: |
| 2384 | valueKind = VK_RValue; |
| 2385 | break; |
| 2386 | |
| 2387 | // Fields and indirect fields that got here must be for |
| 2388 | // pointer-to-member expressions; we just call them l-values for |
| 2389 | // internal consistency, because this subexpression doesn't really |
| 2390 | // exist in the high-level semantics. |
| 2391 | case Decl::Field: |
| 2392 | case Decl::IndirectField: |
| 2393 | assert(getLangOptions().CPlusPlus && |
| 2394 | "building reference to field in C?"); |
| 2395 | |
| 2396 | // These can't have reference type in well-formed programs, but |
| 2397 | // for internal consistency we do this anyway. |
| 2398 | type = type.getNonReferenceType(); |
| 2399 | valueKind = VK_LValue; |
| 2400 | break; |
| 2401 | |
| 2402 | // Non-type template parameters are either l-values or r-values |
| 2403 | // depending on the type. |
| 2404 | case Decl::NonTypeTemplateParm: { |
| 2405 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2406 | type = reftype->getPointeeType(); |
| 2407 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2408 | break; |
| 2409 | } |
| 2410 | |
| 2411 | // For non-references, we need to strip qualifiers just in case |
| 2412 | // the template parameter was declared as 'const int' or whatever. |
| 2413 | valueKind = VK_RValue; |
| 2414 | type = type.getUnqualifiedType(); |
| 2415 | break; |
| 2416 | } |
| 2417 | |
| 2418 | case Decl::Var: |
| 2419 | // In C, "extern void blah;" is valid and is an r-value. |
| 2420 | if (!getLangOptions().CPlusPlus && |
| 2421 | !type.hasQualifiers() && |
| 2422 | type->isVoidType()) { |
| 2423 | valueKind = VK_RValue; |
| 2424 | break; |
| 2425 | } |
| 2426 | // fallthrough |
| 2427 | |
| 2428 | case Decl::ImplicitParam: |
| 2429 | case Decl::ParmVar: |
| 2430 | // These are always l-values. |
| 2431 | valueKind = VK_LValue; |
| 2432 | type = type.getNonReferenceType(); |
| 2433 | break; |
| 2434 | |
| 2435 | case Decl::Function: { |
| 2436 | // Functions are l-values in C++. |
| 2437 | if (getLangOptions().CPlusPlus) { |
| 2438 | valueKind = VK_LValue; |
| 2439 | break; |
| 2440 | } |
| 2441 | |
| 2442 | // C99 DR 316 says that, if a function type comes from a |
| 2443 | // function definition (without a prototype), that type is only |
| 2444 | // used for checking compatibility. Therefore, when referencing |
| 2445 | // the function, we pretend that we don't have the full function |
| 2446 | // type. |
| 2447 | if (!cast<FunctionDecl>(VD)->hasPrototype()) |
| 2448 | if (const FunctionProtoType *proto = type->getAs<FunctionProtoType>()) |
| 2449 | type = Context.getFunctionNoProtoType(proto->getResultType(), |
| 2450 | proto->getExtInfo()); |
| 2451 | |
| 2452 | // Functions are r-values in C. |
| 2453 | valueKind = VK_RValue; |
| 2454 | break; |
| 2455 | } |
| 2456 | |
| 2457 | case Decl::CXXMethod: |
| 2458 | // C++ methods are l-values if static, r-values if non-static. |
| 2459 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2460 | valueKind = VK_LValue; |
| 2461 | break; |
| 2462 | } |
| 2463 | // fallthrough |
| 2464 | |
| 2465 | case Decl::CXXConversion: |
| 2466 | case Decl::CXXDestructor: |
| 2467 | case Decl::CXXConstructor: |
| 2468 | valueKind = VK_RValue; |
| 2469 | break; |
| 2470 | } |
| 2471 | |
| 2472 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2473 | } |
| 2474 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2475 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2476 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2477 | llvm_unreachable("unknown capture result"); |
| 2478 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2481 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2482 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2483 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2485 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2486 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2487 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2488 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2489 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2490 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2492 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2493 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2495 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2496 | if (!currentDecl && getCurBlock()) |
| 2497 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2498 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2499 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2500 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2501 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2502 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2503 | QualType ResTy; |
| 2504 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2505 | ResTy = Context.DependentTy; |
| 2506 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2507 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2508 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2509 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2510 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2511 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2512 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2513 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2516 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2517 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2518 | bool Invalid = false; |
| 2519 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2520 | if (Invalid) |
| 2521 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2522 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2523 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2524 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2525 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2526 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2528 | QualType Ty; |
| 2529 | if (!getLangOptions().CPlusPlus) |
| 2530 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2531 | else if (Literal.isWide()) |
| 2532 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2533 | else if (Literal.isMultiChar()) |
| 2534 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2535 | else |
| 2536 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2537 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2538 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2539 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2540 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2543 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2544 | // 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] | 2545 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2546 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2547 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2548 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2549 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2550 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2551 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2552 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2553 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2554 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2555 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2556 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2558 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2559 | bool Invalid = false; |
| 2560 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2561 | if (Invalid) |
| 2562 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2563 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | Tok.getLocation(), PP); |
| 2566 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2567 | return ExprError(); |
| 2568 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2569 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2570 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2571 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2572 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2573 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2574 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2575 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2576 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2577 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2578 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2579 | |
| 2580 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2581 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2582 | using llvm::APFloat; |
| 2583 | APFloat Val(Format); |
| 2584 | |
| 2585 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2586 | |
| 2587 | // Overflow is always an error, but underflow is only an error if |
| 2588 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2589 | if ((result & APFloat::opOverflow) || |
| 2590 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2591 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2592 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2593 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2594 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2595 | APFloat::getLargest(Format).toString(buffer); |
| 2596 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2597 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2598 | APFloat::getSmallest(Format).toString(buffer); |
| 2599 | } |
| 2600 | |
| 2601 | Diag(Tok.getLocation(), diagnostic) |
| 2602 | << Ty |
| 2603 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2604 | } |
| 2605 | |
| 2606 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2607 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2608 | |
Peter Collingbourne | 0982136 | 2010-12-04 01:50:56 +0000 | [diff] [blame] | 2609 | if (getLangOptions().SinglePrecisionConstants && Ty == Context.DoubleTy) |
| 2610 | ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); |
| 2611 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2612 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2613 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2614 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2615 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2616 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2617 | // long long is a C99 feature. |
| 2618 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2619 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2620 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2621 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2622 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2623 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2624 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2625 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2626 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2627 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2628 | Ty = Context.UnsignedLongLongTy; |
| 2629 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2630 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | } else { |
| 2632 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2633 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2634 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2635 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2636 | // be an unsigned int. |
| 2637 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2638 | |
| 2639 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2640 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2641 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2642 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2643 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2644 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2645 | // Does it fit in a unsigned int? |
| 2646 | if (ResultVal.isIntN(IntSize)) { |
| 2647 | // Does it fit in a signed int? |
| 2648 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2649 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2650 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2651 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2652 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2654 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2655 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2656 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2657 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2658 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2660 | // Does it fit in a unsigned long? |
| 2661 | if (ResultVal.isIntN(LongSize)) { |
| 2662 | // Does it fit in a signed long? |
| 2663 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2664 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2665 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2666 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2667 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2668 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2672 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2673 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2674 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2675 | // Does it fit in a unsigned long long? |
| 2676 | if (ResultVal.isIntN(LongLongSize)) { |
| 2677 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2678 | // To be compatible with MSVC, hex integer literals ending with the |
| 2679 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2680 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2681 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2682 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2684 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2685 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | } |
| 2687 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2689 | // If we still couldn't decide a type, we probably have something that |
| 2690 | // 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] | 2691 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2692 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2693 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2694 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2697 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2698 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2699 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2700 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2701 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2703 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2704 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2706 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2707 | |
| 2708 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2711 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2712 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2713 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2714 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 2718 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2719 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2720 | SourceLocation OpLoc, |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2721 | SourceRange ExprRange, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2722 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2723 | if (exprType->isDependentType()) |
| 2724 | return false; |
| 2725 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2726 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2727 | // the result is the size of the referenced type." |
| 2728 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2729 | // result shall be the alignment of the referenced type." |
| 2730 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2731 | exprType = Ref->getPointeeType(); |
| 2732 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2733 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2734 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2735 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2736 | if (isSizeof) |
| 2737 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 2738 | return false; |
| 2739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2741 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2742 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2743 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 2744 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2745 | return false; |
| 2746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2748 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2749 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2750 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2751 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2753 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2754 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2755 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2756 | << exprType << isSizeof << ExprRange; |
| 2757 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 2758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2760 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2763 | static bool CheckAlignOfExpr(Sema &S, Expr *E, SourceLocation OpLoc, |
| 2764 | SourceRange ExprRange) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2765 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2766 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2768 | if (isa<DeclRefExpr>(E)) |
| 2769 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2770 | |
| 2771 | // Cannot know anything else if the expression is dependent. |
| 2772 | if (E->isTypeDependent()) |
| 2773 | return false; |
| 2774 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2775 | if (E->getBitField()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2776 | S. Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2777 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2778 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2779 | |
| 2780 | // Alignment of a field access is always okay, so long as it isn't a |
| 2781 | // bit-field. |
| 2782 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2783 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2784 | return false; |
| 2785 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2786 | return S.CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2789 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2790 | ExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2791 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2792 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2793 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2794 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2795 | return ExprError(); |
| 2796 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2797 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2799 | if (!T->isDependentType() && |
| 2800 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 2801 | return ExprError(); |
| 2802 | |
| 2803 | // 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] | 2804 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2805 | Context.getSizeType(), OpLoc, |
| 2806 | R.getEnd())); |
| 2807 | } |
| 2808 | |
| 2809 | /// \brief Build a sizeof or alignof expression given an expression |
| 2810 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2811 | ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2813 | bool isSizeOf, SourceRange R) { |
| 2814 | // Verify that the operand is valid. |
| 2815 | bool isInvalid = false; |
| 2816 | if (E->isTypeDependent()) { |
| 2817 | // Delay type-checking for type-dependent expressions. |
| 2818 | } else if (!isSizeOf) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2819 | isInvalid = CheckAlignOfExpr(*this, E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2820 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2821 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 2822 | isInvalid = true; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2823 | } else if (E->getType()->isPlaceholderType()) { |
| 2824 | ExprResult PE = CheckPlaceholderExpr(E, OpLoc); |
| 2825 | if (PE.isInvalid()) return ExprError(); |
| 2826 | return CreateSizeOfAlignOfExpr(PE.take(), OpLoc, isSizeOf, R); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2827 | } else { |
| 2828 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 2829 | } |
| 2830 | |
| 2831 | if (isInvalid) |
| 2832 | return ExprError(); |
| 2833 | |
| 2834 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 2835 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 2836 | Context.getSizeType(), OpLoc, |
| 2837 | R.getEnd())); |
| 2838 | } |
| 2839 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2840 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 2841 | /// the same for @c alignof and @c __alignof |
| 2842 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2843 | ExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2844 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 2845 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2847 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2848 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2849 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2850 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2851 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2852 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2855 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2856 | ExprResult Result |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2857 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 2858 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2859 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2862 | static QualType CheckRealImagOperand(Sema &S, Expr *&V, SourceLocation Loc, |
| 2863 | bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2864 | if (V->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2865 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2867 | // _Real and _Imag are only l-values for normal l-values. |
| 2868 | if (V->getObjectKind() != OK_Ordinary) |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2869 | S.DefaultLvalueConversion(V); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2870 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2871 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2872 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2873 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2875 | // Otherwise they pass through real integer and floating point types here. |
| 2876 | if (V->getType()->isArithmeticType()) |
| 2877 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2879 | // Test for placeholders. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2880 | ExprResult PR = S.CheckPlaceholderExpr(V, Loc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2881 | if (PR.isInvalid()) return QualType(); |
| 2882 | if (PR.take() != V) { |
| 2883 | V = PR.take(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2884 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2887 | // Reject anything else. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2888 | S.Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2889 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2890 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2894 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2895 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2896 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2897 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2898 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2899 | switch (Kind) { |
| 2900 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2901 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2902 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2903 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2904 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2905 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2908 | /// Expressions of certain arbitrary types are forbidden by C from |
| 2909 | /// having l-value type. These are: |
| 2910 | /// - 'void', but not qualified void |
| 2911 | /// - function types |
| 2912 | /// |
| 2913 | /// The exact rule here is C99 6.3.2.1: |
| 2914 | /// An lvalue is an expression with an object type or an incomplete |
| 2915 | /// type other than void. |
| 2916 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 2917 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 2918 | T->isFunctionType()); |
| 2919 | } |
| 2920 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2921 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2922 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2923 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2924 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2925 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2926 | if (Result.isInvalid()) return ExprError(); |
| 2927 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2928 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2929 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2931 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2932 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2933 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2934 | Context.DependentTy, |
| 2935 | VK_LValue, OK_Ordinary, |
| 2936 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2940 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2941 | LHSExp->getType()->isEnumeralType() || |
| 2942 | RHSExp->getType()->isRecordType() || |
| 2943 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2944 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2947 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2951 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2952 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 2953 | Expr *Idx, SourceLocation RLoc) { |
| 2954 | Expr *LHSExp = Base; |
| 2955 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2956 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2957 | // Perform default conversions. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2958 | if (!LHSExp->getType()->getAs<VectorType>()) |
| 2959 | DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2960 | DefaultFunctionArrayLvalueConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2961 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2962 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2963 | ExprValueKind VK = VK_LValue; |
| 2964 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2966 | // 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] | 2967 | // 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] | 2968 | // 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] | 2969 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2970 | Expr *BaseExpr, *IndexExpr; |
| 2971 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2972 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2973 | BaseExpr = LHSExp; |
| 2974 | IndexExpr = RHSExp; |
| 2975 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2976 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2977 | BaseExpr = LHSExp; |
| 2978 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2979 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2980 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2981 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2982 | BaseExpr = RHSExp; |
| 2983 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2984 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2986 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2987 | BaseExpr = LHSExp; |
| 2988 | IndexExpr = RHSExp; |
| 2989 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2991 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2992 | // Handle the uncommon case of "123[Ptr]". |
| 2993 | BaseExpr = RHSExp; |
| 2994 | IndexExpr = LHSExp; |
| 2995 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2996 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2997 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2998 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2999 | VK = LHSExp->getValueKind(); |
| 3000 | if (VK != VK_RValue) |
| 3001 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3003 | // FIXME: need to deal with const... |
| 3004 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3005 | } else if (LHSTy->isArrayType()) { |
| 3006 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3007 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3008 | // wasn't promoted because of the C90 rule that doesn't |
| 3009 | // allow promoting non-lvalue arrays. Warn, then |
| 3010 | // force the promotion here. |
| 3011 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3012 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3013 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3014 | CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3015 | LHSTy = LHSExp->getType(); |
| 3016 | |
| 3017 | BaseExpr = LHSExp; |
| 3018 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3019 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3020 | } else if (RHSTy->isArrayType()) { |
| 3021 | // Same as previous, except for 123[f().a] case |
| 3022 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3023 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3024 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3025 | CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3026 | RHSTy = RHSExp->getType(); |
| 3027 | |
| 3028 | BaseExpr = RHSExp; |
| 3029 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3030 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3031 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3032 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3033 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3034 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3035 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3036 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3037 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3038 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3039 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3040 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3041 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3042 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3043 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3044 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3045 | // 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] | 3046 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3047 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3048 | // incomplete types are not object types. |
| 3049 | if (ResultType->isFunctionType()) { |
| 3050 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3051 | << ResultType << BaseExpr->getSourceRange(); |
| 3052 | return ExprError(); |
| 3053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3055 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3056 | // GNU extension: subscripting on pointer to void |
| 3057 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3058 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3059 | |
| 3060 | // C forbids expressions of unqualified void type from being l-values. |
| 3061 | // See IsCForbiddenLValueType. |
| 3062 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3063 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3065 | PDiag(diag::err_subscript_incomplete_type) |
| 3066 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3067 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3069 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3070 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3071 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3072 | << ResultType << BaseExpr->getSourceRange(); |
| 3073 | return ExprError(); |
| 3074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3076 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3077 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3078 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3079 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3080 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3083 | /// Check an ext-vector component access expression. |
| 3084 | /// |
| 3085 | /// VK should be set in advance to the value kind of the base |
| 3086 | /// expression. |
| 3087 | static QualType |
| 3088 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3089 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3090 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3091 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3092 | // see FIXME there. |
| 3093 | // |
| 3094 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3095 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3096 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3097 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3098 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3099 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3100 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3101 | // 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] | 3102 | // special names that indicate a subset of exactly half the elements are |
| 3103 | // to be selected. |
| 3104 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3105 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3106 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3107 | // 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] | 3108 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3109 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3110 | bool HasRepeated = false; |
| 3111 | bool HasIndex[16] = {}; |
| 3112 | |
| 3113 | int Idx; |
| 3114 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3115 | // Check that we've found one of the special components, or that the component |
| 3116 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3117 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3118 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3119 | HalvingSwizzle = true; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3120 | } else if (!HexSwizzle && |
| 3121 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3122 | do { |
| 3123 | if (HasIndex[Idx]) HasRepeated = true; |
| 3124 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3125 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3126 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3127 | } else { |
| 3128 | if (HexSwizzle) compStr++; |
| 3129 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3130 | if (HasIndex[Idx]) HasRepeated = true; |
| 3131 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3132 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3133 | } |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3134 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3135 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3136 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3137 | // We didn't get to the end of the string. This means the component names |
| 3138 | // didn't come from the same set *or* we encountered an illegal name. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3139 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3140 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3141 | return QualType(); |
| 3142 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3143 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3144 | // Ensure no component accessor exceeds the width of the vector type it |
| 3145 | // operates on. |
| 3146 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3147 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3148 | |
| 3149 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3150 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3151 | |
| 3152 | while (*compStr) { |
| 3153 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3154 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3155 | << baseType << SourceRange(CompLoc); |
| 3156 | return QualType(); |
| 3157 | } |
| 3158 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3159 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3160 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3161 | // 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] | 3162 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3163 | // 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] | 3164 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3165 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3166 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3167 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3168 | if (HexSwizzle) |
| 3169 | CompSize--; |
| 3170 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3171 | if (CompSize == 1) |
| 3172 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3173 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3174 | if (HasRepeated) VK = VK_RValue; |
| 3175 | |
| 3176 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3177 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3178 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3179 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3180 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3181 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3182 | } |
| 3183 | 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] | 3184 | } |
| 3185 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3186 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3187 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3188 | const Selector &Sel, |
| 3189 | ASTContext &Context) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3190 | if (Member) |
| 3191 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3192 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3193 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3194 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3196 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3197 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3198 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3199 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3200 | return D; |
| 3201 | } |
| 3202 | return 0; |
| 3203 | } |
| 3204 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3205 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3206 | IdentifierInfo *Member, |
| 3207 | const Selector &Sel, |
| 3208 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3209 | // Check protocols on qualified interfaces. |
| 3210 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3211 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3212 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3213 | if (Member) |
| 3214 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3215 | GDecl = PD; |
| 3216 | break; |
| 3217 | } |
| 3218 | // Also must look for a getter or setter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3219 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3220 | GDecl = OMD; |
| 3221 | break; |
| 3222 | } |
| 3223 | } |
| 3224 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3225 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3226 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3227 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3228 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3229 | Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3230 | if (GDecl) |
| 3231 | return GDecl; |
| 3232 | } |
| 3233 | } |
| 3234 | return GDecl; |
| 3235 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3236 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3237 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3238 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3239 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3240 | const CXXScopeSpec &SS, |
| 3241 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3242 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3243 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3244 | // Even in dependent contexts, try to diagnose base expressions with |
| 3245 | // obviously wrong types, e.g.: |
| 3246 | // |
| 3247 | // T* t; |
| 3248 | // t.f; |
| 3249 | // |
| 3250 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3251 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3252 | // allows this, while still reporting an error if T is a struct pointer. |
| 3253 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3254 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3255 | if (PT && (!getLangOptions().ObjC1 || |
| 3256 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3257 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3258 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3259 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3260 | return ExprError(); |
| 3261 | } |
| 3262 | } |
| 3263 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3264 | assert(BaseType->isDependentType() || |
| 3265 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3266 | isDependentScopeSpecifier(SS)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3267 | |
| 3268 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3269 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3270 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3271 | IsArrow, OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3272 | SS.getScopeRep(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3273 | SS.getRange(), |
| 3274 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3275 | NameInfo, TemplateArgs)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | /// We know that the given qualified member reference points only to |
| 3279 | /// declarations which do not belong to the static type of the base |
| 3280 | /// expression. Diagnose the problem. |
| 3281 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3282 | Expr *BaseExpr, |
| 3283 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3284 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3285 | NamedDecl *rep, |
| 3286 | const DeclarationNameInfo &nameInfo) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3287 | // If this is an implicit member access, use a different set of |
| 3288 | // diagnostics. |
| 3289 | if (!BaseExpr) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3290 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3291 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3292 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3293 | << SS.getRange() << rep << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | // Check whether the declarations we found through a nested-name |
| 3297 | // specifier in a member expression are actually members of the base |
| 3298 | // type. The restriction here is: |
| 3299 | // |
| 3300 | // C++ [expr.ref]p2: |
| 3301 | // ... In these cases, the id-expression shall name a |
| 3302 | // member of the class or of one of its base classes. |
| 3303 | // |
| 3304 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3305 | // an unrelated class, and for us to find an overload set including |
| 3306 | // decls from classes which are not superclasses, as long as the decl |
| 3307 | // we actually pick through overload resolution is from a superclass. |
| 3308 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3309 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3310 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3311 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3312 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3313 | if (!BaseRT) { |
| 3314 | // We can't check this yet because the base type is still |
| 3315 | // dependent. |
| 3316 | assert(BaseType->isDependentType()); |
| 3317 | return false; |
| 3318 | } |
| 3319 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3320 | |
| 3321 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3322 | // If this is an implicit member reference and we find a |
| 3323 | // non-instance member, it's not an error. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3324 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3325 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3326 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3327 | // Note that we use the DC of the decl, not the underlying decl. |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3328 | DeclContext *DC = (*I)->getDeclContext(); |
| 3329 | while (DC->isTransparentContext()) |
| 3330 | DC = DC->getParent(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 9d4bb94 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3332 | if (!DC->isRecord()) |
| 3333 | continue; |
| 3334 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3335 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3336 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3337 | |
| 3338 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3339 | return false; |
| 3340 | } |
| 3341 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3342 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3343 | R.getRepresentativeDecl(), |
| 3344 | R.getLookupNameInfo()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3345 | return true; |
| 3346 | } |
| 3347 | |
| 3348 | static bool |
| 3349 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3350 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3351 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3352 | bool HasTemplateArgs) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3353 | RecordDecl *RDecl = RTy->getDecl(); |
| 3354 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3355 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3356 | << BaseRange)) |
| 3357 | return true; |
| 3358 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3359 | if (HasTemplateArgs) { |
| 3360 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3361 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3362 | |
| 3363 | bool MOUS; |
| 3364 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3365 | return false; |
| 3366 | } |
| 3367 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3368 | DeclContext *DC = RDecl; |
| 3369 | if (SS.isSet()) { |
| 3370 | // If the member name was a qualified-id, look into the |
| 3371 | // nested-name-specifier. |
| 3372 | DC = SemaRef.computeDeclContext(SS, false); |
| 3373 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3374 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3375 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3376 | << SS.getRange() << DC; |
| 3377 | return true; |
| 3378 | } |
| 3379 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3380 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3381 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3382 | if (!isa<TypeDecl>(DC)) { |
| 3383 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3384 | << DC << SS.getRange(); |
| 3385 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3389 | // The record definition is complete, now look up the member. |
| 3390 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3392 | if (!R.empty()) |
| 3393 | return false; |
| 3394 | |
| 3395 | // We didn't find anything with the given name, so try to correct |
| 3396 | // for typos. |
| 3397 | DeclarationName Name = R.getLookupName(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3398 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3399 | !R.empty() && |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3400 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3401 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3402 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3403 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3404 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3405 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3406 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3407 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3408 | return false; |
| 3409 | } else { |
| 3410 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3411 | R.setLookupName(Name); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3414 | return false; |
| 3415 | } |
| 3416 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3417 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3418 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3419 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3420 | CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3421 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3422 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3423 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3424 | if (BaseType->isDependentType() || |
| 3425 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3426 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3427 | IsArrow, OpLoc, |
| 3428 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3429 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3430 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3431 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3432 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3433 | // Implicit member accesses. |
| 3434 | if (!Base) { |
| 3435 | QualType RecordTy = BaseType; |
| 3436 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3437 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3438 | RecordTy->getAs<RecordType>(), |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3439 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3440 | return ExprError(); |
| 3441 | |
| 3442 | // Explicit member accesses. |
| 3443 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3444 | ExprResult Result = |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3445 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3446 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3447 | |
| 3448 | if (Result.isInvalid()) { |
| 3449 | Owned(Base); |
| 3450 | return ExprError(); |
| 3451 | } |
| 3452 | |
| 3453 | if (Result.get()) |
| 3454 | return move(Result); |
Sebastian Redl | f3e6337 | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3455 | |
| 3456 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3457 | BaseType = Base->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3460 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3461 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3462 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3465 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3466 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3467 | SourceLocation OpLoc, bool IsArrow, |
| 3468 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3469 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3470 | LookupResult &R, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3471 | const TemplateArgumentListInfo *TemplateArgs, |
| 3472 | bool SuppressQualifierCheck) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3473 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3474 | if (IsArrow) { |
| 3475 | assert(BaseType->isPointerType()); |
| 3476 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3477 | } |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3478 | R.setBaseObjectType(BaseType); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3479 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3480 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3481 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3482 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3483 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3484 | |
| 3485 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3486 | return ExprError(); |
| 3487 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3488 | if (R.empty()) { |
| 3489 | // Rederive where we looked up. |
| 3490 | DeclContext *DC = (SS.isSet() |
| 3491 | ? computeDeclContext(SS, false) |
| 3492 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3493 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3494 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3495 | << MemberName << DC |
| 3496 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3497 | return ExprError(); |
| 3498 | } |
| 3499 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3500 | // Diagnose lookups that find only declarations from a non-base |
| 3501 | // type. This is possible for either qualified lookups (which may |
| 3502 | // have been qualified with an unrelated type) or implicit member |
| 3503 | // expressions (which were found with unqualified lookup and thus |
| 3504 | // may have come from an enclosing scope). Note that it's okay for |
| 3505 | // lookup to find declarations from a non-base type as long as those |
| 3506 | // aren't the ones picked by overload resolution. |
| 3507 | if ((SS.isSet() || !BaseExpr || |
| 3508 | (isa<CXXThisExpr>(BaseExpr) && |
| 3509 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3510 | !SuppressQualifierCheck && |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3511 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3512 | return ExprError(); |
| 3513 | |
| 3514 | // Construct an unresolved result if we in fact got an unresolved |
| 3515 | // result. |
| 3516 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3517 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3518 | // pick a member. |
| 3519 | R.suppressDiagnostics(); |
| 3520 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3521 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3522 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3523 | BaseExpr, BaseExprType, |
| 3524 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3525 | Qualifier, SS.getRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3526 | MemberNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3527 | TemplateArgs, R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3528 | |
| 3529 | return Owned(MemExpr); |
| 3530 | } |
| 3531 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3532 | assert(R.isSingleResult()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3533 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3534 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 3535 | |
| 3536 | // FIXME: diagnose the presence of template arguments now. |
| 3537 | |
| 3538 | // If the decl being referenced had an error, return an error for this |
| 3539 | // sub-expr without emitting another error, in order to avoid cascading |
| 3540 | // error cases. |
| 3541 | if (MemberDecl->isInvalidDecl()) |
| 3542 | return ExprError(); |
| 3543 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3544 | // Handle the implicit-member-access case. |
| 3545 | if (!BaseExpr) { |
| 3546 | // If this is not an instance member, convert to a non-member access. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3547 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3548 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 3550 | SourceLocation Loc = R.getNameLoc(); |
| 3551 | if (SS.getRange().isValid()) |
| 3552 | Loc = SS.getRange().getBegin(); |
| 3553 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3556 | bool ShouldCheckUse = true; |
| 3557 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 3558 | // Don't diagnose the use of a virtual member function unless it's |
| 3559 | // explicitly qualified. |
| 3560 | if (MD->isVirtual() && !SS.isSet()) |
| 3561 | ShouldCheckUse = false; |
| 3562 | } |
| 3563 | |
| 3564 | // Check the use of this member. |
| 3565 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 3566 | Owned(BaseExpr); |
| 3567 | return ExprError(); |
| 3568 | } |
| 3569 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3570 | // Perform a property load on the base regardless of whether we |
| 3571 | // actually need it for the declaration. |
| 3572 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) |
| 3573 | ConvertPropertyForRValue(BaseExpr); |
| 3574 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3575 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 3576 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 3577 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3578 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3579 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 3580 | // We may have found a field within an anonymous union or struct |
| 3581 | // (C++ [class.union]). |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3582 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3583 | BaseExpr, OpLoc); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3584 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3585 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 3586 | MarkDeclarationReferenced(MemberLoc, Var); |
| 3587 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3588 | Var, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3589 | Var->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3590 | VK_LValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3593 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3594 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3595 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3596 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3597 | MemberFn->getType(), |
| 3598 | MemberFn->isInstance() ? VK_RValue : VK_LValue, |
| 3599 | OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3600 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3601 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3602 | |
| 3603 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 3604 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3605 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3606 | Enum, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3607 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | Owned(BaseExpr); |
| 3611 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3612 | // We found something that we didn't expect. Complain. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3613 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3614 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3615 | << MemberName << BaseType << int(IsArrow); |
| 3616 | else |
| 3617 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 3618 | << MemberName << BaseType << int(IsArrow); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3620 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 3621 | << MemberName; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3622 | R.suppressDiagnostics(); |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3623 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3626 | /// Given that normal member access failed on the given expression, |
| 3627 | /// and given that the expression's type involves builtin-id or |
| 3628 | /// builtin-Class, decide whether substituting in the redefinition |
| 3629 | /// types would be profitable. The redefinition type is whatever |
| 3630 | /// this translation unit tried to typedef to id/Class; we store |
| 3631 | /// it to the side and then re-use it in places like this. |
| 3632 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, Expr *&base) { |
| 3633 | const ObjCObjectPointerType *opty |
| 3634 | = base->getType()->getAs<ObjCObjectPointerType>(); |
| 3635 | if (!opty) return false; |
| 3636 | |
| 3637 | const ObjCObjectType *ty = opty->getObjectType(); |
| 3638 | |
| 3639 | QualType redef; |
| 3640 | if (ty->isObjCId()) { |
| 3641 | redef = S.Context.ObjCIdRedefinitionType; |
| 3642 | } else if (ty->isObjCClass()) { |
| 3643 | redef = S.Context.ObjCClassRedefinitionType; |
| 3644 | } else { |
| 3645 | return false; |
| 3646 | } |
| 3647 | |
| 3648 | // Do the substitution as long as the redefinition type isn't just a |
| 3649 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 3650 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 3651 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 3652 | return false; |
| 3653 | |
| 3654 | S.ImpCastExprToType(base, redef, CK_BitCast); |
| 3655 | return true; |
| 3656 | } |
| 3657 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3658 | /// Look up the given member of the given non-type-dependent |
| 3659 | /// expression. This can return in one of two ways: |
| 3660 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 3661 | /// assume that lookup was performed and the results written into |
| 3662 | /// the provided structure. It will take over from there. |
| 3663 | /// * Otherwise, the returned expression will be produced in place of |
| 3664 | /// an ordinary member expression. |
| 3665 | /// |
| 3666 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 3667 | /// fixed for ObjC++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3668 | ExprResult |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3669 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3670 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3671 | CXXScopeSpec &SS, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3672 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3673 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 3675 | // Perform default conversions. |
| 3676 | DefaultFunctionArrayConversion(BaseExpr); |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3677 | if (IsArrow) DefaultLvalueConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3678 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 3679 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3680 | assert(!BaseType->isDependentType()); |
| 3681 | |
| 3682 | DeclarationName MemberName = R.getLookupName(); |
| 3683 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3684 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3685 | // For later type-checking purposes, turn arrow accesses into dot |
| 3686 | // accesses. The only access type we support that doesn't follow |
| 3687 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 3688 | // and those never use arrows, so this is unaffected. |
| 3689 | if (IsArrow) { |
| 3690 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3691 | BaseType = Ptr->getPointeeType(); |
| 3692 | else if (const ObjCObjectPointerType *Ptr |
| 3693 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3694 | BaseType = Ptr->getPointeeType(); |
| 3695 | else if (BaseType->isRecordType()) { |
| 3696 | // Recover from arrow accesses to records, e.g.: |
| 3697 | // struct MyRecord foo; |
| 3698 | // foo->bar |
| 3699 | // This is actually well-formed in C++ if MyRecord has an |
| 3700 | // overloaded operator->, but that should have been dealt with |
| 3701 | // by now. |
| 3702 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3703 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 3704 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 3705 | IsArrow = false; |
| 3706 | } else { |
| 3707 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 3708 | << BaseType << BaseExpr->getSourceRange(); |
| 3709 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3710 | } |
| 3711 | } |
| 3712 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3713 | // Handle field access to simple records. |
| 3714 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| 3715 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 3716 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 3717 | return ExprError(); |
| 3718 | |
| 3719 | // Returning valid-but-null is how we indicate to the caller that |
| 3720 | // the lookup result was filled in. |
| 3721 | return Owned((Expr*) 0); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3722 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3723 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3724 | // Handle ivar access to Objective-C objects. |
| 3725 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3726 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3727 | |
| 3728 | // There are three cases for the base type: |
| 3729 | // - builtin id (qualified or unqualified) |
| 3730 | // - builtin Class (qualified or unqualified) |
| 3731 | // - an interface |
| 3732 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 3733 | if (!IDecl) { |
| 3734 | // There's an implicit 'isa' ivar on all objects. |
| 3735 | // But we only actually find it this way on objects of type 'id', |
| 3736 | // apparently. |
| 3737 | if (OTy->isObjCId() && Member->isStr("isa")) |
| 3738 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, IsArrow, MemberLoc, |
| 3739 | Context.getObjCClassType())); |
| 3740 | |
| 3741 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3742 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3743 | ObjCImpDecl, HasTemplateArgs); |
| 3744 | goto fail; |
| 3745 | } |
| 3746 | |
| 3747 | ObjCInterfaceDecl *ClassDeclared; |
| 3748 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 3749 | |
| 3750 | if (!IV) { |
| 3751 | // Attempt to correct for typos in ivar names. |
| 3752 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3753 | LookupMemberName); |
| 3754 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 3755 | IsArrow ? CTC_ObjCIvarLookup |
| 3756 | : CTC_ObjCPropertyLookup) && |
| 3757 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 3758 | Diag(R.getNameLoc(), |
| 3759 | diag::err_typecheck_member_reference_ivar_suggest) |
| 3760 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 3761 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3762 | IV->getNameAsString()); |
| 3763 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 3764 | << IV->getDeclName(); |
| 3765 | } else { |
| 3766 | Res.clear(); |
| 3767 | Res.setLookupName(Member); |
| 3768 | |
| 3769 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 3770 | << IDecl->getDeclName() << MemberName |
| 3771 | << BaseExpr->getSourceRange(); |
| 3772 | return ExprError(); |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | // If the decl being referenced had an error, return an error for this |
| 3777 | // sub-expr without emitting another error, in order to avoid cascading |
| 3778 | // error cases. |
| 3779 | if (IV->isInvalidDecl()) |
| 3780 | return ExprError(); |
| 3781 | |
| 3782 | // Check whether we can reference this field. |
| 3783 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 3784 | return ExprError(); |
| 3785 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 3786 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 3787 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 3788 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 3789 | ClassOfMethodDecl = MD->getClassInterface(); |
| 3790 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 3791 | // Case of a c-function declared inside an objc implementation. |
| 3792 | // FIXME: For a c-style function nested inside an objc implementation |
| 3793 | // class, there is no implementation context available, so we pass |
| 3794 | // down the context as argument to this routine. Ideally, this context |
| 3795 | // need be passed down in the AST node and somehow calculated from the |
| 3796 | // AST for a function decl. |
| 3797 | if (ObjCImplementationDecl *IMPD = |
| 3798 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 3799 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 3800 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 3801 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 3802 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 3803 | } |
| 3804 | |
| 3805 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 3806 | if (ClassDeclared != IDecl || |
| 3807 | ClassOfMethodDecl != ClassDeclared) |
| 3808 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 3809 | << IV->getDeclName(); |
| 3810 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 3811 | // @protected |
| 3812 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 3813 | << IV->getDeclName(); |
| 3814 | } |
| 3815 | |
| 3816 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 3817 | MemberLoc, BaseExpr, |
| 3818 | IsArrow)); |
| 3819 | } |
| 3820 | |
| 3821 | // Objective-C property access. |
| 3822 | const ObjCObjectPointerType *OPT; |
| 3823 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 3824 | // This actually uses the base as an r-value. |
| 3825 | DefaultLvalueConversion(BaseExpr); |
| 3826 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr->getType())); |
| 3827 | |
| 3828 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3829 | |
| 3830 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 3831 | |
| 3832 | // id, with and without qualifiers. |
| 3833 | if (OT->isObjCId()) { |
| 3834 | // Check protocols on qualified interfaces. |
| 3835 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 3836 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 3837 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 3838 | // Check the use of this declaration |
| 3839 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3840 | return ExprError(); |
| 3841 | |
| 3842 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 3843 | VK_LValue, |
| 3844 | OK_ObjCProperty, |
| 3845 | MemberLoc, |
| 3846 | BaseExpr)); |
| 3847 | } |
| 3848 | |
| 3849 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3850 | // Check the use of this method. |
| 3851 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3852 | return ExprError(); |
| 3853 | Selector SetterSel = |
| 3854 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3855 | PP.getSelectorTable(), Member); |
| 3856 | ObjCMethodDecl *SMD = 0; |
| 3857 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 3858 | SetterSel, Context)) |
| 3859 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 3860 | QualType PType = OMD->getSendResultType(); |
| 3861 | |
| 3862 | ExprValueKind VK = VK_LValue; |
| 3863 | if (!getLangOptions().CPlusPlus && |
| 3864 | IsCForbiddenLValueType(Context, PType)) |
| 3865 | VK = VK_RValue; |
| 3866 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 3867 | |
| 3868 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 3869 | VK, OK, |
| 3870 | MemberLoc, BaseExpr)); |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3875 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3876 | ObjCImpDecl, HasTemplateArgs); |
| 3877 | |
| 3878 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 3879 | << MemberName << BaseType); |
| 3880 | } |
| 3881 | |
| 3882 | // 'Class', unqualified only. |
| 3883 | if (OT->isObjCClass()) { |
| 3884 | // Only works in a method declaration (??!). |
| 3885 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 3886 | if (!MD) { |
| 3887 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3888 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3889 | ObjCImpDecl, HasTemplateArgs); |
| 3890 | |
| 3891 | goto fail; |
| 3892 | } |
| 3893 | |
| 3894 | // Also must look for a getter name which uses property syntax. |
| 3895 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3896 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 3897 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3898 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 3899 | // Check the use of this method. |
| 3900 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3901 | return ExprError(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3902 | } else |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3903 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3904 | // If we found a getter then this may be a valid dot-reference, we |
| 3905 | // will look for the matching setter, in case it is needed. |
| 3906 | Selector SetterSel = |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3907 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3908 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3909 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 3910 | if (!Setter) { |
| 3911 | // If this reference is in an @implementation, also check for 'private' |
| 3912 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3913 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3914 | } |
| 3915 | // Look through local category implementations associated with the class. |
| 3916 | if (!Setter) |
| 3917 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3918 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3919 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3920 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3921 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3922 | if (Getter || Setter) { |
| 3923 | QualType PType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3924 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3925 | ExprValueKind VK = VK_LValue; |
| 3926 | if (Getter) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3927 | PType = Getter->getSendResultType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3928 | if (!getLangOptions().CPlusPlus && |
| 3929 | IsCForbiddenLValueType(Context, PType)) |
| 3930 | VK = VK_RValue; |
| 3931 | } else { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3932 | // Get the expression type from Setter's incoming parameter. |
| 3933 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3934 | } |
| 3935 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 3936 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3937 | // FIXME: we must check that the setter has property type. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 3938 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 3939 | PType, VK, OK, |
| 3940 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3941 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3942 | |
| 3943 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3944 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3945 | ObjCImpDecl, HasTemplateArgs); |
| 3946 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3947 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3948 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3949 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3950 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3951 | // Normal property access. |
| 3952 | return HandleExprPropertyRefExpr(OPT, BaseExpr, MemberName, MemberLoc, |
| 3953 | SourceLocation(), QualType(), false); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3954 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3956 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3957 | if (BaseType->isExtVectorType()) { |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3958 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3959 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3960 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr->getValueKind()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3961 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 3962 | Member, MemberLoc); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3963 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3964 | return ExprError(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3965 | |
| 3966 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr, |
| 3967 | *Member, MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3968 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3969 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3970 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 3971 | // not just a pointer to builtin-sel again. |
| 3972 | if (IsArrow && |
| 3973 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 3974 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
| 3975 | ImpCastExprToType(BaseExpr, Context.ObjCSelRedefinitionType, CK_BitCast); |
| 3976 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3977 | ObjCImpDecl, HasTemplateArgs); |
| 3978 | } |
| 3979 | |
| 3980 | // Failure cases. |
| 3981 | fail: |
| 3982 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 3983 | // Recover from dot accesses to pointers, e.g.: |
| 3984 | // type *foo; |
| 3985 | // foo.bar |
| 3986 | // This is actually well-formed in two cases: |
| 3987 | // - 'type' is an Objective C type |
| 3988 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 3989 | // the appropriate pointer type |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3990 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 3991 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 3992 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3993 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 3994 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 3995 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3996 | |
| 3997 | // Recurse as an -> access. |
| 3998 | IsArrow = true; |
| 3999 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4000 | ObjCImpDecl, HasTemplateArgs); |
| 4001 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4004 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4005 | // because they forgot parentheses to call that function. |
| 4006 | bool TryCall = false; |
| 4007 | bool Overloaded = false; |
| 4008 | UnresolvedSet<8> AllOverloads; |
| 4009 | if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(BaseExpr)) { |
| 4010 | AllOverloads.append(Overloads->decls_begin(), Overloads->decls_end()); |
| 4011 | TryCall = true; |
| 4012 | Overloaded = true; |
| 4013 | } else if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(BaseExpr)) { |
| 4014 | if (FunctionDecl* Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 4015 | AllOverloads.addDecl(Fun); |
| 4016 | TryCall = true; |
| 4017 | } |
| 4018 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4019 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4020 | if (TryCall) { |
| 4021 | // Plunder the overload set for something that would make the member |
| 4022 | // expression valid. |
| 4023 | UnresolvedSet<4> ViableOverloads; |
| 4024 | bool HasViableZeroArgOverload = false; |
| 4025 | for (OverloadExpr::decls_iterator it = AllOverloads.begin(), |
| 4026 | DeclsEnd = AllOverloads.end(); it != DeclsEnd; ++it) { |
| 4027 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*it); |
| 4028 | QualType ResultTy = OverloadDecl->getResultType(); |
| 4029 | if ((!IsArrow && ResultTy->isRecordType()) || |
| 4030 | (IsArrow && ResultTy->isPointerType() && |
| 4031 | ResultTy->getPointeeType()->isRecordType())) { |
| 4032 | ViableOverloads.addDecl(*it); |
| 4033 | if (OverloadDecl->getMinRequiredArguments() == 0) { |
| 4034 | HasViableZeroArgOverload = true; |
| 4035 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4036 | } |
| 4037 | } |
| 4038 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4039 | if (!HasViableZeroArgOverload || ViableOverloads.size() != 1) { |
| 4040 | Diag(BaseExpr->getExprLoc(), diag::err_member_reference_needs_call) |
| 4041 | << 1 << 0 |
| 4042 | << BaseExpr->getSourceRange(); |
| 4043 | int ViableOverloadCount = ViableOverloads.size(); |
| 4044 | int I; |
| 4045 | for (I = 0; I < ViableOverloadCount; ++I) { |
| 4046 | // FIXME: Magic number for max shown overloads stolen from |
| 4047 | // OverloadCandidateSet::NoteCandidates. |
| 4048 | if (I >= 4 && Diags.getShowOverloads() == Diagnostic::Ovl_Best) { |
| 4049 | break; |
| 4050 | } |
| 4051 | Diag(ViableOverloads[I].getDecl()->getSourceRange().getBegin(), |
| 4052 | diag::note_member_ref_possible_intended_overload); |
| 4053 | } |
| 4054 | if (I != ViableOverloadCount) { |
| 4055 | Diag(BaseExpr->getExprLoc(), diag::note_ovl_too_many_candidates) |
| 4056 | << int(ViableOverloadCount - I); |
| 4057 | } |
| 4058 | return ExprError(); |
| 4059 | } |
| 4060 | } else { |
| 4061 | // We don't have an expression that's convenient to get a Decl from, but we |
| 4062 | // can at least check if the type is "function of 0 arguments which returns |
| 4063 | // an acceptable type". |
| 4064 | const FunctionType *Fun = NULL; |
| 4065 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 4066 | if ((Fun = Ptr->getPointeeType()->getAs<FunctionType>())) { |
| 4067 | TryCall = true; |
| 4068 | } |
| 4069 | } else if ((Fun = BaseType->getAs<FunctionType>())) { |
| 4070 | TryCall = true; |
| 4071 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4072 | |
| 4073 | if (TryCall) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4074 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Fun)) { |
| 4075 | if (FPT->getNumArgs() == 0) { |
| 4076 | QualType ResultTy = Fun->getResultType(); |
| 4077 | TryCall = (!IsArrow && ResultTy->isRecordType()) || |
| 4078 | (IsArrow && ResultTy->isPointerType() && |
| 4079 | ResultTy->getPointeeType()->isRecordType()); |
| 4080 | } |
Matt Beaumont-Gay | 26ae5dd | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 4081 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4082 | } |
| 4083 | } |
| 4084 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4085 | if (TryCall) { |
| 4086 | // At this point, we know BaseExpr looks like it's potentially callable with |
| 4087 | // 0 arguments, and that it returns something of a reasonable type, so we |
| 4088 | // can emit a fixit and carry on pretending that BaseExpr was actually a |
| 4089 | // CallExpr. |
| 4090 | SourceLocation ParenInsertionLoc = |
| 4091 | PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 4092 | Diag(BaseExpr->getExprLoc(), diag::err_member_reference_needs_call) |
| 4093 | << int(Overloaded) << 1 |
| 4094 | << BaseExpr->getSourceRange() |
| 4095 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4096 | ExprResult NewBase = ActOnCallExpr(0, BaseExpr, ParenInsertionLoc, |
| 4097 | MultiExprArg(*this, 0, 0), |
| 4098 | ParenInsertionLoc); |
| 4099 | if (NewBase.isInvalid()) |
| 4100 | return ExprError(); |
| 4101 | BaseExpr = NewBase.takeAs<Expr>(); |
| 4102 | DefaultFunctionArrayConversion(BaseExpr); |
| 4103 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4104 | ObjCImpDecl, HasTemplateArgs); |
| 4105 | } |
| 4106 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4107 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 4108 | << BaseType << BaseExpr->getSourceRange(); |
| 4109 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4110 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4113 | /// The main callback when the parser finds something like |
| 4114 | /// expression . [nested-name-specifier] identifier |
| 4115 | /// expression -> [nested-name-specifier] identifier |
| 4116 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4117 | /// possibilities, including destructor and operator references. |
| 4118 | /// |
| 4119 | /// \param OpKind either tok::arrow or tok::period |
| 4120 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4121 | /// is used to diagnose mis-uses of special members that can |
| 4122 | /// only be called |
| 4123 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4124 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4125 | /// aren't properly put in the context chain |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4126 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4127 | SourceLocation OpLoc, |
| 4128 | tok::TokenKind OpKind, |
| 4129 | CXXScopeSpec &SS, |
| 4130 | UnqualifiedId &Id, |
| 4131 | Decl *ObjCImpDecl, |
| 4132 | bool HasTrailingLParen) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4133 | if (SS.isSet() && SS.isInvalid()) |
| 4134 | return ExprError(); |
| 4135 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4136 | // Warn about the explicit constructor calls Microsoft extension. |
| 4137 | if (getLangOptions().Microsoft && |
| 4138 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4139 | Diag(Id.getSourceRange().getBegin(), |
| 4140 | diag::ext_ms_explicit_constructor_call); |
| 4141 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4142 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4143 | |
| 4144 | // Decompose the name into its component parts. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4145 | DeclarationNameInfo NameInfo; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4146 | const TemplateArgumentListInfo *TemplateArgs; |
| 4147 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4148 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4149 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4150 | DeclarationName Name = NameInfo.getName(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4151 | bool IsArrow = (OpKind == tok::arrow); |
| 4152 | |
| 4153 | NamedDecl *FirstQualifierInScope |
| 4154 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4155 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4156 | |
| 4157 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4158 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4159 | if (Result.isInvalid()) return ExprError(); |
| 4160 | Base = Result.take(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4162 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4163 | isDependentScopeSpecifier(SS)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4164 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4165 | IsArrow, OpLoc, |
| 4166 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4167 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4168 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4169 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4170 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 4171 | SS, ObjCImpDecl, TemplateArgs != 0); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4172 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4173 | if (Result.isInvalid()) { |
| 4174 | Owned(Base); |
| 4175 | return ExprError(); |
| 4176 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4177 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4178 | if (Result.get()) { |
| 4179 | // The only way a reference to a destructor can be used is to |
| 4180 | // immediately call it, which falls into this case. If the |
| 4181 | // next token is not a '(', produce a diagnostic and build the |
| 4182 | // call now. |
| 4183 | if (!HasTrailingLParen && |
| 4184 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4185 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4186 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4187 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4190 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4191 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4192 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4198 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4199 | FunctionDecl *FD, |
| 4200 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4201 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4202 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4203 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4204 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4206 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4207 | return ExprError(); |
| 4208 | } |
| 4209 | |
| 4210 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4211 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4213 | // Instantiate the expression. |
| 4214 | MultiLevelTemplateArgumentList ArgList |
| 4215 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4216 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4217 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4218 | = ArgList.getInnermost(); |
| 4219 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4220 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4221 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4222 | ExprResult Result; |
| 4223 | { |
| 4224 | // C++ [dcl.fct.default]p5: |
| 4225 | // The names in the [default argument] expression are bound, and |
| 4226 | // the semantic constraints are checked, at the point where the |
| 4227 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4228 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4229 | Result = SubstExpr(UninstExpr, ArgList); |
| 4230 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4231 | if (Result.isInvalid()) |
| 4232 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4234 | // Check the expression as an initializer for the parameter. |
| 4235 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4236 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4237 | InitializationKind Kind |
| 4238 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4239 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4240 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4242 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4243 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4244 | MultiExprArg(*this, &ResultE, 1)); |
| 4245 | if (Result.isInvalid()) |
| 4246 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4248 | // Build the default argument expression. |
| 4249 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4250 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4253 | // If the default expression creates temporaries, we need to |
| 4254 | // push them to the current stack of expression temporaries so they'll |
| 4255 | // be properly destroyed. |
| 4256 | // FIXME: We should really be rebuilding the default argument with new |
| 4257 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4258 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4259 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4260 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4261 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4262 | ExprTemporaries.push_back(Temporary); |
| 4263 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4264 | |
| 4265 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4266 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4267 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4268 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4269 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4272 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4273 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4274 | /// function prototype Proto. Call is the call expression itself, and |
| 4275 | /// Fn is the function expression. For a C++ member function, this |
| 4276 | /// routine does not attempt to convert the object argument. Returns |
| 4277 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4278 | bool |
| 4279 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4280 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4281 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4282 | Expr **Args, unsigned NumArgs, |
| 4283 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4284 | // 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] | 4285 | // assignment, to the types of the corresponding parameter, ... |
| 4286 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4287 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4289 | // If too few arguments are available (and we don't have default |
| 4290 | // arguments for the remaining parameters), don't make the call. |
| 4291 | if (NumArgs < NumArgsInProto) { |
| 4292 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4293 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4294 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4295 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4296 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | // If too many are passed and not variadic, error on the extras and drop |
| 4300 | // them. |
| 4301 | if (NumArgs > NumArgsInProto) { |
| 4302 | if (!Proto->isVariadic()) { |
| 4303 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4304 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4305 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4306 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4307 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4308 | Args[NumArgs-1]->getLocEnd()); |
| 4309 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4310 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4311 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4312 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4313 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4314 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4315 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4316 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4317 | if (Fn->getType()->isBlockPointerType()) |
| 4318 | CallType = VariadicBlock; // Block |
| 4319 | else if (isa<MemberExpr>(Fn)) |
| 4320 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4321 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4322 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4323 | if (Invalid) |
| 4324 | return true; |
| 4325 | unsigned TotalNumArgs = AllArgs.size(); |
| 4326 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4327 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4328 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4329 | return false; |
| 4330 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4331 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4332 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4333 | FunctionDecl *FDecl, |
| 4334 | const FunctionProtoType *Proto, |
| 4335 | unsigned FirstProtoArg, |
| 4336 | Expr **Args, unsigned NumArgs, |
| 4337 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4338 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4339 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4340 | unsigned NumArgsToCheck = NumArgs; |
| 4341 | bool Invalid = false; |
| 4342 | if (NumArgs != NumArgsInProto) |
| 4343 | // Use default arguments for missing arguments |
| 4344 | NumArgsToCheck = NumArgsInProto; |
| 4345 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4346 | // 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] | 4347 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4348 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4350 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4351 | if (ArgIx < NumArgs) { |
| 4352 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4353 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4354 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4355 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4356 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4357 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4358 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4360 | // Pass the argument |
| 4361 | ParmVarDecl *Param = 0; |
| 4362 | if (FDecl && i < FDecl->getNumParams()) |
| 4363 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4364 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4365 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4366 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4367 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4368 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4369 | SourceLocation(), |
| 4370 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4371 | if (ArgE.isInvalid()) |
| 4372 | return true; |
| 4373 | |
| 4374 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4375 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4376 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4377 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4378 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4379 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4380 | if (ArgExpr.isInvalid()) |
| 4381 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4382 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4383 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4384 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4385 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4386 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4387 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4388 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4389 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4390 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4391 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4392 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4393 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType, FDecl); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4394 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4395 | } |
| 4396 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4397 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4400 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4401 | /// This provides the location of the left/right parens and a list of comma |
| 4402 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4403 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4404 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4405 | MultiExprArg args, SourceLocation RParenLoc, |
| 4406 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4407 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4408 | |
| 4409 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4410 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4411 | if (Result.isInvalid()) return ExprError(); |
| 4412 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4414 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4416 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4417 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4418 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4419 | if (NumArgs > 0) { |
| 4420 | // Pseudo-destructor calls should not have any arguments. |
| 4421 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4422 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4423 | SourceRange(Args[0]->getLocStart(), |
| 4424 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4426 | NumArgs = 0; |
| 4427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4429 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4430 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4433 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4434 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4435 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4436 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4437 | bool Dependent = false; |
| 4438 | if (Fn->isTypeDependent()) |
| 4439 | Dependent = true; |
| 4440 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4441 | Dependent = true; |
| 4442 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4443 | if (Dependent) { |
| 4444 | if (ExecConfig) { |
| 4445 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4446 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4447 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4448 | } else { |
| 4449 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4450 | Context.DependentTy, VK_RValue, |
| 4451 | RParenLoc)); |
| 4452 | } |
| 4453 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4454 | |
| 4455 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4456 | if (Fn->getType()->isRecordType()) |
| 4457 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4458 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4459 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4460 | Expr *NakedFn = Fn->IgnoreParens(); |
| 4461 | |
| 4462 | // Determine whether this is a call to an unresolved member function. |
| 4463 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4464 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 4465 | // an unresolved using declaration), it has to be an overloaded |
| 4466 | // function set, which means it must contain either multiple |
| 4467 | // declarations (all methods or method templates) or a single |
| 4468 | // method template. |
| 4469 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4470 | isa<FunctionTemplateDecl>( |
| 4471 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 4472 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4473 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4474 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4475 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4478 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4479 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4480 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4481 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4482 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4483 | RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4484 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4485 | |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4486 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4487 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4488 | if (BO->getOpcode() == BO_PtrMemD || |
| 4489 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 4490 | if (const FunctionProtoType *FPT |
| 4491 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4492 | QualType ResultTy = FPT->getCallResultType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4493 | ExprValueKind VK = Expr::getValueKindForType(FPT->getResultType()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | fdc13a0 | 2011-02-04 12:57:49 +0000 | [diff] [blame] | 4495 | // Check that the object type isn't more qualified than the |
| 4496 | // member function we're calling. |
| 4497 | Qualifiers FuncQuals = Qualifiers::fromCVRMask(FPT->getTypeQuals()); |
| 4498 | Qualifiers ObjectQuals |
| 4499 | = BO->getOpcode() == BO_PtrMemD |
| 4500 | ? BO->getLHS()->getType().getQualifiers() |
| 4501 | : BO->getLHS()->getType()->getAs<PointerType>() |
| 4502 | ->getPointeeType().getQualifiers(); |
| 4503 | |
| 4504 | Qualifiers Difference = ObjectQuals - FuncQuals; |
| 4505 | Difference.removeObjCGCAttr(); |
| 4506 | Difference.removeAddressSpace(); |
| 4507 | if (Difference) { |
| 4508 | std::string QualsString = Difference.getAsString(); |
| 4509 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 4510 | << BO->getType().getUnqualifiedType() |
| 4511 | << QualsString |
| 4512 | << (QualsString.find(' ') == std::string::npos? 1 : 2); |
| 4513 | } |
| 4514 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4515 | CXXMemberCallExpr *TheCall |
Abramo Bagnara | 6c572f1 | 2010-12-03 21:39:42 +0000 | [diff] [blame] | 4516 | = new (Context) CXXMemberCallExpr(Context, Fn, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4517 | NumArgs, ResultTy, VK, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4518 | RParenLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4519 | |
| 4520 | if (CheckCallReturnType(FPT->getResultType(), |
| 4521 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4522 | TheCall, 0)) |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4523 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 4524 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4525 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4526 | RParenLoc)) |
| 4527 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4528 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4529 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4530 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4531 | return ExprError(Diag(Fn->getLocStart(), |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4532 | diag::err_typecheck_call_not_function) |
| 4533 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4538 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4540 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4541 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4542 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4543 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 4544 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 4545 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4546 | RParenLoc, ExecConfig); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4549 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4550 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4551 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4552 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4553 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4554 | if (isa<DeclRefExpr>(NakedFn)) |
| 4555 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 4556 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4557 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 4558 | ExecConfig); |
| 4559 | } |
| 4560 | |
| 4561 | ExprResult |
| 4562 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 4563 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 4564 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4565 | if (!ConfigDecl) |
| 4566 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4567 | << "cudaConfigureCall"); |
| 4568 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4569 | |
| 4570 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 4571 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 4572 | |
| 4573 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4576 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4577 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4578 | /// unary-convert to an expression of function-pointer or |
| 4579 | /// block-pointer type. |
| 4580 | /// |
| 4581 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4582 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4583 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4584 | SourceLocation LParenLoc, |
| 4585 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4586 | SourceLocation RParenLoc, |
| 4587 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4588 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 4589 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4590 | // Promote the function operand. |
| 4591 | UsualUnaryConversions(Fn); |
| 4592 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4593 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4594 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4595 | CallExpr *TheCall; |
| 4596 | if (Config) { |
| 4597 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4598 | cast<CallExpr>(Config), |
| 4599 | Args, NumArgs, |
| 4600 | Context.BoolTy, |
| 4601 | VK_RValue, |
| 4602 | RParenLoc); |
| 4603 | } else { |
| 4604 | TheCall = new (Context) CallExpr(Context, Fn, |
| 4605 | Args, NumArgs, |
| 4606 | Context.BoolTy, |
| 4607 | VK_RValue, |
| 4608 | RParenLoc); |
| 4609 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4610 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4611 | const FunctionType *FuncT; |
| 4612 | if (!Fn->getType()->isBlockPointerType()) { |
| 4613 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4614 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4615 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4616 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4617 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4618 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4619 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4620 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4621 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4622 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4623 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4624 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4625 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4626 | << Fn->getType() << Fn->getSourceRange()); |
| 4627 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4628 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4629 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4630 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4631 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4632 | return ExprError(); |
| 4633 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4634 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4635 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4636 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4638 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4639 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4640 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4641 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4642 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4643 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4645 | if (FDecl) { |
| 4646 | // Check if we have too few/too many template arguments, based |
| 4647 | // on our knowledge of the function definition. |
| 4648 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4649 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4650 | const FunctionProtoType *Proto |
| 4651 | = Def->getType()->getAs<FunctionProtoType>(); |
| 4652 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4653 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4654 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4655 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4656 | |
| 4657 | // If the function we're calling isn't a function prototype, but we have |
| 4658 | // a function prototype from a prior declaratiom, use that prototype. |
| 4659 | if (!FDecl->hasPrototype()) |
| 4660 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4663 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4664 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4665 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4666 | |
| 4667 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4668 | InitializedEntity Entity |
| 4669 | = InitializedEntity::InitializeParameter(Context, |
| 4670 | Proto->getArgType(i)); |
| 4671 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4672 | SourceLocation(), |
| 4673 | Owned(Arg)); |
| 4674 | if (ArgE.isInvalid()) |
| 4675 | return true; |
| 4676 | |
| 4677 | Arg = ArgE.takeAs<Expr>(); |
| 4678 | |
| 4679 | } else { |
| 4680 | DefaultArgumentPromotion(Arg); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4683 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4684 | Arg->getType(), |
| 4685 | PDiag(diag::err_call_incomplete_argument) |
| 4686 | << Arg->getSourceRange())) |
| 4687 | return ExprError(); |
| 4688 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4689 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4690 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4691 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4692 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4693 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4694 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4695 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4696 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4697 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4698 | // Check for sentinels |
| 4699 | if (NDecl) |
| 4700 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4702 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4703 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4704 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4705 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4707 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
| 4708 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4709 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4710 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4711 | return ExprError(); |
| 4712 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4713 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4714 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4717 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4718 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4719 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4720 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4721 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4722 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4723 | |
| 4724 | TypeSourceInfo *TInfo; |
| 4725 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4726 | if (!TInfo) |
| 4727 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4728 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4729 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4732 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4733 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4734 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4735 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4736 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4737 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4738 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 4739 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 4740 | << SourceRange(LParenLoc, |
| 4741 | literalExpr->getSourceRange().getEnd()))) |
| 4742 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4743 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4744 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 4745 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4746 | } else if (!literalType->isDependentType() && |
| 4747 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4748 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4750 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4751 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4753 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4754 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4755 | InitializationKind Kind |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4756 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4757 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4758 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4759 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4760 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4761 | &literalType); |
| 4762 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4763 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4764 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4765 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4766 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4767 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4768 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4769 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4770 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4771 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4772 | // In C, compound literals are l-values for some reason. |
| 4773 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 4774 | |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 4775 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4776 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4779 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4780 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4781 | SourceLocation RBraceLoc) { |
| 4782 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4783 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 4784 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4785 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4786 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4787 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4788 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 4789 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4790 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4791 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4794 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4795 | /// except the final cast and returning the kind required. |
| 4796 | static CastKind PrepareScalarCast(Sema &S, Expr *&Src, QualType DestTy) { |
| 4797 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4798 | // Also, callers should have filtered out the invalid cases with |
| 4799 | // pointers. Everything else should be possible. |
| 4800 | |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4801 | QualType SrcTy = Src->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4802 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4803 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4804 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4805 | switch (SrcTy->getScalarTypeKind()) { |
| 4806 | case Type::STK_MemberPointer: |
| 4807 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4808 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4809 | case Type::STK_Pointer: |
| 4810 | switch (DestTy->getScalarTypeKind()) { |
| 4811 | case Type::STK_Pointer: |
| 4812 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4813 | CK_AnyPointerToObjCPointerCast : |
| 4814 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4815 | case Type::STK_Bool: |
| 4816 | return CK_PointerToBoolean; |
| 4817 | case Type::STK_Integral: |
| 4818 | return CK_PointerToIntegral; |
| 4819 | case Type::STK_Floating: |
| 4820 | case Type::STK_FloatingComplex: |
| 4821 | case Type::STK_IntegralComplex: |
| 4822 | case Type::STK_MemberPointer: |
| 4823 | llvm_unreachable("illegal cast from pointer"); |
| 4824 | } |
| 4825 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4826 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4827 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4828 | case Type::STK_Integral: |
| 4829 | switch (DestTy->getScalarTypeKind()) { |
| 4830 | case Type::STK_Pointer: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4831 | if (Src->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4832 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4833 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4834 | case Type::STK_Bool: |
| 4835 | return CK_IntegralToBoolean; |
| 4836 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4837 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4838 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4839 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4840 | case Type::STK_IntegralComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4841 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4842 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4843 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4844 | case Type::STK_FloatingComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4845 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4846 | CK_IntegralToFloating); |
| 4847 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4848 | case Type::STK_MemberPointer: |
| 4849 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4850 | } |
| 4851 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4852 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4853 | case Type::STK_Floating: |
| 4854 | switch (DestTy->getScalarTypeKind()) { |
| 4855 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4856 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4857 | case Type::STK_Bool: |
| 4858 | return CK_FloatingToBoolean; |
| 4859 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4860 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4861 | case Type::STK_FloatingComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4862 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4863 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4864 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4865 | case Type::STK_IntegralComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4866 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4867 | CK_FloatingToIntegral); |
| 4868 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4869 | case Type::STK_Pointer: |
| 4870 | llvm_unreachable("valid float->pointer cast?"); |
| 4871 | case Type::STK_MemberPointer: |
| 4872 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4873 | } |
| 4874 | break; |
| 4875 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4876 | case Type::STK_FloatingComplex: |
| 4877 | switch (DestTy->getScalarTypeKind()) { |
| 4878 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4879 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4880 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4881 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4882 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4883 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4884 | if (S.Context.hasSameType(ET, DestTy)) |
| 4885 | return CK_FloatingComplexToReal; |
| 4886 | S.ImpCastExprToType(Src, ET, CK_FloatingComplexToReal); |
| 4887 | return CK_FloatingCast; |
| 4888 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4889 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4890 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4891 | case Type::STK_Integral: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4892 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4893 | CK_FloatingComplexToReal); |
| 4894 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4895 | case Type::STK_Pointer: |
| 4896 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4897 | case Type::STK_MemberPointer: |
| 4898 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4899 | } |
| 4900 | break; |
| 4901 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4902 | case Type::STK_IntegralComplex: |
| 4903 | switch (DestTy->getScalarTypeKind()) { |
| 4904 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4905 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4906 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4907 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4908 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4909 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4910 | if (S.Context.hasSameType(ET, DestTy)) |
| 4911 | return CK_IntegralComplexToReal; |
| 4912 | S.ImpCastExprToType(Src, ET, CK_IntegralComplexToReal); |
| 4913 | return CK_IntegralCast; |
| 4914 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4915 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4916 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4917 | case Type::STK_Floating: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4918 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4919 | CK_IntegralComplexToReal); |
| 4920 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4921 | case Type::STK_Pointer: |
| 4922 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4923 | case Type::STK_MemberPointer: |
| 4924 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4925 | } |
| 4926 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4927 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4928 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4929 | llvm_unreachable("Unhandled scalar cast"); |
| 4930 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4933 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4934 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 4935 | Expr *&castExpr, CastKind& Kind, ExprValueKind &VK, |
| 4936 | CXXCastPath &BasePath, bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4937 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 4938 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 4939 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4940 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4941 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4942 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4943 | // We only support r-value casts in C. |
| 4944 | VK = VK_RValue; |
| 4945 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4946 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4947 | // type needs to be scalar. |
| 4948 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4949 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| 4950 | IgnoredValueConversions(castExpr); |
| 4951 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4952 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4953 | Kind = CK_ToVoid; |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4954 | return false; |
| 4955 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4956 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4957 | DefaultFunctionArrayLvalueConversion(castExpr); |
| 4958 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4959 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 4960 | diag::err_typecheck_cast_to_incomplete)) |
| 4961 | return true; |
| 4962 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4963 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4964 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4965 | (castType->isStructureType() || castType->isUnionType())) { |
| 4966 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4967 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4968 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4969 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4970 | Kind = CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4971 | return false; |
| 4972 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4973 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4974 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4975 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4976 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4977 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4978 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4979 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4980 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4981 | castExpr->getType()) && |
| 4982 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4983 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4984 | << castExpr->getSourceRange(); |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | if (Field == FieldEnd) |
| 4989 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 4990 | << castExpr->getType() << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4991 | Kind = CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4992 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4993 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4994 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4995 | // Reject any other conversions to non-scalar types. |
| 4996 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 4997 | << castType << castExpr->getSourceRange(); |
| 4998 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4999 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5000 | // The type we're casting to is known to be a scalar or vector. |
| 5001 | |
| 5002 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5003 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5004 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5005 | return Diag(castExpr->getLocStart(), |
| 5006 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5007 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5008 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5009 | |
| 5010 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5011 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5012 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5013 | if (castType->isVectorType()) |
| 5014 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 5015 | if (castExpr->getType()->isVectorType()) |
| 5016 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 5017 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5018 | // The source and target types are both scalars, i.e. |
| 5019 | // - arithmetic types (fundamental, enum, and complex) |
| 5020 | // - all kinds of pointers |
| 5021 | // Note that member pointers were filtered out with C++, above. |
| 5022 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5023 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 5024 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5025 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5026 | // If either type is a pointer, the other type has to be either an |
| 5027 | // integer or a pointer. |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5028 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5029 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5030 | if (!castExprType->isIntegralType(Context) && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5031 | castExprType->isArithmeticType()) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5032 | return Diag(castExpr->getLocStart(), |
| 5033 | diag::err_cast_pointer_from_non_pointer_int) |
| 5034 | << castExprType << castExpr->getSourceRange(); |
| 5035 | } else if (!castExpr->getType()->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5036 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5037 | return Diag(castExpr->getLocStart(), |
| 5038 | diag::err_cast_pointer_to_non_pointer_int) |
| 5039 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5040 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5041 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5042 | Kind = PrepareScalarCast(*this, castExpr, castType); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5043 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5044 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5045 | CheckCastAlign(castExpr, castType, TyR); |
| 5046 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5047 | return false; |
| 5048 | } |
| 5049 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5050 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5051 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5052 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5053 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5054 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5055 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5056 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5057 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5058 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5059 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5060 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5061 | } else |
| 5062 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5063 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5064 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5065 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5066 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5067 | return false; |
| 5068 | } |
| 5069 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5070 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5071 | CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5072 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5073 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5074 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5075 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5076 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5077 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5078 | if (SrcTy->isVectorType()) { |
| 5079 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 5080 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 5081 | << DestTy << SrcTy << R; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5082 | Kind = CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5083 | return false; |
| 5084 | } |
| 5085 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5086 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5087 | // conversion will take place first from scalar to elt type, and then |
| 5088 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5089 | if (SrcTy->isPointerType()) |
| 5090 | return Diag(R.getBegin(), |
| 5091 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5092 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5093 | |
| 5094 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 5095 | ImpCastExprToType(CastExpr, DestElemTy, |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5096 | PrepareScalarCast(*this, CastExpr, DestElemTy)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5097 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5098 | Kind = CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5099 | return false; |
| 5100 | } |
| 5101 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5102 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5103 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5104 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5105 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5106 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5107 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5108 | TypeSourceInfo *castTInfo; |
| 5109 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5110 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5111 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5112 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5113 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5114 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5115 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5116 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5117 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5118 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5121 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5122 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5123 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5124 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5125 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5126 | CXXCastPath BasePath; |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5127 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5128 | Kind, VK, BasePath)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5129 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5130 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5131 | return Owned(CStyleCastExpr::Create(Context, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 5132 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5133 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5134 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5137 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5138 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5139 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5140 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5141 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5142 | if (!E) |
| 5143 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5144 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5145 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5146 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5147 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5148 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5149 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5150 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5151 | if (Result.isInvalid()) return ExprError(); |
| 5152 | |
| 5153 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5156 | ExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5157 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5158 | SourceLocation RParenLoc, Expr *Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5159 | TypeSourceInfo *TInfo) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5160 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5161 | QualType Ty = TInfo->getType(); |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5162 | bool isAltiVecLiteral = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5163 | |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5164 | // Check for an altivec literal, |
| 5165 | // i.e. all the elements are integer constants. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5166 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5167 | if (PE->getNumExprs() == 0) { |
| 5168 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5169 | return ExprError(); |
| 5170 | } |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5171 | if (PE->getNumExprs() == 1) { |
| 5172 | if (!PE->getExpr(0)->getType()->isVectorType()) |
| 5173 | isAltiVecLiteral = true; |
| 5174 | } |
| 5175 | else |
| 5176 | isAltiVecLiteral = true; |
| 5177 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5178 | |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5179 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
| 5180 | // then handle it as such. |
| 5181 | if (isAltiVecLiteral) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5182 | llvm::SmallVector<Expr *, 8> initExprs; |
| 5183 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5184 | initExprs.push_back(PE->getExpr(i)); |
| 5185 | |
| 5186 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5187 | // braces instead of the original commas. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5188 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5189 | &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5190 | initExprs.size(), RParenLoc); |
| 5191 | E->setType(Ty); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5192 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5193 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5194 | // 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] | 5195 | // sequence of BinOp comma operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5196 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5197 | if (Result.isInvalid()) return ExprError(); |
| 5198 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5199 | } |
| 5200 | } |
| 5201 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5202 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5203 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5204 | MultiExprArg Val, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5205 | ParsedType TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5206 | unsigned nexprs = Val.size(); |
| 5207 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5208 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5209 | Expr *expr; |
| 5210 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5211 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5212 | else |
| 5213 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5214 | return Owned(expr); |
| 5215 | } |
| 5216 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5217 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5218 | /// constant and the other is not a pointer. |
| 5219 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5220 | SourceLocation QuestionLoc) { |
| 5221 | Expr *NullExpr = LHS; |
| 5222 | Expr *NonPointerExpr = RHS; |
| 5223 | Expr::NullPointerConstantKind NullKind = |
| 5224 | NullExpr->isNullPointerConstant(Context, |
| 5225 | Expr::NPC_ValueDependentIsNotNull); |
| 5226 | |
| 5227 | if (NullKind == Expr::NPCK_NotNull) { |
| 5228 | NullExpr = RHS; |
| 5229 | NonPointerExpr = LHS; |
| 5230 | NullKind = |
| 5231 | NullExpr->isNullPointerConstant(Context, |
| 5232 | Expr::NPC_ValueDependentIsNotNull); |
| 5233 | } |
| 5234 | |
| 5235 | if (NullKind == Expr::NPCK_NotNull) |
| 5236 | return false; |
| 5237 | |
| 5238 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5239 | // In this case, check to make sure that we got here from a "NULL" |
| 5240 | // string in the source code. |
| 5241 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
| 5242 | SourceManager& SM = Context.getSourceManager(); |
| 5243 | SourceLocation Loc = SM.getInstantiationLoc(NullExpr->getExprLoc()); |
| 5244 | unsigned Len = |
| 5245 | Lexer::MeasureTokenLength(Loc, SM, Context.getLangOptions()); |
| 5246 | if (Len != 4 || memcmp(SM.getCharacterData(Loc), "NULL", 4)) |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
| 5250 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5251 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5252 | << NonPointerExpr->getType() << DiagType |
| 5253 | << NonPointerExpr->getSourceRange(); |
| 5254 | return true; |
| 5255 | } |
| 5256 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5257 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5258 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5259 | /// C99 6.5.15 |
| 5260 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5261 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5262 | SourceLocation QuestionLoc) { |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5263 | // If both LHS and RHS are overloaded functions, try to resolve them. |
| 5264 | if (Context.hasSameType(LHS->getType(), RHS->getType()) && |
| 5265 | LHS->getType()->isSpecificBuiltinType(BuiltinType::Overload)) { |
| 5266 | ExprResult LHSResult = CheckPlaceholderExpr(LHS, QuestionLoc); |
| 5267 | if (LHSResult.isInvalid()) |
| 5268 | return QualType(); |
| 5269 | |
| 5270 | ExprResult RHSResult = CheckPlaceholderExpr(RHS, QuestionLoc); |
| 5271 | if (RHSResult.isInvalid()) |
| 5272 | return QualType(); |
| 5273 | |
| 5274 | LHS = LHSResult.take(); |
| 5275 | RHS = RHSResult.take(); |
| 5276 | } |
| 5277 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5278 | // C++ is sufficiently different to merit its own checker. |
| 5279 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5280 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5281 | |
| 5282 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5283 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5285 | UsualUnaryConversions(Cond); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5286 | UsualUnaryConversions(LHS); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5287 | UsualUnaryConversions(RHS); |
| 5288 | QualType CondTy = Cond->getType(); |
| 5289 | QualType LHSTy = LHS->getType(); |
| 5290 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5292 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5293 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5294 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5295 | // Throw an error if its not either. |
| 5296 | if (getLangOptions().OpenCL) { |
| 5297 | if (!CondTy->isVectorType()) { |
| 5298 | Diag(Cond->getLocStart(), |
| 5299 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5300 | << CondTy; |
| 5301 | return QualType(); |
| 5302 | } |
| 5303 | } |
| 5304 | else { |
| 5305 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5306 | << CondTy; |
| 5307 | return QualType(); |
| 5308 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5309 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5310 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5311 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5312 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5313 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5314 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5315 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5316 | // attempt to implicity convert them to the vector type to act like the |
| 5317 | // built in select. |
| 5318 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5319 | // Both operands should be of scalar type. |
| 5320 | if (!LHSTy->isScalarType()) { |
| 5321 | Diag(LHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5322 | << CondTy; |
| 5323 | return QualType(); |
| 5324 | } |
| 5325 | if (!RHSTy->isScalarType()) { |
| 5326 | Diag(RHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5327 | << CondTy; |
| 5328 | return QualType(); |
| 5329 | } |
| 5330 | // Implicity convert these scalars to the type of the condition. |
| 5331 | ImpCastExprToType(LHS, CondTy, CK_IntegralCast); |
| 5332 | ImpCastExprToType(RHS, CondTy, CK_IntegralCast); |
| 5333 | } |
| 5334 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5335 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5336 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5337 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5338 | UsualArithmeticConversions(LHS, RHS); |
| 5339 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5340 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5341 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5342 | // If both operands are the same structure or union type, the result is that |
| 5343 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5344 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5345 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5346 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5347 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5348 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5349 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5350 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5351 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5352 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5353 | // 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] | 5354 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5355 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5356 | if (!LHSTy->isVoidType()) |
| 5357 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5358 | << RHS->getSourceRange(); |
| 5359 | if (!RHSTy->isVoidType()) |
| 5360 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5361 | << LHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5362 | ImpCastExprToType(LHS, Context.VoidTy, CK_ToVoid); |
| 5363 | ImpCastExprToType(RHS, Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5364 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5365 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5366 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5367 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5368 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5369 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5370 | // promote the null to a pointer. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5371 | ImpCastExprToType(RHS, LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5372 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5373 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5374 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5375 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5376 | ImpCastExprToType(LHS, RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5377 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5378 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5379 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5380 | // All objective-c pointer type analysis is done here. |
| 5381 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5382 | QuestionLoc); |
| 5383 | if (!compositeType.isNull()) |
| 5384 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5385 | |
| 5386 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5387 | // Handle block pointer types. |
| 5388 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5389 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5390 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5391 | QualType destType = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5392 | ImpCastExprToType(LHS, destType, CK_BitCast); |
| 5393 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5394 | return destType; |
| 5395 | } |
| 5396 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5397 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5398 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5399 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5400 | // We have 2 block pointer types. |
| 5401 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5402 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5403 | return LHSTy; |
| 5404 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5405 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5406 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5407 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5408 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5409 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5410 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5411 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5412 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5413 | // In this situation, we assume void* type. No especially good |
| 5414 | // reason, but this is what gcc does, and we do have to pick |
| 5415 | // to get a consistent AST. |
| 5416 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5417 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5418 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5419 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5420 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5421 | // The block pointer types are compatible. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5422 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5423 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5424 | return LHSTy; |
| 5425 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5426 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5427 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5428 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5429 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5430 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5431 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5432 | |
| 5433 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5434 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5435 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5436 | QualType destPointee |
| 5437 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5438 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5439 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5440 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5441 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5442 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5443 | return destType; |
| 5444 | } |
| 5445 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5446 | QualType destPointee |
| 5447 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5448 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5449 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5450 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5451 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5452 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5453 | return destType; |
| 5454 | } |
| 5455 | |
| 5456 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5457 | // Two identical pointer types are always compatible. |
| 5458 | return LHSTy; |
| 5459 | } |
| 5460 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5461 | rhptee.getUnqualifiedType())) { |
| 5462 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 5463 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5464 | // In this situation, we assume void* type. No especially good |
| 5465 | // reason, but this is what gcc does, and we do have to pick |
| 5466 | // to get a consistent AST. |
| 5467 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5468 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5469 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5470 | return incompatTy; |
| 5471 | } |
| 5472 | // The pointer types are compatible. |
| 5473 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 5474 | // differently qualified versions of compatible types, the result type is |
| 5475 | // a pointer to an appropriately qualified version of the *composite* |
| 5476 | // type. |
| 5477 | // FIXME: Need to calculate the composite type. |
| 5478 | // FIXME: Need to add qualifiers |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5479 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5480 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5481 | return LHSTy; |
| 5482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5484 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5485 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5486 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 5487 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5488 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5489 | ImpCastExprToType(LHS, RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5490 | return RHSTy; |
| 5491 | } |
| 5492 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 5493 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5494 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5495 | ImpCastExprToType(RHS, LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5496 | return LHSTy; |
| 5497 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5498 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5499 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5500 | // constant and the other is not a pointer type. In this case, the user most |
| 5501 | // likely forgot to take the address of the other expression. |
| 5502 | if (DiagnoseConditionalForNull(LHS, RHS, QuestionLoc)) |
| 5503 | return QualType(); |
| 5504 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5505 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5506 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 5507 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5508 | return QualType(); |
| 5509 | } |
| 5510 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5511 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5512 | /// two objective-c pointer types of the two input expressions. |
| 5513 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 5514 | SourceLocation QuestionLoc) { |
| 5515 | QualType LHSTy = LHS->getType(); |
| 5516 | QualType RHSTy = RHS->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5517 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5518 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5519 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5520 | // redefinition type if an attempt is made to access its fields. |
| 5521 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5522 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5523 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5524 | return LHSTy; |
| 5525 | } |
| 5526 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5527 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5528 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5529 | return RHSTy; |
| 5530 | } |
| 5531 | // And the same for struct objc_object* / id |
| 5532 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5533 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5534 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5535 | return LHSTy; |
| 5536 | } |
| 5537 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5538 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5539 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5540 | return RHSTy; |
| 5541 | } |
| 5542 | // And the same for struct objc_selector* / SEL |
| 5543 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5544 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5545 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5546 | return LHSTy; |
| 5547 | } |
| 5548 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5549 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5550 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5551 | return RHSTy; |
| 5552 | } |
| 5553 | // Check constraints for Objective-C object pointers types. |
| 5554 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5555 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5556 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5557 | // Two identical object pointer types are always compatible. |
| 5558 | return LHSTy; |
| 5559 | } |
| 5560 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 5561 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 5562 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5563 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5564 | // If both operands are interfaces and either operand can be |
| 5565 | // assigned to the other, use that type as the composite |
| 5566 | // type. This allows |
| 5567 | // xxx ? (A*) a : (B*) b |
| 5568 | // where B is a subclass of A. |
| 5569 | // |
| 5570 | // Additionally, as for assignment, if either type is 'id' |
| 5571 | // allow silent coercion. Finally, if the types are |
| 5572 | // incompatible then make sure to use 'id' as the composite |
| 5573 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5574 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5575 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5576 | // It could return the composite type. |
| 5577 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5578 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5579 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5580 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5581 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5582 | RHSTy->isObjCQualifiedIdType()) && |
| 5583 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5584 | // Need to handle "id<xx>" explicitly. |
| 5585 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5586 | // id. Currently localizing to here until clear this should be |
| 5587 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5588 | compositeType = Context.getObjCIdType(); |
| 5589 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5590 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5591 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5592 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5593 | ; |
| 5594 | else { |
| 5595 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5596 | << LHSTy << RHSTy |
| 5597 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5598 | QualType incompatTy = Context.getObjCIdType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5599 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5600 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5601 | return incompatTy; |
| 5602 | } |
| 5603 | // The object pointer types are compatible. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5604 | ImpCastExprToType(LHS, compositeType, CK_BitCast); |
| 5605 | ImpCastExprToType(RHS, compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5606 | return compositeType; |
| 5607 | } |
| 5608 | // Check Objective-C object pointer types and 'void *' |
| 5609 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 5610 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5611 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5612 | QualType destPointee |
| 5613 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5614 | QualType destType = Context.getPointerType(destPointee); |
| 5615 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5616 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5617 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5618 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5619 | return destType; |
| 5620 | } |
| 5621 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 5622 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5623 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5624 | QualType destPointee |
| 5625 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5626 | QualType destType = Context.getPointerType(destPointee); |
| 5627 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5628 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5629 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5630 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5631 | return destType; |
| 5632 | } |
| 5633 | return QualType(); |
| 5634 | } |
| 5635 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5636 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5637 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5638 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5639 | SourceLocation ColonLoc, |
| 5640 | Expr *CondExpr, Expr *LHSExpr, |
| 5641 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5642 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5643 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5644 | OpaqueValueExpr *opaqueValue = 0; |
| 5645 | Expr *commonExpr = 0; |
| 5646 | if (LHSExpr == 0) { |
| 5647 | commonExpr = CondExpr; |
| 5648 | |
| 5649 | // We usually want to apply unary conversions *before* saving, except |
| 5650 | // in the special case of a C++ l-value conditional. |
| 5651 | if (!(getLangOptions().CPlusPlus |
| 5652 | && !commonExpr->isTypeDependent() |
| 5653 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5654 | && commonExpr->isGLValue() |
| 5655 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5656 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5657 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
| 5658 | UsualUnaryConversions(commonExpr); |
| 5659 | } |
| 5660 | |
| 5661 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5662 | commonExpr->getType(), |
| 5663 | commonExpr->getValueKind(), |
| 5664 | commonExpr->getObjectKind()); |
| 5665 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5666 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5667 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5668 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5669 | ExprObjectKind OK = OK_Ordinary; |
Fariborz Jahanian | 1fb019b | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 5670 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, RHSExpr, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5671 | VK, OK, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5672 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5673 | return ExprError(); |
| 5674 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5675 | if (!commonExpr) |
| 5676 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
| 5677 | LHSExpr, ColonLoc, |
| 5678 | RHSExpr, result, VK, OK)); |
| 5679 | |
| 5680 | return Owned(new (Context) |
| 5681 | BinaryConditionalOperator(commonExpr, opaqueValue, CondExpr, LHSExpr, |
| 5682 | RHSExpr, QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5685 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5686 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5687 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5688 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5689 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5690 | static Sema::AssignConvertType |
| 5691 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5692 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5693 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5694 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5695 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5696 | const Type *lhptee, *rhptee; |
| 5697 | Qualifiers lhq, rhq; |
| 5698 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 5699 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5700 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5701 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5702 | |
| 5703 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5704 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5705 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5706 | Qualifiers lq; |
| 5707 | |
| 5708 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5709 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5710 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5711 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5712 | |
| 5713 | // For GCC compatibility, other qualifier mismatches are treated |
| 5714 | // as still compatible in C. |
| 5715 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5716 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5717 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5718 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5719 | // 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] | 5720 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5721 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5722 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5723 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5724 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5725 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5726 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5727 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5728 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5729 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5730 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5731 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5732 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5733 | |
| 5734 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5735 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5736 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5737 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5738 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5739 | // 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] | 5740 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5741 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5742 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5743 | // Check if the pointee types are compatible ignoring the sign. |
| 5744 | // We explicitly check for char so that we catch "char" vs |
| 5745 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5746 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5747 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5748 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5749 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5750 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5751 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5752 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5753 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5754 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5755 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5756 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5757 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5758 | // takes priority over sign incompatibility because the sign |
| 5759 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5760 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5761 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5762 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5763 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5764 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5765 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5766 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5767 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5768 | // the eventual target type is the same and the pointers have the same |
| 5769 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5770 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5771 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5772 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5773 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5774 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5775 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5776 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5777 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5778 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5779 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5780 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5781 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5782 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5783 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5786 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5787 | /// block pointer types are compatible or whether a block and normal pointer |
| 5788 | /// are compatible. It is more restrict than comparing two function pointer |
| 5789 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5790 | static Sema::AssignConvertType |
| 5791 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5792 | QualType rhsType) { |
| 5793 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5794 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5795 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5796 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5797 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5798 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5799 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5800 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5801 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5802 | // In C++, the types have to match exactly. |
| 5803 | if (S.getLangOptions().CPlusPlus) |
| 5804 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5805 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5806 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5807 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5808 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5809 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5810 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5811 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5812 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5813 | return Sema::IncompatibleBlockPointer; |
| 5814 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5815 | return ConvTy; |
| 5816 | } |
| 5817 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5818 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5819 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5820 | static Sema::AssignConvertType |
| 5821 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5822 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5823 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5824 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5825 | if (lhsType->isObjCBuiltinType()) { |
| 5826 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5827 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5828 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5829 | return Sema::IncompatiblePointer; |
| 5830 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5831 | } |
| 5832 | if (rhsType->isObjCBuiltinType()) { |
| 5833 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5834 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5835 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5836 | return Sema::IncompatiblePointer; |
| 5837 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5838 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5839 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5840 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5841 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5842 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5843 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5844 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5845 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5846 | |
| 5847 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5848 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5849 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5850 | return Sema::IncompatibleObjCQualifiedId; |
| 5851 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5854 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5855 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5856 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5857 | // Fake up an opaque expression. We don't actually care about what |
| 5858 | // cast operations are required, so if CheckAssignmentConstraints |
| 5859 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5860 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5861 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5862 | Expr *rhsPtr = &rhs; |
| 5863 | CastKind K = CK_Invalid; |
| 5864 | |
| 5865 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5866 | } |
| 5867 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5868 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5869 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5870 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5871 | /// |
| 5872 | /// int a, *pint; |
| 5873 | /// short *pshort; |
| 5874 | /// struct foo *pfoo; |
| 5875 | /// |
| 5876 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5877 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5878 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5879 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5880 | /// |
| 5881 | /// 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] | 5882 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5883 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5884 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5885 | Sema::AssignConvertType |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5886 | Sema::CheckAssignmentConstraints(QualType lhsType, Expr *&rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5887 | CastKind &Kind) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5888 | QualType rhsType = rhs->getType(); |
| 5889 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5890 | // Get canonical types. We're not formatting these types, just comparing |
| 5891 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5892 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5893 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5894 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5895 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5896 | if (lhsType == rhsType) { |
| 5897 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5898 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5901 | // If the left-hand side is a reference type, then we are in a |
| 5902 | // (rare!) case where we've allowed the use of references in C, |
| 5903 | // e.g., as a parameter type in a built-in function. In this case, |
| 5904 | // just make sure that the type referenced is compatible with the |
| 5905 | // right-hand side type. The caller is responsible for adjusting |
| 5906 | // lhsType so that the resulting expression does not have reference |
| 5907 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5908 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5909 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5910 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5911 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5912 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5913 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5914 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5915 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5916 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5917 | // to the same ExtVector type. |
| 5918 | if (lhsType->isExtVectorType()) { |
| 5919 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5920 | return Incompatible; |
| 5921 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5922 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5923 | // element type. |
| 5924 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5925 | if (elType != rhsType) { |
| 5926 | Kind = PrepareScalarCast(*this, rhs, elType); |
| 5927 | ImpCastExprToType(rhs, elType, Kind); |
| 5928 | } |
| 5929 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5930 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5931 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5932 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5933 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5934 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5935 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5936 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5937 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5938 | // vector type and vice versa |
| 5939 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5940 | Kind = CK_BitCast; |
| 5941 | return Compatible; |
| 5942 | } |
| 5943 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5944 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5945 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5946 | // no bits are changed but the result type is different. |
| 5947 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5948 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5949 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5950 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5951 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5952 | } |
| 5953 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5954 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5955 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5956 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5957 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5958 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5959 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5960 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5961 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5962 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5963 | // Conversions to normal pointers. |
| 5964 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 5965 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5966 | if (isa<PointerType>(rhsType)) { |
| 5967 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5968 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5969 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5970 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5971 | // int -> T* |
| 5972 | if (rhsType->isIntegerType()) { |
| 5973 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5974 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5975 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5976 | |
| 5977 | // C pointers are not compatible with ObjC object pointers, |
| 5978 | // with two exceptions: |
| 5979 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5980 | // - conversions to void* |
| 5981 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5982 | Kind = CK_AnyPointerToObjCPointerCast; |
| 5983 | return Compatible; |
| 5984 | } |
| 5985 | |
| 5986 | // - conversions from 'Class' to the redefinition type |
| 5987 | if (rhsType->isObjCClassType() && |
| 5988 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5989 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5990 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5991 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5992 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5993 | Kind = CK_BitCast; |
| 5994 | return IncompatiblePointer; |
| 5995 | } |
| 5996 | |
| 5997 | // U^ -> void* |
| 5998 | if (rhsType->getAs<BlockPointerType>()) { |
| 5999 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6000 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6001 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6002 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6003 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6004 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6005 | return Incompatible; |
| 6006 | } |
| 6007 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6008 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6009 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6010 | // U^ -> T^ |
| 6011 | if (rhsType->isBlockPointerType()) { |
| 6012 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6013 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6017 | if (rhsType->isIntegerType()) { |
| 6018 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6019 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6022 | // id -> T^ |
| 6023 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6024 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6025 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6026 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6027 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6028 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6029 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6030 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6031 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6032 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6033 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6034 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6035 | return Incompatible; |
| 6036 | } |
| 6037 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6038 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6039 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6040 | // A* -> B* |
| 6041 | if (rhsType->isObjCObjectPointerType()) { |
| 6042 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6043 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6047 | if (rhsType->isIntegerType()) { |
| 6048 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6049 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6052 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6053 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6054 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6055 | // - conversions from 'void*' |
| 6056 | if (rhsType->isVoidPointerType()) { |
| 6057 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6058 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | // - conversions to 'Class' from its redefinition type |
| 6062 | if (lhsType->isObjCClassType() && |
| 6063 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6064 | Kind = CK_BitCast; |
| 6065 | return Compatible; |
| 6066 | } |
| 6067 | |
| 6068 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6069 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6070 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6071 | |
| 6072 | // T^ -> A* |
| 6073 | if (rhsType->isBlockPointerType()) { |
| 6074 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6075 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6078 | return Incompatible; |
| 6079 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6080 | |
| 6081 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6082 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6083 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6084 | if (lhsType == Context.BoolTy) { |
| 6085 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6086 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6087 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6088 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6089 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6090 | if (lhsType->isIntegerType()) { |
| 6091 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6092 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6093 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6094 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6095 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6096 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6097 | |
| 6098 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6099 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6100 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6101 | if (lhsType == Context.BoolTy) { |
| 6102 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6103 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6104 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6105 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6106 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6107 | if (lhsType->isIntegerType()) { |
| 6108 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6109 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6110 | } |
| 6111 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6112 | return Incompatible; |
| 6113 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6114 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6115 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6116 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6117 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6118 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6119 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6120 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6121 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6123 | return Incompatible; |
| 6124 | } |
| 6125 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6126 | /// \brief Constructs a transparent union from an expression that is |
| 6127 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6128 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6129 | QualType UnionType, FieldDecl *Field) { |
| 6130 | // Build an initializer list that designates the appropriate member |
| 6131 | // of the transparent union. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6132 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6133 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6134 | SourceLocation()); |
| 6135 | Initializer->setType(UnionType); |
| 6136 | Initializer->setInitializedFieldInUnion(Field); |
| 6137 | |
| 6138 | // Build a compound literal constructing a value of the transparent |
| 6139 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6140 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 6141 | E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6142 | VK_RValue, Initializer, false); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | Sema::AssignConvertType |
| 6146 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 6147 | QualType FromType = rExpr->getType(); |
| 6148 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | // 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] | 6150 | // transparent_union GCC extension. |
| 6151 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6152 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6153 | return Incompatible; |
| 6154 | |
| 6155 | // The field to initialize within the transparent union. |
| 6156 | RecordDecl *UD = UT->getDecl(); |
| 6157 | FieldDecl *InitField = 0; |
| 6158 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6159 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6160 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6161 | it != itend; ++it) { |
| 6162 | if (it->getType()->isPointerType()) { |
| 6163 | // If the transparent union contains a pointer type, we allow: |
| 6164 | // 1) void pointer |
| 6165 | // 2) null pointer constant |
| 6166 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6167 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6168 | ImpCastExprToType(rExpr, it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6169 | InitField = *it; |
| 6170 | break; |
| 6171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6173 | if (rExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6174 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6175 | ImpCastExprToType(rExpr, it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6176 | InitField = *it; |
| 6177 | break; |
| 6178 | } |
| 6179 | } |
| 6180 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6181 | Expr *rhs = rExpr; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6182 | CastKind Kind = CK_Invalid; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6183 | if (CheckAssignmentConstraints(it->getType(), rhs, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6184 | == Compatible) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6185 | ImpCastExprToType(rhs, it->getType(), Kind); |
| 6186 | rExpr = rhs; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6187 | InitField = *it; |
| 6188 | break; |
| 6189 | } |
| 6190 | } |
| 6191 | |
| 6192 | if (!InitField) |
| 6193 | return Incompatible; |
| 6194 | |
| 6195 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 6196 | return Compatible; |
| 6197 | } |
| 6198 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6199 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6200 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6201 | if (getLangOptions().CPlusPlus) { |
| 6202 | if (!lhsType->isRecordType()) { |
| 6203 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6204 | // expression is implicitly converted (C++ 4) to the |
| 6205 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 6206 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6207 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6208 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6209 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6210 | } |
| 6211 | |
| 6212 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6213 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6214 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6215 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6216 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6217 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6218 | if ((lhsType->isPointerType() || |
| 6219 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6220 | lhsType->isBlockPointerType()) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6221 | && rExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6222 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6223 | ImpCastExprToType(rExpr, lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6224 | return Compatible; |
| 6225 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6226 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6227 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6228 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6229 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6230 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6231 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6232 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6233 | if (!lhsType->isReferenceType()) |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6234 | DefaultFunctionArrayLvalueConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6235 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6236 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6237 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6238 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6239 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6240 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6241 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6242 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6243 | // so that we can use references in built-in functions even in C. |
| 6244 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6245 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6246 | if (result != Incompatible && rExpr->getType() != lhsType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6247 | ImpCastExprToType(rExpr, lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6248 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6251 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6252 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6253 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6254 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6255 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6258 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6259 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6260 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6261 | QualType lhsType = |
| 6262 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 6263 | QualType rhsType = |
| 6264 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6265 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6266 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6267 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6268 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6269 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6270 | // Handle the case of a vector & extvector type of the same size and element |
| 6271 | // 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] | 6272 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6273 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6274 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6275 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6276 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6277 | if (lhsType->isExtVectorType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6278 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6279 | return lhsType; |
| 6280 | } |
| 6281 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6282 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6283 | return rhsType; |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6284 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 6285 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6286 | // vectors, the total size only needs to be the same. This is a |
| 6287 | // bitcast; no bits are changed but the result type is different. |
| 6288 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
| 6289 | return lhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6290 | } |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6291 | } |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6292 | } |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6293 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6294 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6295 | // Handle the case of equivalent AltiVec and GCC vector types |
| 6296 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 6297 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6298 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6299 | return rhsType; |
| 6300 | } |
| 6301 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6302 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6303 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6304 | bool swapped = false; |
| 6305 | if (rhsType->isExtVectorType()) { |
| 6306 | swapped = true; |
| 6307 | std::swap(rex, lex); |
| 6308 | std::swap(rhsType, lhsType); |
| 6309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6311 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6312 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6313 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 6314 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6315 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 6316 | if (order > 0) |
| 6317 | ImpCastExprToType(rex, EltTy, CK_IntegralCast); |
| 6318 | if (order >= 0) { |
| 6319 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6320 | if (swapped) std::swap(rex, lex); |
| 6321 | return lhsType; |
| 6322 | } |
| 6323 | } |
| 6324 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 6325 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6326 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 6327 | if (order > 0) |
| 6328 | ImpCastExprToType(rex, EltTy, CK_FloatingCast); |
| 6329 | if (order >= 0) { |
| 6330 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6331 | if (swapped) std::swap(rex, lex); |
| 6332 | return lhsType; |
| 6333 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6334 | } |
| 6335 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6337 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6338 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6339 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6340 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6341 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6344 | QualType Sema::CheckMultiplyDivideOperands( |
| 6345 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 6346 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6347 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6348 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6349 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6350 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6351 | if (!lex->getType()->isArithmeticType() || |
| 6352 | !rex->getType()->isArithmeticType()) |
| 6353 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6354 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6355 | // Check for division by zero. |
| 6356 | if (isDiv && |
| 6357 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 6358 | DiagRuntimeBehavior(Loc, rex, PDiag(diag::warn_division_by_zero) |
| 6359 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6360 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6361 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6364 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6365 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6366 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6367 | if (lex->getType()->hasIntegerRepresentation() && |
| 6368 | rex->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6369 | return CheckVectorOperands(Loc, lex, rex); |
| 6370 | return InvalidOperands(Loc, lex, rex); |
| 6371 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6372 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6373 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6374 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6375 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 6376 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6377 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6378 | // Check for remainder by zero. |
| 6379 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 6380 | DiagRuntimeBehavior(Loc, rex, PDiag(diag::warn_remainder_by_zero) |
| 6381 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6382 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6383 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6386 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6387 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6388 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6389 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6390 | if (CompLHSTy) *CompLHSTy = compType; |
| 6391 | return compType; |
| 6392 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6393 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6394 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6396 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6397 | if (lex->getType()->isArithmeticType() && |
| 6398 | rex->getType()->isArithmeticType()) { |
| 6399 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6400 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6401 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6402 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6403 | // Put any potential pointer into PExp |
| 6404 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6405 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6406 | std::swap(PExp, IExp); |
| 6407 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6408 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6409 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6410 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6411 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6412 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6413 | // Check for arithmetic on pointers to incomplete types. |
| 6414 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6415 | if (getLangOptions().CPlusPlus) { |
| 6416 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6417 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6418 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6419 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6420 | |
| 6421 | // GNU extension: arithmetic on pointer to void |
| 6422 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6423 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6424 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6425 | if (getLangOptions().CPlusPlus) { |
| 6426 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 6427 | << lex->getType() << lex->getSourceRange(); |
| 6428 | return QualType(); |
| 6429 | } |
| 6430 | |
| 6431 | // GNU extension: arithmetic on pointer to function |
| 6432 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 6433 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6434 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6435 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6436 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6437 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6438 | PExp->getType()->isObjCObjectPointerType()) && |
| 6439 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 6441 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6442 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6443 | return QualType(); |
| 6444 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6445 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6446 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6447 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6448 | << PointeeTy << PExp->getSourceRange(); |
| 6449 | return QualType(); |
| 6450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6451 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6452 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6453 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 6454 | if (LHSTy.isNull()) { |
| 6455 | LHSTy = lex->getType(); |
| 6456 | if (LHSTy->isPromotableIntegerType()) |
| 6457 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6458 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6459 | *CompLHSTy = LHSTy; |
| 6460 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6461 | return PExp->getType(); |
| 6462 | } |
| 6463 | } |
| 6464 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6465 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6468 | // C99 6.5.6 |
| 6469 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6470 | SourceLocation Loc, QualType* CompLHSTy) { |
| 6471 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6472 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6473 | if (CompLHSTy) *CompLHSTy = compType; |
| 6474 | return compType; |
| 6475 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6476 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6477 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6478 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6479 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6480 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6481 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6482 | if (lex->getType()->isArithmeticType() |
| 6483 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6484 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6485 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6487 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6488 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6489 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 6490 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6491 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6492 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6493 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6494 | bool ComplainAboutVoid = false; |
| 6495 | Expr *ComplainAboutFunc = 0; |
| 6496 | if (lpointee->isVoidType()) { |
| 6497 | if (getLangOptions().CPlusPlus) { |
| 6498 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6499 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6500 | return QualType(); |
| 6501 | } |
| 6502 | |
| 6503 | // GNU C extension: arithmetic on pointer to void |
| 6504 | ComplainAboutVoid = true; |
| 6505 | } else if (lpointee->isFunctionType()) { |
| 6506 | if (getLangOptions().CPlusPlus) { |
| 6507 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6508 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6509 | return QualType(); |
| 6510 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6511 | |
| 6512 | // GNU C extension: arithmetic on pointer to function |
| 6513 | ComplainAboutFunc = lex; |
| 6514 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6515 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6516 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6517 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6518 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6519 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6520 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6521 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6522 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6523 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6524 | << lpointee << lex->getSourceRange(); |
| 6525 | return QualType(); |
| 6526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6527 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6528 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6529 | if (rex->getType()->isIntegerType()) { |
| 6530 | if (ComplainAboutVoid) |
| 6531 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6532 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6533 | if (ComplainAboutFunc) |
| 6534 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6535 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6536 | << ComplainAboutFunc->getSourceRange(); |
| 6537 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6538 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6539 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6540 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6541 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6542 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6543 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6544 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6545 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6546 | // RHS must be a completely-type object type. |
| 6547 | // Handle the GNU void* extension. |
| 6548 | if (rpointee->isVoidType()) { |
| 6549 | if (getLangOptions().CPlusPlus) { |
| 6550 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6551 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6552 | return QualType(); |
| 6553 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6554 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6555 | ComplainAboutVoid = true; |
| 6556 | } else if (rpointee->isFunctionType()) { |
| 6557 | if (getLangOptions().CPlusPlus) { |
| 6558 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6559 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6560 | return QualType(); |
| 6561 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6562 | |
| 6563 | // GNU extension: arithmetic on pointer to function |
| 6564 | if (!ComplainAboutFunc) |
| 6565 | ComplainAboutFunc = rex; |
| 6566 | } else if (!rpointee->isDependentType() && |
| 6567 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6568 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 6569 | << rex->getSourceRange() |
| 6570 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6571 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6572 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6573 | if (getLangOptions().CPlusPlus) { |
| 6574 | // Pointee types must be the same: C++ [expr.add] |
| 6575 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6576 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6577 | << lex->getType() << rex->getType() |
| 6578 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6579 | return QualType(); |
| 6580 | } |
| 6581 | } else { |
| 6582 | // Pointee types must be compatible C99 6.5.6p3 |
| 6583 | if (!Context.typesAreCompatible( |
| 6584 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6585 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6586 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6587 | << lex->getType() << rex->getType() |
| 6588 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6589 | return QualType(); |
| 6590 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6591 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6593 | if (ComplainAboutVoid) |
| 6594 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6595 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6596 | if (ComplainAboutFunc) |
| 6597 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6598 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6599 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6600 | |
| 6601 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6602 | return Context.getPointerDiffType(); |
| 6603 | } |
| 6604 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6605 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6606 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6609 | static bool isScopedEnumerationType(QualType T) { |
| 6610 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6611 | return ET->getDecl()->isScoped(); |
| 6612 | return false; |
| 6613 | } |
| 6614 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6615 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6616 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6617 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6618 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6619 | if (!lex->getType()->hasIntegerRepresentation() || |
| 6620 | !rex->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6621 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6623 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6624 | // hasIntegerRepresentation() above instead of this. |
| 6625 | if (isScopedEnumerationType(lex->getType()) || |
| 6626 | isScopedEnumerationType(rex->getType())) { |
| 6627 | return InvalidOperands(Loc, lex, rex); |
| 6628 | } |
| 6629 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6630 | // Vector shifts promote their scalar inputs to vector type. |
| 6631 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 6632 | return CheckVectorOperands(Loc, lex, rex); |
| 6633 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6634 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6635 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6636 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6637 | // For the LHS, do usual unary conversions, but then reset them away |
| 6638 | // if this is a compound assignment. |
| 6639 | Expr *old_lex = lex; |
| 6640 | UsualUnaryConversions(lex); |
| 6641 | QualType LHSTy = lex->getType(); |
| 6642 | if (isCompAssign) lex = old_lex; |
| 6643 | |
| 6644 | // The RHS is simpler. |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6645 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6646 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6647 | // Sanity-check shift operands |
| 6648 | llvm::APSInt Right; |
| 6649 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 6650 | if (!rex->isValueDependent() && |
| 6651 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 6652 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6653 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 6654 | else { |
| 6655 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 6656 | Context.getTypeSize(lex->getType())); |
| 6657 | if (Right.uge(LeftBits)) |
| 6658 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 6659 | } |
| 6660 | } |
| 6661 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6662 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6663 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6666 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6667 | if (DeclContext *DC = D->getDeclContext()) { |
| 6668 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6669 | return true; |
| 6670 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6671 | return FD->isFunctionTemplateSpecialization(); |
| 6672 | } |
| 6673 | return false; |
| 6674 | } |
| 6675 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6676 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6677 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6678 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6679 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6680 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6681 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6682 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6683 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6684 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6685 | QualType lType = lex->getType(); |
| 6686 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6687 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6688 | Expr *LHSStripped = lex->IgnoreParenImpCasts(); |
| 6689 | Expr *RHSStripped = rex->IgnoreParenImpCasts(); |
| 6690 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6691 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6692 | |
| 6693 | // Two different enums will raise a warning when compared. |
| 6694 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 6695 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 6696 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 6697 | RHSEnumType->getDecl()->getIdentifier() && |
| 6698 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 6699 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6700 | << LHSStrippedType << RHSStrippedType |
| 6701 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6702 | } |
| 6703 | } |
| 6704 | } |
| 6705 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6706 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6707 | !(lType->isBlockPointerType() && isRelational) && |
| 6708 | !lex->getLocStart().isMacroID() && |
| 6709 | !rex->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6710 | // For non-floating point types, check for self-comparisons of the form |
| 6711 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6712 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6713 | // |
| 6714 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6715 | // expansion. Also don't warn about comparisons which are only self |
| 6716 | // comparisons within a template specialization. The warnings should catch |
| 6717 | // obvious cases in the definition of the template anyways. The idea is to |
| 6718 | // warn when the typed comparison operator will always evaluate to the same |
| 6719 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6720 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6721 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6722 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6723 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 6724 | DiagRuntimeBehavior(Loc, lex, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6725 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6726 | << (Opc == BO_EQ |
| 6727 | || Opc == BO_LE |
| 6728 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6729 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6730 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6731 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6732 | // what is it always going to eval to? |
| 6733 | char always_evals_to; |
| 6734 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6735 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6736 | always_evals_to = 0; // false |
| 6737 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6738 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6739 | always_evals_to = 1; // true |
| 6740 | break; |
| 6741 | default: |
| 6742 | // best we can say is 'a constant' |
| 6743 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6744 | break; |
| 6745 | } |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 6746 | DiagRuntimeBehavior(Loc, lex, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6747 | << 1 // array |
| 6748 | << always_evals_to); |
| 6749 | } |
| 6750 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6751 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6752 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6753 | if (isa<CastExpr>(LHSStripped)) |
| 6754 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6755 | if (isa<CastExpr>(RHSStripped)) |
| 6756 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6757 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6758 | // Warn about comparisons against a string constant (unless the other |
| 6759 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6760 | Expr *literalString = 0; |
| 6761 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6762 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6763 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6764 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6765 | literalString = lex; |
| 6766 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6767 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6768 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6769 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6770 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6771 | literalString = rex; |
| 6772 | literalStringStripped = RHSStripped; |
| 6773 | } |
| 6774 | |
| 6775 | if (literalString) { |
| 6776 | std::string resultComparison; |
| 6777 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6778 | case BO_LT: resultComparison = ") < 0"; break; |
| 6779 | case BO_GT: resultComparison = ") > 0"; break; |
| 6780 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6781 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6782 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6783 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6784 | default: assert(false && "Invalid comparison operator"); |
| 6785 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6786 | |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 6787 | DiagRuntimeBehavior(Loc, literalString, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6788 | PDiag(diag::warn_stringcompare) |
| 6789 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6790 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6791 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6792 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6793 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6794 | // C99 6.5.8p3 / C99 6.5.9p4 |
| 6795 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 6796 | UsualArithmeticConversions(lex, rex); |
| 6797 | else { |
| 6798 | UsualUnaryConversions(lex); |
| 6799 | UsualUnaryConversions(rex); |
| 6800 | } |
| 6801 | |
| 6802 | lType = lex->getType(); |
| 6803 | rType = rex->getType(); |
| 6804 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6805 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6806 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6807 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6808 | if (isRelational) { |
| 6809 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6810 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6811 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6812 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6813 | if (lType->hasFloatingRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6814 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6815 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6816 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6817 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6818 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6819 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6820 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6821 | Expr::NPC_ValueDependentIsNull); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6822 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6823 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6824 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6825 | // All of the following pointer-related warnings are GCC extensions, except |
| 6826 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6827 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6828 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6829 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6830 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6831 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6832 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6833 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6834 | if (LCanPointeeTy == RCanPointeeTy) |
| 6835 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6836 | if (!isRelational && |
| 6837 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6838 | // Valid unless comparison between non-null pointer and function pointer |
| 6839 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6840 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6841 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6842 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6843 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6844 | Diag(Loc, |
| 6845 | isSFINAEContext()? |
| 6846 | diag::err_typecheck_comparison_of_fptr_to_void |
| 6847 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6848 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6849 | |
| 6850 | if (isSFINAEContext()) |
| 6851 | return QualType(); |
| 6852 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6853 | ImpCastExprToType(rex, lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6854 | return ResultTy; |
| 6855 | } |
| 6856 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6857 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6858 | // C++ [expr.rel]p2: |
| 6859 | // [...] Pointer conversions (4.10) and qualification |
| 6860 | // conversions (4.4) are performed on pointer operands (or on |
| 6861 | // a pointer operand and a null pointer constant) to bring |
| 6862 | // them to their composite pointer type. [...] |
| 6863 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6864 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6865 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6866 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6867 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6868 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6869 | if (T.isNull()) { |
| 6870 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 6871 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6872 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6873 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6874 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6875 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6876 | << lType << rType << T |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6877 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6880 | ImpCastExprToType(lex, T, CK_BitCast); |
| 6881 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6882 | return ResultTy; |
| 6883 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6884 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6885 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6886 | RCanPointeeTy.getUnqualifiedType())) { |
| 6887 | // Valid unless a relational comparison of function pointers |
| 6888 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6889 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 6890 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6891 | } |
| 6892 | } else if (!isRelational && |
| 6893 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6894 | // Valid unless comparison between non-null pointer and function pointer |
| 6895 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6896 | && !LHSIsNull && !RHSIsNull) { |
| 6897 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 6898 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6899 | } |
| 6900 | } else { |
| 6901 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6902 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6903 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6904 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6905 | if (LCanPointeeTy != RCanPointeeTy) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6906 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6907 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6909 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6910 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6911 | // Comparison of nullptr_t with itself. |
| 6912 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6913 | return ResultTy; |
| 6914 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6915 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6916 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6917 | if (RHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6918 | ((lType->isPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6919 | (!isRelational && lType->isMemberPointerType()))) { |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6920 | ImpCastExprToType(rex, lType, |
| 6921 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6922 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6923 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6924 | return ResultTy; |
| 6925 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6926 | if (LHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6927 | ((rType->isPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6928 | (!isRelational && rType->isMemberPointerType()))) { |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6929 | ImpCastExprToType(lex, rType, |
| 6930 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6931 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6932 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6933 | return ResultTy; |
| 6934 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6935 | |
| 6936 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6937 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6938 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 6939 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6940 | // In addition, pointers to members can be compared, or a pointer to |
| 6941 | // member and a null pointer constant. Pointer to member conversions |
| 6942 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6943 | // them to a common type. If one operand is a null pointer constant, |
| 6944 | // the common type is the type of the other operand. Otherwise, the |
| 6945 | // common type is a pointer to member type similar (4.4) to the type |
| 6946 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6947 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6948 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6949 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6950 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6951 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6952 | if (T.isNull()) { |
| 6953 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6954 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6955 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6956 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6957 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6958 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6959 | << lType << rType << T |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6960 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6961 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6962 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6963 | ImpCastExprToType(lex, T, CK_BitCast); |
| 6964 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6965 | return ResultTy; |
| 6966 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6967 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6968 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6969 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6970 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6971 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6972 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6973 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6974 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6975 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6976 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6977 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6978 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6979 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6980 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6981 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6982 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6983 | if (!isRelational |
| 6984 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6985 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6986 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6987 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6988 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6989 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6990 | ->getPointeeType()->isVoidType()))) |
| 6991 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 6992 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6993 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6994 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6995 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6996 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6997 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6998 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6999 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7000 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7001 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7002 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7003 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7004 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7005 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7006 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7007 | if (!LPtrToVoid && !RPtrToVoid && |
| 7008 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7009 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7010 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7011 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7012 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7013 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7014 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7015 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7016 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7017 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 7018 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7019 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7020 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7021 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7022 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7023 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7024 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7025 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7026 | bool isError = false; |
| 7027 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7028 | (RHSIsNull && rType->isIntegerType())) { |
| 7029 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7030 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7031 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7032 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7033 | else if (getLangOptions().CPlusPlus) { |
| 7034 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7035 | isError = true; |
| 7036 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7037 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7038 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7039 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7040 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 7041 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7042 | if (isError) |
| 7043 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7044 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7045 | |
| 7046 | if (lType->isIntegerType()) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7047 | ImpCastExprToType(lex, rType, |
| 7048 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7049 | else |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7050 | ImpCastExprToType(rex, lType, |
| 7051 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7052 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7053 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7054 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7055 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7056 | if (!isRelational && RHSIsNull |
| 7057 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7058 | ImpCastExprToType(rex, lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7059 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7060 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7061 | if (!isRelational && LHSIsNull |
| 7062 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7063 | ImpCastExprToType(lex, rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7064 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7065 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7066 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7067 | } |
| 7068 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7069 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7070 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7071 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7072 | /// types. |
| 7073 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7074 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7075 | bool isRelational) { |
| 7076 | // Check to make sure we're operating on vectors of the same type and width, |
| 7077 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7078 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7079 | if (vType.isNull()) |
| 7080 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7081 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 7082 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7083 | // bool for C++, int for C |
| 7084 | if (getLangOptions().AltiVec) |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7085 | return Context.getLogicalOperationType(); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 7086 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7087 | QualType lType = lex->getType(); |
| 7088 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7089 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7090 | // For non-floating point types, check for self-comparisons of the form |
| 7091 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7092 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7093 | if (!lType->hasFloatingRepresentation()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7094 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 7095 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 7096 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 7097 | DiagRuntimeBehavior(Loc, rex, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7098 | PDiag(diag::warn_comparison_always) |
| 7099 | << 0 // self- |
| 7100 | << 2 // "a constant" |
| 7101 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7102 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7103 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7104 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7105 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7106 | assert (rType->hasFloatingRepresentation()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7107 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7108 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7109 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7110 | // Return the type for the comparison, which is the same as vector type for |
| 7111 | // integer vectors, or an integer type of identical size and number of |
| 7112 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7113 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7114 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7115 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7116 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7117 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7118 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7119 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7120 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7121 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7122 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7123 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7124 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7125 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7126 | } |
| 7127 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7128 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7129 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7130 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 7131 | if (lex->getType()->hasIntegerRepresentation() && |
| 7132 | rex->getType()->hasIntegerRepresentation()) |
| 7133 | return CheckVectorOperands(Loc, lex, rex); |
| 7134 | |
| 7135 | return InvalidOperands(Loc, lex, rex); |
| 7136 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7137 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7138 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7139 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7140 | if (lex->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7141 | rex->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7142 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7143 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7144 | } |
| 7145 | |
| 7146 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7147 | Expr *&lex, Expr *&rex, SourceLocation Loc, unsigned Opc) { |
| 7148 | |
| 7149 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7150 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7151 | // is a constant. |
| 7152 | if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() && |
Eli Friedman | 787b094 | 2010-07-27 19:14:53 +0000 | [diff] [blame] | 7153 | rex->getType()->isIntegerType() && !rex->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7154 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7155 | !Loc.isMacroID()) { |
| 7156 | // If the RHS can be constant folded, and if it constant folds to something |
| 7157 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7158 | // happened to fold to true/false) then warn. |
| 7159 | Expr::EvalResult Result; |
| 7160 | if (rex->Evaluate(Result, Context) && !Result.HasSideEffects && |
| 7161 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 7162 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 7163 | << rex->getSourceRange() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7164 | << (Opc == BO_LAnd ? "&&" : "||") |
| 7165 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7166 | } |
| 7167 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7168 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7169 | if (!Context.getLangOptions().CPlusPlus) { |
| 7170 | UsualUnaryConversions(lex); |
| 7171 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7172 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7173 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 7174 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7175 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7176 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7177 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7178 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7179 | // The following is safe because we only use this method for |
| 7180 | // non-overloadable operands. |
| 7181 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7182 | // C++ [expr.log.and]p1 |
| 7183 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7184 | // The operands are both contextually converted to type bool. |
| 7185 | if (PerformContextuallyConvertToBool(lex) || |
| 7186 | PerformContextuallyConvertToBool(rex)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7187 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7188 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7189 | // C++ [expr.log.and]p2 |
| 7190 | // C++ [expr.log.or]p2 |
| 7191 | // The result is a bool. |
| 7192 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7193 | } |
| 7194 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7195 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7196 | /// is a read-only property; return true if so. A readonly property expression |
| 7197 | /// depends on various declarations and thus must be treated specially. |
| 7198 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7199 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7200 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7201 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7202 | if (PropExpr->isImplicitProperty()) return false; |
| 7203 | |
| 7204 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7205 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 7206 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7207 | PropExpr->getBase()->getType(); |
| 7208 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7209 | if (const ObjCObjectPointerType *OPT = |
| 7210 | BaseType->getAsObjCInterfacePointerType()) |
| 7211 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7212 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7213 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7214 | } |
| 7215 | return false; |
| 7216 | } |
| 7217 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7218 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7219 | /// emit an error and return true. If so, return false. |
| 7220 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7221 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7222 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7223 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7224 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7225 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7226 | if (IsLV == Expr::MLV_Valid) |
| 7227 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7228 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7229 | unsigned Diag = 0; |
| 7230 | bool NeedType = false; |
| 7231 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7232 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7233 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7234 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7235 | NeedType = true; |
| 7236 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7237 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7238 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7239 | NeedType = true; |
| 7240 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7241 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7242 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7243 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7244 | case Expr::MLV_Valid: |
| 7245 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7246 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7247 | case Expr::MLV_MemberFunction: |
| 7248 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7249 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7250 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7251 | case Expr::MLV_IncompleteType: |
| 7252 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7253 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7254 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7255 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7256 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7257 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7258 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7259 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7260 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7261 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7262 | case Expr::MLV_ReadonlyProperty: |
| 7263 | Diag = diag::error_readonly_property_assignment; |
| 7264 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7265 | case Expr::MLV_NoSetterProperty: |
| 7266 | Diag = diag::error_nosetter_property_assignment; |
| 7267 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7268 | case Expr::MLV_SubObjCPropertySetting: |
| 7269 | Diag = diag::error_no_subobject_property_setting; |
| 7270 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7271 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7272 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7273 | SourceRange Assign; |
| 7274 | if (Loc != OrigLoc) |
| 7275 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7276 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7277 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7278 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7279 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7280 | return true; |
| 7281 | } |
| 7282 | |
| 7283 | |
| 7284 | |
| 7285 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7286 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 7287 | SourceLocation Loc, |
| 7288 | QualType CompoundType) { |
| 7289 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 7290 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7291 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7292 | |
| 7293 | QualType LHSType = LHS->getType(); |
| 7294 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7295 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7296 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7297 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7298 | // Simple assignment "x = y". |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7299 | if (LHS->getObjectKind() == OK_ObjCProperty) |
| 7300 | ConvertPropertyForLValue(LHS, RHS, LHSTy); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7301 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7302 | // Special case of NSObject attributes on c-style pointer types. |
| 7303 | if (ConvTy == IncompatiblePointer && |
| 7304 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7305 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7306 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7307 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7308 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7309 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7310 | if (ConvTy == Compatible && |
| 7311 | getLangOptions().ObjCNonFragileABI && |
| 7312 | LHSType->isObjCObjectType()) |
| 7313 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7314 | << LHSType; |
| 7315 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7316 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7317 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7318 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7319 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7320 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7321 | RHSCheck = ICE->getSubExpr(); |
| 7322 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7323 | if ((UO->getOpcode() == UO_Plus || |
| 7324 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7325 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7326 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7327 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7328 | // And there is a space or other character before the subexpr of the |
| 7329 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7330 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7331 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7332 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7333 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7334 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7335 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7336 | } |
| 7337 | } else { |
| 7338 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7339 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7340 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7341 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7342 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7343 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7344 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7345 | |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7346 | |
| 7347 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 7348 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 7349 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 7350 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 7351 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 7352 | // check. |
| 7353 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7354 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7355 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7356 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 7357 | !UO->getType().isVolatileQualified()) { |
| 7358 | Diag(UO->getOperatorLoc(), diag::warn_indirection_through_null) |
| 7359 | << UO->getSubExpr()->getSourceRange(); |
| 7360 | Diag(UO->getOperatorLoc(), diag::note_indirection_through_null); |
| 7361 | } |
| 7362 | |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7363 | // Check for trivial buffer overflows. |
| 7364 | if (const ArraySubscriptExpr *ae |
| 7365 | = dyn_cast<ArraySubscriptExpr>(LHS->IgnoreParenCasts())) |
| 7366 | CheckArrayAccess(ae); |
| 7367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7368 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7369 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7370 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7371 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7372 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7373 | // 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] | 7374 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7375 | return (getLangOptions().CPlusPlus |
| 7376 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7379 | // C99 6.5.17 |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7380 | static QualType CheckCommaOperands(Sema &S, Expr *&LHS, Expr *&RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7381 | SourceLocation Loc) { |
| 7382 | S.DiagnoseUnusedExprResult(LHS); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7383 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7384 | ExprResult LHSResult = S.CheckPlaceholderExpr(LHS, Loc); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7385 | if (LHSResult.isInvalid()) |
| 7386 | return QualType(); |
| 7387 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7388 | ExprResult RHSResult = S.CheckPlaceholderExpr(RHS, Loc); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7389 | if (RHSResult.isInvalid()) |
| 7390 | return QualType(); |
| 7391 | RHS = RHSResult.take(); |
| 7392 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7393 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7394 | // operands, but not unary promotions. |
| 7395 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7396 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7397 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7398 | // containing site to determine what should be done with the RHS. |
| 7399 | S.IgnoredValueConversions(LHS); |
| 7400 | |
| 7401 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7402 | S.DefaultFunctionArrayLvalueConversion(RHS); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7403 | if (!RHS->getType()->isVoidType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7404 | S.RequireCompleteType(Loc, RHS->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7405 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7406 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7407 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7408 | } |
| 7409 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7410 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7411 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7412 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7413 | ExprValueKind &VK, |
| 7414 | SourceLocation OpLoc, |
| 7415 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7416 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7417 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7418 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7419 | QualType ResType = Op->getType(); |
| 7420 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7421 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7422 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7423 | // Decrement of bool is not allowed. |
| 7424 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7425 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7426 | return QualType(); |
| 7427 | } |
| 7428 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7429 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7430 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7431 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7432 | } else if (ResType->isAnyPointerType()) { |
| 7433 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7434 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7435 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7436 | if (PointeeTy->isVoidType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7437 | if (S.getLangOptions().CPlusPlus) { |
| 7438 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7439 | << Op->getSourceRange(); |
| 7440 | return QualType(); |
| 7441 | } |
| 7442 | |
| 7443 | // Pointer to void is a GNU extension in C. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7444 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7445 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7446 | if (S.getLangOptions().CPlusPlus) { |
| 7447 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7448 | << Op->getType() << Op->getSourceRange(); |
| 7449 | return QualType(); |
| 7450 | } |
| 7451 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7452 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7453 | << ResType << Op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7454 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 7455 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7456 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7457 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7458 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7459 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7460 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7461 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7462 | << PointeeTy << Op->getSourceRange(); |
| 7463 | return QualType(); |
| 7464 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7465 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7466 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7467 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7468 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7469 | } else if (ResType->isPlaceholderType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7470 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7471 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7472 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7473 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7474 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7475 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7476 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7477 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7478 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7479 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7480 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7481 | // 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] | 7482 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7483 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7484 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7485 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7486 | // (in C or with postfix), the increment is the unqualified type of the |
| 7487 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7488 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7489 | VK = VK_LValue; |
| 7490 | return ResType; |
| 7491 | } else { |
| 7492 | VK = VK_RValue; |
| 7493 | return ResType.getUnqualifiedType(); |
| 7494 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7497 | void Sema::ConvertPropertyForRValue(Expr *&E) { |
| 7498 | assert(E->getValueKind() == VK_LValue && |
| 7499 | E->getObjectKind() == OK_ObjCProperty); |
| 7500 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7501 | |
| 7502 | ExprValueKind VK = VK_RValue; |
| 7503 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7504 | if (const ObjCMethodDecl *GetterMethod = |
| 7505 | PRE->getImplicitPropertyGetter()) { |
| 7506 | QualType Result = GetterMethod->getResultType(); |
| 7507 | VK = Expr::getValueKindForType(Result); |
| 7508 | } |
| 7509 | else { |
| 7510 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7511 | << PRE->getBase()->getType(); |
| 7512 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 7516 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7517 | |
| 7518 | ExprResult Result = MaybeBindToTemporary(E); |
| 7519 | if (!Result.isInvalid()) |
| 7520 | E = Result.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7521 | } |
| 7522 | |
| 7523 | void Sema::ConvertPropertyForLValue(Expr *&LHS, Expr *&RHS, QualType &LHSTy) { |
| 7524 | assert(LHS->getValueKind() == VK_LValue && |
| 7525 | LHS->getObjectKind() == OK_ObjCProperty); |
| 7526 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 7527 | |
| 7528 | if (PRE->isImplicitProperty()) { |
| 7529 | // If using property-dot syntax notation for assignment, and there is a |
| 7530 | // setter, RHS expression is being passed to the setter argument. So, |
| 7531 | // type conversion (and comparison) is RHS to setter's argument type. |
| 7532 | if (const ObjCMethodDecl *SetterMD = PRE->getImplicitPropertySetter()) { |
| 7533 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7534 | LHSTy = (*P)->getType(); |
| 7535 | |
| 7536 | // Otherwise, if the getter returns an l-value, just call that. |
| 7537 | } else { |
| 7538 | QualType Result = PRE->getImplicitPropertyGetter()->getResultType(); |
| 7539 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7540 | if (VK == VK_LValue) { |
| 7541 | LHS = ImplicitCastExpr::Create(Context, LHS->getType(), |
| 7542 | CK_GetObjCProperty, LHS, 0, VK); |
| 7543 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7544 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7545 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7546 | } |
| 7547 | |
| 7548 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7549 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7550 | InitializedEntity::InitializeParameter(Context, LHSTy); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7551 | Expr *Arg = RHS; |
| 7552 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), |
| 7553 | Owned(Arg)); |
| 7554 | if (!ArgE.isInvalid()) |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7555 | RHS = ArgE.takeAs<Expr>(); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7556 | } |
| 7557 | } |
| 7558 | |
| 7559 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7560 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7561 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7562 | /// where the declaration is needed for type checking. We only need to |
| 7563 | /// handle cases when the expression references a function designator |
| 7564 | /// or is an lvalue. Here are some examples: |
| 7565 | /// - &(x) => x |
| 7566 | /// - &*****f => f for f a function designator. |
| 7567 | /// - &s.xx => s |
| 7568 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7569 | /// - *(x + 1) -> x, if x is an array |
| 7570 | /// - &"123"[2] -> 0 |
| 7571 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7572 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7573 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7574 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7575 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7576 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7577 | // If this is an arrow operator, the address is an offset from |
| 7578 | // the base's value, so the object the base refers to is |
| 7579 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7580 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7581 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7582 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7583 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7584 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7585 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7586 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7587 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7588 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7589 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7590 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7591 | } |
| 7592 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7593 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7594 | case Stmt::UnaryOperatorClass: { |
| 7595 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7596 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7597 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7598 | case UO_Real: |
| 7599 | case UO_Imag: |
| 7600 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7601 | return getPrimaryDecl(UO->getSubExpr()); |
| 7602 | default: |
| 7603 | return 0; |
| 7604 | } |
| 7605 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7606 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7607 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7608 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7609 | // If the result of an implicit cast is an l-value, we care about |
| 7610 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7611 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7612 | default: |
| 7613 | return 0; |
| 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7618 | /// 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] | 7619 | /// 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] | 7620 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7621 | /// 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] | 7622 | /// 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] | 7623 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7624 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7625 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7626 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7627 | return S.Context.DependentTy; |
| 7628 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7629 | return S.Context.OverloadTy; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7630 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7631 | ExprResult PR = S.CheckPlaceholderExpr(OrigOp, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7632 | if (PR.isInvalid()) return QualType(); |
| 7633 | OrigOp = PR.take(); |
| 7634 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7635 | // Make sure to ignore parentheses in subsequent checks |
| 7636 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7637 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7638 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7639 | // Implement C99-only parts of addressof rules. |
| 7640 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7641 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7642 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7643 | // (assuming the deref expression is valid). |
| 7644 | return uOp->getSubExpr()->getType(); |
| 7645 | } |
| 7646 | // Technically, there should be a check for array subscript |
| 7647 | // expressions here, but the result of one is always an lvalue anyway. |
| 7648 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7649 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7650 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7651 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7652 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7653 | bool sfinae = S.isSFINAEContext(); |
| 7654 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7655 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7656 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7657 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7658 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7659 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7660 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7661 | } else if (lval == Expr::LV_MemberFunction) { |
| 7662 | // If it's an instance method, make a member pointer. |
| 7663 | // The expression must have exactly the form &A::foo. |
| 7664 | |
| 7665 | // If the underlying expression isn't a decl ref, give up. |
| 7666 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7667 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7668 | << OrigOp->getSourceRange(); |
| 7669 | return QualType(); |
| 7670 | } |
| 7671 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7672 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7673 | |
| 7674 | // The id-expression was parenthesized. |
| 7675 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7676 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7677 | << OrigOp->getSourceRange(); |
| 7678 | |
| 7679 | // The method was named without a qualifier. |
| 7680 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7681 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7682 | << op->getSourceRange(); |
| 7683 | } |
| 7684 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7685 | return S.Context.getMemberPointerType(op->getType(), |
| 7686 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7687 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7688 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7689 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7690 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7691 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7692 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7693 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7694 | return QualType(); |
| 7695 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7696 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7697 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7698 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7699 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7700 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7701 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7702 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7703 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7704 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7705 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7706 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7707 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7708 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7709 | << "property expression" << op->getSourceRange(); |
| 7710 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7711 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7712 | // 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] | 7713 | // with the register storage-class specifier. |
| 7714 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7715 | // in C++ it is not error to take address of a register |
| 7716 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7717 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7718 | !S.getLangOptions().CPlusPlus) { |
| 7719 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7720 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7721 | return QualType(); |
| 7722 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7723 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7724 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7725 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7726 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7727 | // Could be a pointer to member, though, if there is an explicit |
| 7728 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7729 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7730 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7731 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7732 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7733 | S.Diag(OpLoc, |
| 7734 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7735 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7736 | return QualType(); |
| 7737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7738 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7739 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7740 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7741 | return S.Context.getMemberPointerType(op->getType(), |
| 7742 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7743 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7744 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7745 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7746 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7747 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7748 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7749 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7750 | // Taking the address of a void variable is technically illegal, but we |
| 7751 | // allow it in cases which are otherwise valid. |
| 7752 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7753 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7754 | } |
| 7755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7756 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 8f70ddb | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 7757 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7758 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7759 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7762 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7763 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7764 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7765 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7766 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7767 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7768 | S.UsualUnaryConversions(Op); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7769 | QualType OpTy = Op->getType(); |
| 7770 | QualType Result; |
| 7771 | |
| 7772 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7773 | // is an incomplete type or void. It would be possible to warn about |
| 7774 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7775 | // warning is unlikely to catch any mistakes. |
| 7776 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7777 | Result = PT->getPointeeType(); |
| 7778 | else if (const ObjCObjectPointerType *OPT = |
| 7779 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7780 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7781 | else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7782 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7783 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7784 | if (PR.take() != Op) |
| 7785 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7786 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7787 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7788 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7789 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7790 | << OpTy << Op->getSourceRange(); |
| 7791 | return QualType(); |
| 7792 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7793 | |
| 7794 | // Dereferences are usually l-values... |
| 7795 | VK = VK_LValue; |
| 7796 | |
| 7797 | // ...except that certain expressions are never l-values in C. |
| 7798 | if (!S.getLangOptions().CPlusPlus && |
| 7799 | IsCForbiddenLValueType(S.Context, Result)) |
| 7800 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7801 | |
| 7802 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7803 | } |
| 7804 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7805 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7806 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7807 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7808 | switch (Kind) { |
| 7809 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7810 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7811 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7812 | case tok::star: Opc = BO_Mul; break; |
| 7813 | case tok::slash: Opc = BO_Div; break; |
| 7814 | case tok::percent: Opc = BO_Rem; break; |
| 7815 | case tok::plus: Opc = BO_Add; break; |
| 7816 | case tok::minus: Opc = BO_Sub; break; |
| 7817 | case tok::lessless: Opc = BO_Shl; break; |
| 7818 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7819 | case tok::lessequal: Opc = BO_LE; break; |
| 7820 | case tok::less: Opc = BO_LT; break; |
| 7821 | case tok::greaterequal: Opc = BO_GE; break; |
| 7822 | case tok::greater: Opc = BO_GT; break; |
| 7823 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7824 | case tok::equalequal: Opc = BO_EQ; break; |
| 7825 | case tok::amp: Opc = BO_And; break; |
| 7826 | case tok::caret: Opc = BO_Xor; break; |
| 7827 | case tok::pipe: Opc = BO_Or; break; |
| 7828 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7829 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7830 | case tok::equal: Opc = BO_Assign; break; |
| 7831 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7832 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7833 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7834 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7835 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7836 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7837 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7838 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7839 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7840 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7841 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7842 | } |
| 7843 | return Opc; |
| 7844 | } |
| 7845 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7846 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7847 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7848 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7849 | switch (Kind) { |
| 7850 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7851 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7852 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7853 | case tok::amp: Opc = UO_AddrOf; break; |
| 7854 | case tok::star: Opc = UO_Deref; break; |
| 7855 | case tok::plus: Opc = UO_Plus; break; |
| 7856 | case tok::minus: Opc = UO_Minus; break; |
| 7857 | case tok::tilde: Opc = UO_Not; break; |
| 7858 | case tok::exclaim: Opc = UO_LNot; break; |
| 7859 | case tok::kw___real: Opc = UO_Real; break; |
| 7860 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7861 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7862 | } |
| 7863 | return Opc; |
| 7864 | } |
| 7865 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7866 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7867 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7868 | /// suppressed in the event of macro expansions. |
| 7869 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7870 | SourceLocation OpLoc) { |
| 7871 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7872 | return; |
| 7873 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7874 | return; |
| 7875 | lhs = lhs->IgnoreParenImpCasts(); |
| 7876 | rhs = rhs->IgnoreParenImpCasts(); |
| 7877 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7878 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7879 | if (!LeftDeclRef || !RightDeclRef || |
| 7880 | LeftDeclRef->getLocation().isMacroID() || |
| 7881 | RightDeclRef->getLocation().isMacroID()) |
| 7882 | return; |
| 7883 | const ValueDecl *LeftDecl = |
| 7884 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7885 | const ValueDecl *RightDecl = |
| 7886 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7887 | if (LeftDecl != RightDecl) |
| 7888 | return; |
| 7889 | if (LeftDecl->getType().isVolatileQualified()) |
| 7890 | return; |
| 7891 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7892 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7893 | return; |
| 7894 | |
| 7895 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7896 | << LeftDeclRef->getType() |
| 7897 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7898 | } |
| 7899 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7900 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7901 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7902 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7903 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7904 | BinaryOperatorKind Opc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7905 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7906 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7907 | // The following two variables are used for compound assignment operators |
| 7908 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7909 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7910 | ExprValueKind VK = VK_RValue; |
| 7911 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7912 | |
| 7913 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7914 | case BO_Assign: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7915 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7916 | if (getLangOptions().CPlusPlus && |
| 7917 | lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7918 | VK = lhs->getValueKind(); |
| 7919 | OK = lhs->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7920 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7921 | if (!ResultTy.isNull()) |
| 7922 | DiagnoseSelfAssignment(*this, lhs, rhs, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7923 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7924 | case BO_PtrMemD: |
| 7925 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7926 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7927 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7928 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7929 | case BO_Mul: |
| 7930 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7931 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7932 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7933 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7934 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7935 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7936 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7937 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7938 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7939 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7940 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7941 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7942 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7943 | case BO_Shl: |
| 7944 | case BO_Shr: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7945 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 7946 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7947 | case BO_LE: |
| 7948 | case BO_LT: |
| 7949 | case BO_GE: |
| 7950 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7951 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7952 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7953 | case BO_EQ: |
| 7954 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7955 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7956 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7957 | case BO_And: |
| 7958 | case BO_Xor: |
| 7959 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7960 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7961 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7962 | case BO_LAnd: |
| 7963 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7964 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7965 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7966 | case BO_MulAssign: |
| 7967 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7968 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7969 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7970 | CompLHSTy = CompResultTy; |
| 7971 | if (!CompResultTy.isNull()) |
| 7972 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7973 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7974 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7975 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7976 | CompLHSTy = CompResultTy; |
| 7977 | if (!CompResultTy.isNull()) |
| 7978 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7979 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7980 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7981 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 7982 | if (!CompResultTy.isNull()) |
| 7983 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7984 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7985 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7986 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 7987 | if (!CompResultTy.isNull()) |
| 7988 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7989 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7990 | case BO_ShlAssign: |
| 7991 | case BO_ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7992 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 7993 | CompLHSTy = CompResultTy; |
| 7994 | if (!CompResultTy.isNull()) |
| 7995 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7996 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7997 | case BO_AndAssign: |
| 7998 | case BO_XorAssign: |
| 7999 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8000 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8001 | CompLHSTy = CompResultTy; |
| 8002 | if (!CompResultTy.isNull()) |
| 8003 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8004 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8005 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8006 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8007 | if (getLangOptions().CPlusPlus) { |
| 8008 | VK = rhs->getValueKind(); |
| 8009 | OK = rhs->getObjectKind(); |
| 8010 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8011 | break; |
| 8012 | } |
| 8013 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8014 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8015 | if (CompResultTy.isNull()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8016 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, |
| 8017 | VK, OK, OpLoc)); |
| 8018 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8019 | if (getLangOptions().CPlusPlus && lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8020 | VK = VK_LValue; |
| 8021 | OK = lhs->getObjectKind(); |
| 8022 | } |
| 8023 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
| 8024 | VK, OK, CompLHSTy, |
| 8025 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8026 | } |
| 8027 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8028 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 8029 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8030 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 8031 | const PartialDiagnostic &PD, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8032 | const PartialDiagnostic &FirstNote, |
| 8033 | SourceRange FirstParenRange, |
| 8034 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8035 | SourceRange SecondParenRange) { |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8036 | Self.Diag(Loc, PD); |
| 8037 | |
| 8038 | if (!FirstNote.getDiagID()) |
| 8039 | return; |
| 8040 | |
| 8041 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 8042 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8043 | // We can't display the parentheses, so just return. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8044 | return; |
| 8045 | } |
| 8046 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8047 | Self.Diag(Loc, FirstNote) |
| 8048 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8049 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8050 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8051 | if (!SecondNote.getDiagID()) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8052 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8053 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8054 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 8055 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8056 | // We can't display the parentheses, so just dig the |
| 8057 | // warning/error and return. |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8058 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8059 | return; |
| 8060 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8061 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8062 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8063 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 8064 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8065 | } |
| 8066 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8067 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8068 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8069 | /// comparison operators have higher precedence. The most typical example of |
| 8070 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8071 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8072 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8073 | typedef BinaryOperator BinOp; |
| 8074 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8075 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8076 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8077 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8078 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8079 | rhsopc = BO->getOpcode(); |
| 8080 | |
| 8081 | // Subs are not binary operators. |
| 8082 | if (lhsopc == -1 && rhsopc == -1) |
| 8083 | return; |
| 8084 | |
| 8085 | // Bitwise operations are sometimes used as eager logical ops. |
| 8086 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8087 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8088 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8089 | return; |
| 8090 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8091 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8092 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8093 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8094 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8095 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8096 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8097 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8098 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 8099 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8100 | << BinOp::getOpcodeStr(lhsopc), |
| 8101 | lhs->getSourceRange()); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8102 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8103 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8104 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8105 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8106 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8107 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8108 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8109 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 8110 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8111 | << BinOp::getOpcodeStr(rhsopc), |
| 8112 | rhs->getSourceRange()); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8115 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8116 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8117 | /// in parentheses. |
| 8118 | static void |
| 8119 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| 8120 | Expr *E) { |
| 8121 | assert(isa<BinaryOperator>(E) && |
| 8122 | cast<BinaryOperator>(E)->getOpcode() == BO_LAnd); |
| 8123 | SuggestParentheses(Self, OpLoc, |
| 8124 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| 8125 | << E->getSourceRange(), |
| 8126 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| 8127 | E->getSourceRange(), |
| 8128 | Self.PDiag(0), SourceRange()); |
| 8129 | } |
| 8130 | |
| 8131 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8132 | /// 'true'. |
| 8133 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8134 | bool Res; |
| 8135 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8136 | } |
| 8137 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8138 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8139 | /// 'false'. |
| 8140 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8141 | bool Res; |
| 8142 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8143 | } |
| 8144 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8145 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8146 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8147 | Expr *OrLHS, Expr *OrRHS) { |
| 8148 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8149 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8150 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 8151 | if (EvaluatesAsFalse(S, OrRHS)) |
| 8152 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8153 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8154 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8155 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8156 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8157 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8158 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8159 | // "a || b && 1", but warn now. |
| 8160 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8161 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8162 | } |
| 8163 | } |
| 8164 | } |
| 8165 | } |
| 8166 | |
| 8167 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8168 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8169 | Expr *OrLHS, Expr *OrRHS) { |
| 8170 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8171 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8172 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 8173 | if (EvaluatesAsFalse(S, OrLHS)) |
| 8174 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8175 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8176 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8177 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8178 | } |
| 8179 | } |
| 8180 | } |
| 8181 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8182 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8183 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8184 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8185 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8186 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8187 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8188 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 8189 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8190 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8191 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | d92ccaa | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 8192 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8193 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 8194 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8195 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8198 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8199 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8200 | tok::TokenKind Kind, |
| 8201 | Expr *lhs, Expr *rhs) { |
| 8202 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8203 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8204 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8205 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8206 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8207 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8208 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8209 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8210 | } |
| 8211 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8212 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8213 | BinaryOperatorKind Opc, |
| 8214 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8215 | if (getLangOptions().CPlusPlus) { |
| 8216 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8217 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8218 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8219 | UseBuiltinOperator = false; |
| 8220 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8221 | UseBuiltinOperator = true; |
| 8222 | } else { |
| 8223 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 8224 | !rhs->getType()->isOverloadableType(); |
| 8225 | } |
| 8226 | |
| 8227 | if (!UseBuiltinOperator) { |
| 8228 | // Find all of the overloaded operators visible from this |
| 8229 | // point. We perform both an operator-name lookup from the local |
| 8230 | // scope and an argument-dependent lookup based on the types of |
| 8231 | // the arguments. |
| 8232 | UnresolvedSet<16> Functions; |
| 8233 | OverloadedOperatorKind OverOp |
| 8234 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8235 | if (S && OverOp != OO_None) |
| 8236 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 8237 | Functions); |
| 8238 | |
| 8239 | // Build the (potentially-overloaded, potentially-dependent) |
| 8240 | // binary operation. |
| 8241 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 8242 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8243 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8244 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8245 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8246 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8247 | } |
| 8248 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8249 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8250 | UnaryOperatorKind Opc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8251 | Expr *Input) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8252 | ExprValueKind VK = VK_RValue; |
| 8253 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8254 | QualType resultType; |
| 8255 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8256 | case UO_PreInc: |
| 8257 | case UO_PreDec: |
| 8258 | case UO_PostInc: |
| 8259 | case UO_PostDec: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8260 | resultType = CheckIncrementDecrementOperand(*this, Input, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8261 | Opc == UO_PreInc || |
| 8262 | Opc == UO_PostInc, |
| 8263 | Opc == UO_PreInc || |
| 8264 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8265 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8266 | case UO_AddrOf: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8267 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8268 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8269 | case UO_Deref: |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8270 | DefaultFunctionArrayLvalueConversion(Input); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8271 | resultType = CheckIndirectionOperand(*this, Input, VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8272 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8273 | case UO_Plus: |
| 8274 | case UO_Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8275 | UsualUnaryConversions(Input); |
| 8276 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8277 | if (resultType->isDependentType()) |
| 8278 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8279 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8280 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8281 | break; |
| 8282 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8283 | resultType->isEnumeralType()) |
| 8284 | break; |
| 8285 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8286 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8287 | resultType->isPointerType()) |
| 8288 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8289 | else if (resultType->isPlaceholderType()) { |
| 8290 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8291 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8292 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8293 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8294 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8295 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8296 | << resultType << Input->getSourceRange()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8297 | case UO_Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8298 | UsualUnaryConversions(Input); |
| 8299 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8300 | if (resultType->isDependentType()) |
| 8301 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8302 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8303 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8304 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8305 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8306 | << resultType << Input->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8307 | else if (resultType->hasIntegerRepresentation()) |
| 8308 | break; |
| 8309 | else if (resultType->isPlaceholderType()) { |
| 8310 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8311 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8312 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8313 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8314 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8315 | << resultType << Input->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8316 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8317 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8318 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8319 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8320 | DefaultFunctionArrayLvalueConversion(Input); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8321 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8322 | if (resultType->isDependentType()) |
| 8323 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8324 | if (resultType->isScalarType()) { // C99 6.5.3.3p1 |
| 8325 | // ok, fallthrough |
| 8326 | } else if (resultType->isPlaceholderType()) { |
| 8327 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8328 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8329 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8330 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8331 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8332 | << resultType << Input->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8333 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8335 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8336 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8337 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8338 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8339 | case UO_Real: |
| 8340 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8341 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8342 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
| 8343 | if (Input->getValueKind() != VK_RValue && |
| 8344 | Input->getObjectKind() == OK_Ordinary) |
| 8345 | VK = Input->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8346 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8347 | case UO_Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8348 | resultType = Input->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8349 | VK = Input->getValueKind(); |
| 8350 | OK = Input->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8351 | break; |
| 8352 | } |
| 8353 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8354 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8355 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8356 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, |
| 8357 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8358 | } |
| 8359 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8360 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8361 | UnaryOperatorKind Opc, |
| 8362 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8363 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8364 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8365 | // Find all of the overloaded operators visible from this |
| 8366 | // point. We perform both an operator-name lookup from the local |
| 8367 | // scope and an argument-dependent lookup based on the types of |
| 8368 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8369 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8370 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8371 | if (S && OverOp != OO_None) |
| 8372 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8373 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8374 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8375 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8376 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8377 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8378 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8381 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8382 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8383 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8384 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8387 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8388 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8389 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8390 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8391 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8392 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8393 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8394 | } |
| 8395 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8396 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8397 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8398 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8399 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8400 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8401 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8402 | bool isFileScope |
| 8403 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8404 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8405 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8406 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8407 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8408 | // example, it is not possible to goto into a stmt expression apparently. |
| 8409 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8410 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8411 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8412 | // as the type of the stmtexpr. |
| 8413 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8414 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8415 | if (!Compound->body_empty()) { |
| 8416 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8417 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8418 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8419 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8420 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8421 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8422 | } |
| 8423 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8424 | // Do function/array conversion on the last expression, but not |
| 8425 | // lvalue-to-rvalue. However, initialize an unqualified type. |
| 8426 | DefaultFunctionArrayConversion(LastExpr); |
| 8427 | Ty = LastExpr->getType().getUnqualifiedType(); |
| 8428 | |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8429 | if (!Ty->isDependentType() && !LastExpr->isTypeDependent()) { |
| 8430 | ExprResult Res = PerformCopyInitialization( |
| 8431 | InitializedEntity::InitializeResult(LPLoc, |
| 8432 | Ty, |
| 8433 | false), |
| 8434 | SourceLocation(), |
| 8435 | Owned(LastExpr)); |
| 8436 | if (Res.isInvalid()) |
| 8437 | return ExprError(); |
| 8438 | if ((LastExpr = Res.takeAs<Expr>())) { |
| 8439 | if (!LastLabelStmt) |
| 8440 | Compound->setLastStmt(LastExpr); |
| 8441 | else |
| 8442 | LastLabelStmt->setSubStmt(LastExpr); |
| 8443 | StmtExprMayBindToTemp = true; |
| 8444 | } |
| 8445 | } |
| 8446 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8447 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8448 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8449 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8450 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8451 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8452 | if (StmtExprMayBindToTemp) |
| 8453 | return MaybeBindToTemporary(ResStmtExpr); |
| 8454 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8455 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8456 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8457 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8458 | TypeSourceInfo *TInfo, |
| 8459 | OffsetOfComponent *CompPtr, |
| 8460 | unsigned NumComponents, |
| 8461 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8462 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8463 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8464 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8465 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8466 | // We must have at least one component that refers to the type, and the first |
| 8467 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8468 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8469 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8470 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8471 | << ArgTy << TypeRange); |
| 8472 | |
| 8473 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8474 | // with an incomplete type would be ill-formed. |
| 8475 | if (!Dependent |
| 8476 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8477 | PDiag(diag::err_offsetof_incomplete_type) |
| 8478 | << TypeRange)) |
| 8479 | return ExprError(); |
| 8480 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8481 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8482 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8483 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8484 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8485 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8486 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8487 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8488 | |
| 8489 | bool DidWarnAboutNonPOD = false; |
| 8490 | QualType CurrentType = ArgTy; |
| 8491 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 8492 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 8493 | llvm::SmallVector<Expr*, 4> Exprs; |
| 8494 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8495 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8496 | if (OC.isBrackets) { |
| 8497 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8498 | if (!CurrentType->isDependentType()) { |
| 8499 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8500 | if(!AT) |
| 8501 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8502 | << CurrentType); |
| 8503 | CurrentType = AT->getElementType(); |
| 8504 | } else |
| 8505 | CurrentType = Context.DependentTy; |
| 8506 | |
| 8507 | // The expression must be an integral expression. |
| 8508 | // FIXME: An integral constant expression? |
| 8509 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8510 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8511 | !Idx->getType()->isIntegerType()) |
| 8512 | return ExprError(Diag(Idx->getLocStart(), |
| 8513 | diag::err_typecheck_subscript_not_integer) |
| 8514 | << Idx->getSourceRange()); |
| 8515 | |
| 8516 | // Record this array index. |
| 8517 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8518 | Exprs.push_back(Idx); |
| 8519 | continue; |
| 8520 | } |
| 8521 | |
| 8522 | // Offset of a field. |
| 8523 | if (CurrentType->isDependentType()) { |
| 8524 | // We have the offset of a field, but we can't look into the dependent |
| 8525 | // type. Just record the identifier of the field. |
| 8526 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8527 | CurrentType = Context.DependentTy; |
| 8528 | continue; |
| 8529 | } |
| 8530 | |
| 8531 | // We need to have a complete type to look into. |
| 8532 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8533 | diag::err_offsetof_incomplete_type)) |
| 8534 | return ExprError(); |
| 8535 | |
| 8536 | // Look for the designated field. |
| 8537 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8538 | if (!RC) |
| 8539 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8540 | << CurrentType); |
| 8541 | RecordDecl *RD = RC->getDecl(); |
| 8542 | |
| 8543 | // C++ [lib.support.types]p5: |
| 8544 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8545 | // International Standard. type shall be a POD structure or a POD union |
| 8546 | // (clause 9). |
| 8547 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8548 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 8549 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8550 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8551 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8552 | << CurrentType)) |
| 8553 | DidWarnAboutNonPOD = true; |
| 8554 | } |
| 8555 | |
| 8556 | // Look for the field. |
| 8557 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8558 | LookupQualifiedName(R, RD); |
| 8559 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8560 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8561 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8562 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8563 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8564 | } |
| 8565 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8566 | if (!MemberDecl) |
| 8567 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8568 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8569 | OC.LocEnd)); |
| 8570 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8571 | // C99 7.17p3: |
| 8572 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8573 | // |
| 8574 | // We diagnose this as an error. |
| 8575 | if (MemberDecl->getBitWidth()) { |
| 8576 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8577 | << MemberDecl->getDeclName() |
| 8578 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8579 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8580 | return ExprError(); |
| 8581 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8582 | |
| 8583 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8584 | if (IndirectMemberDecl) |
| 8585 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8586 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8587 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8588 | // the base class indirections. |
| 8589 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8590 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8591 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8592 | CXXBasePath &Path = Paths.front(); |
| 8593 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8594 | B != BEnd; ++B) |
| 8595 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8596 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8597 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8598 | if (IndirectMemberDecl) { |
| 8599 | for (IndirectFieldDecl::chain_iterator FI = |
| 8600 | IndirectMemberDecl->chain_begin(), |
| 8601 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8602 | assert(isa<FieldDecl>(*FI)); |
| 8603 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8604 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8605 | } |
| 8606 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8607 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8608 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8609 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8610 | } |
| 8611 | |
| 8612 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8613 | TInfo, Comps.data(), Comps.size(), |
| 8614 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8615 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8616 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8617 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8618 | SourceLocation BuiltinLoc, |
| 8619 | SourceLocation TypeLoc, |
| 8620 | ParsedType argty, |
| 8621 | OffsetOfComponent *CompPtr, |
| 8622 | unsigned NumComponents, |
| 8623 | SourceLocation RPLoc) { |
| 8624 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8625 | TypeSourceInfo *ArgTInfo; |
| 8626 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8627 | if (ArgTy.isNull()) |
| 8628 | return ExprError(); |
| 8629 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8630 | if (!ArgTInfo) |
| 8631 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8632 | |
| 8633 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8634 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
| 8637 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8638 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8639 | Expr *CondExpr, |
| 8640 | Expr *LHSExpr, Expr *RHSExpr, |
| 8641 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8642 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8643 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8644 | ExprValueKind VK = VK_RValue; |
| 8645 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8646 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8647 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8648 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8649 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8650 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8651 | } else { |
| 8652 | // The conditional expression is required to be a constant expression. |
| 8653 | llvm::APSInt condEval(32); |
| 8654 | SourceLocation ExpLoc; |
| 8655 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8656 | return ExprError(Diag(ExpLoc, |
| 8657 | diag::err_typecheck_choose_expr_requires_constant) |
| 8658 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8659 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8660 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8661 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8662 | |
| 8663 | resType = ActiveExpr->getType(); |
| 8664 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8665 | VK = ActiveExpr->getValueKind(); |
| 8666 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8667 | } |
| 8668 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8669 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8670 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8671 | resType->isDependentType(), |
| 8672 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8675 | //===----------------------------------------------------------------------===// |
| 8676 | // Clang Extensions. |
| 8677 | //===----------------------------------------------------------------------===// |
| 8678 | |
| 8679 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8680 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8681 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8682 | PushBlockScope(BlockScope, Block); |
| 8683 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8684 | if (BlockScope) |
| 8685 | PushDeclContext(BlockScope, Block); |
| 8686 | else |
| 8687 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8688 | } |
| 8689 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8690 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8691 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8692 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8693 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8694 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8695 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8696 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8697 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8698 | // GetTypeForDeclarator always produces a function type for a block |
| 8699 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8700 | // unless the function was written with a typedef. |
| 8701 | assert(T->isFunctionType() && |
| 8702 | "GetTypeForDeclarator made a non-function block signature"); |
| 8703 | |
| 8704 | // Look for an explicit signature in that function type. |
| 8705 | FunctionProtoTypeLoc ExplicitSignature; |
| 8706 | |
| 8707 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8708 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8709 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8710 | |
| 8711 | // Check whether that explicit signature was synthesized by |
| 8712 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8713 | // written signature. |
| 8714 | if (ExplicitSignature.getLParenLoc() == |
| 8715 | ExplicitSignature.getRParenLoc()) { |
| 8716 | // This would be much cheaper if we stored TypeLocs instead of |
| 8717 | // TypeSourceInfos. |
| 8718 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8719 | unsigned Size = Result.getFullDataSize(); |
| 8720 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8721 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8722 | |
| 8723 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8724 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8726 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8727 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8728 | CurBlock->FunctionType = T; |
| 8729 | |
| 8730 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8731 | QualType RetTy = Fn->getResultType(); |
| 8732 | bool isVariadic = |
| 8733 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8734 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8735 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8736 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8737 | // Don't allow returning a objc interface by value. |
| 8738 | if (RetTy->isObjCObjectType()) { |
| 8739 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8740 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8741 | return; |
| 8742 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8743 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8744 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8745 | // return type. TODO: what should we do with declarators like: |
| 8746 | // ^ * { ... } |
| 8747 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8748 | if (RetTy != Context.DependentTy) |
| 8749 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8750 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8751 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8752 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8753 | if (ExplicitSignature) { |
| 8754 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8755 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8756 | if (Param->getIdentifier() == 0 && |
| 8757 | !Param->isImplicit() && |
| 8758 | !Param->isInvalidDecl() && |
| 8759 | !getLangOptions().CPlusPlus) |
| 8760 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8761 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8762 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8763 | |
| 8764 | // Fake up parameter variables if we have a typedef, like |
| 8765 | // ^ fntype { ... } |
| 8766 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8767 | for (FunctionProtoType::arg_type_iterator |
| 8768 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8769 | ParmVarDecl *Param = |
| 8770 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8771 | ParamInfo.getSourceRange().getBegin(), |
| 8772 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8773 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8774 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8775 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8776 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8777 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8778 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8779 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8780 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8781 | CurBlock->TheDecl->param_end(), |
| 8782 | /*CheckParameterNames=*/false); |
| 8783 | } |
| 8784 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8785 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8786 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8787 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8788 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8789 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8790 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8791 | // FIXME: remove the attribute. |
| 8792 | } |
| 8793 | |
| 8794 | // Put the parameter variables in scope. We can bail out immediately |
| 8795 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8796 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8797 | return; |
| 8798 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8799 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8800 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8801 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8802 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8803 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8804 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8805 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8806 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8807 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8808 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8809 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8813 | /// is invoked to pop the information about the block from the action impl. |
| 8814 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8815 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8816 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8817 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8818 | } |
| 8819 | |
| 8820 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8821 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8822 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8823 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8824 | // If blocks are disabled, emit an error. |
| 8825 | if (!LangOpts.Blocks) |
| 8826 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8827 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8828 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8829 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8830 | PopDeclContext(); |
| 8831 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8832 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8833 | if (!BSI->ReturnType.isNull()) |
| 8834 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8835 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8836 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8837 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8838 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8839 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8840 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8841 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8842 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8843 | // If the user wrote a function type in some form, try to use that. |
| 8844 | if (!BSI->FunctionType.isNull()) { |
| 8845 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8846 | |
| 8847 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8848 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8849 | |
| 8850 | // Turn protoless block types into nullary block types. |
| 8851 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8852 | FunctionProtoType::ExtProtoInfo EPI; |
| 8853 | EPI.ExtInfo = Ext; |
| 8854 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8855 | |
| 8856 | // Otherwise, if we don't need to change anything about the function type, |
| 8857 | // preserve its sugar structure. |
| 8858 | } else if (FTy->getResultType() == RetTy && |
| 8859 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8860 | BlockTy = BSI->FunctionType; |
| 8861 | |
| 8862 | // Otherwise, make the minimal modifications to the function type. |
| 8863 | } else { |
| 8864 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8865 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8866 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8867 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8868 | BlockTy = Context.getFunctionType(RetTy, |
| 8869 | FPT->arg_type_begin(), |
| 8870 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8871 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8872 | } |
| 8873 | |
| 8874 | // If we don't have a function type, just build one from nothing. |
| 8875 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8876 | FunctionProtoType::ExtProtoInfo EPI; |
| 8877 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, 0, CC_Default); |
| 8878 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8879 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8880 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8881 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8882 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8883 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8884 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8885 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 8886 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8887 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8888 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8889 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8890 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8891 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 8892 | |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 8893 | // Issue any analysis-based warnings. |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 8894 | const sema::AnalysisBasedWarnings::Policy &WP = |
| 8895 | AnalysisWarnings.getDefaultPolicy(); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 8896 | AnalysisWarnings.IssueWarnings(WP, Result); |
Ted Kremenek | dbdbaaf | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 8897 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8898 | PopFunctionOrBlockScope(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8899 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8900 | } |
| 8901 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8902 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8903 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8904 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8905 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8906 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8907 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8910 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8911 | Expr *E, TypeSourceInfo *TInfo, |
| 8912 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8913 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8914 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8915 | // Get the va_list type |
| 8916 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8917 | if (VaListType->isArrayType()) { |
| 8918 | // Deal with implicit array decay; for example, on x86-64, |
| 8919 | // va_list is an array, but it's supposed to decay to |
| 8920 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8921 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8922 | // Make sure the input expression also decays appropriately. |
| 8923 | UsualUnaryConversions(E); |
| 8924 | } else { |
| 8925 | // Otherwise, the va_list argument must be an l-value because |
| 8926 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8927 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8928 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8929 | return ExprError(); |
| 8930 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8931 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8932 | if (!E->isTypeDependent() && |
| 8933 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8934 | return ExprError(Diag(E->getLocStart(), |
| 8935 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8936 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8937 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8938 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8939 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8940 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8941 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8942 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8943 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8946 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8947 | // The type of __null will be int or long, depending on the size of |
| 8948 | // pointers on the target. |
| 8949 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8950 | unsigned pw = Context.Target.getPointerWidth(0); |
| 8951 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8952 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8953 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8954 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8955 | else if (pw == Context.Target.getLongLongWidth()) |
| 8956 | Ty = Context.LongLongTy; |
| 8957 | else { |
| 8958 | assert(!"I don't know size of pointer!"); |
| 8959 | Ty = Context.IntTy; |
| 8960 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8961 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8962 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8963 | } |
| 8964 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8965 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8966 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8967 | if (!SemaRef.getLangOptions().ObjC1) |
| 8968 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8969 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8970 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8971 | if (!PT) |
| 8972 | return; |
| 8973 | |
| 8974 | // Check if the destination is of type 'id'. |
| 8975 | if (!PT->isObjCIdType()) { |
| 8976 | // Check if the destination is the 'NSString' interface. |
| 8977 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8978 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8979 | return; |
| 8980 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8981 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8982 | // Strip off any parens and casts. |
| 8983 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 8984 | if (!SL || SL->isWide()) |
| 8985 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8986 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8987 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8988 | } |
| 8989 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8990 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8991 | SourceLocation Loc, |
| 8992 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8993 | Expr *SrcExpr, AssignmentAction Action, |
| 8994 | bool *Complained) { |
| 8995 | if (Complained) |
| 8996 | *Complained = false; |
| 8997 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8998 | // Decode the result (notice that AST's are still created for extensions). |
| 8999 | bool isInvalid = false; |
| 9000 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9001 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9002 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9003 | switch (ConvTy) { |
| 9004 | default: assert(0 && "Unknown conversion type"); |
| 9005 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9006 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9007 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9008 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9009 | case IntToPointer: |
| 9010 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9011 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9012 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9013 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9014 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 9015 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9016 | case IncompatiblePointerSign: |
| 9017 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9018 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9019 | case FunctionVoidPointer: |
| 9020 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9021 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9022 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9023 | // Perform array-to-pointer decay if necessary. |
| 9024 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9025 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9026 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9027 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9028 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9029 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9030 | break; |
| 9031 | } |
| 9032 | |
| 9033 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9034 | // fallthrough |
| 9035 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9036 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9037 | // If the qualifiers lost were because we were applying the |
| 9038 | // (deprecated) C++ conversion from a string literal to a char* |
| 9039 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9040 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9041 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9042 | // bit of refactoring (so that the second argument is an |
| 9043 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9044 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9045 | // C++ semantics. |
| 9046 | if (getLangOptions().CPlusPlus && |
| 9047 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9048 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9049 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9050 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9051 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9052 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9053 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9054 | case IntToBlockPointer: |
| 9055 | DiagKind = diag::err_int_to_block_pointer; |
| 9056 | break; |
| 9057 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9058 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9059 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9060 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9061 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9062 | // it can give a more specific diagnostic. |
| 9063 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9064 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9065 | case IncompatibleVectors: |
| 9066 | DiagKind = diag::warn_incompatible_vectors; |
| 9067 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9068 | case Incompatible: |
| 9069 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9070 | isInvalid = true; |
| 9071 | break; |
| 9072 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9073 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9074 | QualType FirstType, SecondType; |
| 9075 | switch (Action) { |
| 9076 | case AA_Assigning: |
| 9077 | case AA_Initializing: |
| 9078 | // The destination type comes first. |
| 9079 | FirstType = DstType; |
| 9080 | SecondType = SrcType; |
| 9081 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9082 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9083 | case AA_Returning: |
| 9084 | case AA_Passing: |
| 9085 | case AA_Converting: |
| 9086 | case AA_Sending: |
| 9087 | case AA_Casting: |
| 9088 | // The source type comes first. |
| 9089 | FirstType = SrcType; |
| 9090 | SecondType = DstType; |
| 9091 | break; |
| 9092 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9093 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9094 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9095 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9096 | if (Complained) |
| 9097 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9098 | return isInvalid; |
| 9099 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9100 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9101 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9102 | llvm::APSInt ICEResult; |
| 9103 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9104 | if (Result) |
| 9105 | *Result = ICEResult; |
| 9106 | return false; |
| 9107 | } |
| 9108 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9109 | Expr::EvalResult EvalResult; |
| 9110 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9111 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9112 | EvalResult.HasSideEffects) { |
| 9113 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9114 | |
| 9115 | if (EvalResult.Diag) { |
| 9116 | // We only show the note if it's not the usual "invalid subexpression" |
| 9117 | // or if it's actually in a subexpression. |
| 9118 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9119 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9120 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9121 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9122 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9123 | return true; |
| 9124 | } |
| 9125 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9126 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9127 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9128 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9129 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9130 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9131 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9132 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9133 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9134 | if (Result) |
| 9135 | *Result = EvalResult.Val.getInt(); |
| 9136 | return false; |
| 9137 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9138 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9139 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9140 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9141 | ExprEvalContexts.push_back( |
| 9142 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9145 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9146 | Sema::PopExpressionEvaluationContext() { |
| 9147 | // Pop the current expression evaluation context off the stack. |
| 9148 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9149 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9150 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9151 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9152 | if (Rec.PotentiallyReferenced) { |
| 9153 | // Mark any remaining declarations in the current position of the stack |
| 9154 | // as "referenced". If they were not meant to be referenced, semantic |
| 9155 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9156 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9157 | I = Rec.PotentiallyReferenced->begin(), |
| 9158 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9159 | I != IEnd; ++I) |
| 9160 | MarkDeclarationReferenced(I->first, I->second); |
| 9161 | } |
| 9162 | |
| 9163 | if (Rec.PotentiallyDiagnosed) { |
| 9164 | // Emit any pending diagnostics. |
| 9165 | for (PotentiallyEmittedDiagnostics::iterator |
| 9166 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9167 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9168 | I != IEnd; ++I) |
| 9169 | Diag(I->first, I->second); |
| 9170 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9171 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9172 | |
| 9173 | // When are coming out of an unevaluated context, clear out any |
| 9174 | // temporaries that we may have created as part of the evaluation of |
| 9175 | // the expression in that context: they aren't relevant because they |
| 9176 | // will never be constructed. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9177 | if (Rec.Context == Unevaluated && |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9178 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 9179 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9180 | ExprTemporaries.end()); |
| 9181 | |
| 9182 | // Destroy the popped expression evaluation record. |
| 9183 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9184 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9185 | |
| 9186 | /// \brief Note that the given declaration was referenced in the source code. |
| 9187 | /// |
| 9188 | /// This routine should be invoke whenever a given declaration is referenced |
| 9189 | /// in the source code, and where that reference occurred. If this declaration |
| 9190 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9191 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9192 | /// |
| 9193 | /// \param Loc the location where the declaration was referenced. |
| 9194 | /// |
| 9195 | /// \param D the declaration that has been referenced by the source code. |
| 9196 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9197 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9198 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9199 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9200 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9201 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 9202 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 9203 | // template or not. The reason for this is that unevaluated expressions |
| 9204 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 9205 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9206 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9207 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9208 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9209 | return; |
| 9210 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9211 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9212 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9213 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9214 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9215 | // Do not mark anything as "used" within a dependent context; wait for |
| 9216 | // an instantiation. |
| 9217 | if (CurContext->isDependentContext()) |
| 9218 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9219 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9220 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9221 | case Unevaluated: |
| 9222 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9223 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9224 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9225 | case PotentiallyEvaluated: |
| 9226 | // We are in a potentially-evaluated expression, so this declaration is |
| 9227 | // "used"; handle this below. |
| 9228 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9229 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9230 | case PotentiallyPotentiallyEvaluated: |
| 9231 | // We are in an expression that may be potentially evaluated; queue this |
| 9232 | // declaration reference until we know whether the expression is |
| 9233 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9234 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9235 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9236 | |
| 9237 | case PotentiallyEvaluatedIfUsed: |
| 9238 | // Referenced declarations will only be used if the construct in the |
| 9239 | // containing expression is used. |
| 9240 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9242 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9243 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9244 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9245 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 9246 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9247 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 9248 | return; |
| 9249 | if (!Constructor->isUsed(false)) |
| 9250 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9251 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 9252 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9253 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9254 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 9255 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9256 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9257 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9258 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9259 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9260 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9261 | if (Destructor->isVirtual()) |
| 9262 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9263 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 9264 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 9265 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9266 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9267 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9268 | } else if (MethodDecl->isVirtual()) |
| 9269 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9270 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9271 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9272 | // Recursive functions should be marked when used from another function. |
| 9273 | if (CurContext == Function) return; |
| 9274 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9275 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9276 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9277 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9278 | bool AlreadyInstantiated = false; |
| 9279 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9280 | = Function->getTemplateSpecializationInfo()) { |
| 9281 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9282 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9283 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9284 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9285 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9286 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9287 | = Function->getMemberSpecializationInfo()) { |
| 9288 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9289 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9290 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9291 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9292 | AlreadyInstantiated = true; |
| 9293 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9294 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9295 | if (!AlreadyInstantiated) { |
| 9296 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9297 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9298 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9299 | Loc)); |
| 9300 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9301 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9302 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9303 | } else { |
| 9304 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9305 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9306 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9307 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9308 | MarkDeclarationReferenced(Loc, *i); |
| 9309 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9310 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9311 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9312 | // Keep track of used but undefined functions. |
| 9313 | if (!Function->isPure() && !Function->hasBody() && |
| 9314 | Function->getLinkage() != ExternalLinkage) { |
| 9315 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9316 | if (old.isInvalid()) old = Loc; |
| 9317 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9318 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9319 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9320 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9322 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9323 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9324 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9325 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9326 | Var->getInstantiatedFromStaticDataMember()) { |
| 9327 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9328 | assert(MSInfo && "Missing member specialization information?"); |
| 9329 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9330 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9331 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9332 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9333 | } |
| 9334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9335 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9336 | // Keep track of used but undefined variables. We make a hole in |
| 9337 | // the warning for static const data members with in-line |
| 9338 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9339 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9340 | && Var->getLinkage() != ExternalLinkage |
| 9341 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9342 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9343 | if (old.isInvalid()) old = Loc; |
| 9344 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9345 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9346 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9347 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9348 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9349 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9350 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9351 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9352 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9353 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9354 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9355 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9356 | Sema &S; |
| 9357 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9358 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9359 | public: |
| 9360 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9361 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9362 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9363 | |
| 9364 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9365 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9366 | }; |
| 9367 | } |
| 9368 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9369 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9370 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9371 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9372 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9373 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9374 | |
| 9375 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9376 | } |
| 9377 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9378 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9379 | if (ClassTemplateSpecializationDecl *Spec |
| 9380 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9381 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9382 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9383 | } |
| 9384 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9385 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9386 | } |
| 9387 | |
| 9388 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9389 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9390 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9391 | } |
| 9392 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9393 | namespace { |
| 9394 | /// \brief Helper class that marks all of the declarations referenced by |
| 9395 | /// potentially-evaluated subexpressions as "referenced". |
| 9396 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9397 | Sema &S; |
| 9398 | |
| 9399 | public: |
| 9400 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9401 | |
| 9402 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9403 | |
| 9404 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9405 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9406 | } |
| 9407 | |
| 9408 | void VisitMemberExpr(MemberExpr *E) { |
| 9409 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9410 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
| 9413 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9414 | if (E->getConstructor()) |
| 9415 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9416 | if (E->getOperatorNew()) |
| 9417 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9418 | if (E->getOperatorDelete()) |
| 9419 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9420 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9421 | } |
| 9422 | |
| 9423 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9424 | if (E->getOperatorDelete()) |
| 9425 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9426 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9427 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9428 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9429 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9430 | S.LookupDestructor(Record)); |
| 9431 | } |
| 9432 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9433 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
| 9436 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9437 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9438 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9439 | } |
| 9440 | |
| 9441 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9442 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9443 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9444 | |
| 9445 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9446 | Visit(E->getExpr()); |
| 9447 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9448 | }; |
| 9449 | } |
| 9450 | |
| 9451 | /// \brief Mark any declarations that appear within this expression or any |
| 9452 | /// potentially-evaluated subexpressions as "referenced". |
| 9453 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9454 | EvaluatedExprMarker(*this).Visit(E); |
| 9455 | } |
| 9456 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9457 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9458 | /// of the program being compiled. |
| 9459 | /// |
| 9460 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9461 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9462 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9463 | /// expressions, code used only during overload resolution, etc., are not |
| 9464 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9465 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9466 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9467 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9468 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9469 | /// This routine should be used for all diagnostics that describe the run-time |
| 9470 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9471 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9472 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame^] | 9473 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9474 | const PartialDiagnostic &PD) { |
| 9475 | switch (ExprEvalContexts.back().Context ) { |
| 9476 | case Unevaluated: |
| 9477 | // The argument will never be evaluated, so don't complain. |
| 9478 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9479 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9480 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9481 | case PotentiallyEvaluatedIfUsed: |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9482 | Diag(Loc, PD); |
| 9483 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9484 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9485 | case PotentiallyPotentiallyEvaluated: |
| 9486 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9487 | break; |
| 9488 | } |
| 9489 | |
| 9490 | return false; |
| 9491 | } |
| 9492 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9493 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9494 | CallExpr *CE, FunctionDecl *FD) { |
| 9495 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9496 | return false; |
| 9497 | |
| 9498 | PartialDiagnostic Note = |
| 9499 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9500 | << FD->getDeclName() : PDiag(); |
| 9501 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9502 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9503 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9504 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9505 | PDiag(diag::err_call_function_incomplete_return) |
| 9506 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9507 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9508 | << CE->getSourceRange(), |
| 9509 | std::make_pair(NoteLoc, Note))) |
| 9510 | return true; |
| 9511 | |
| 9512 | return false; |
| 9513 | } |
| 9514 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9515 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9516 | // will prevent this condition from triggering, which is what we want. |
| 9517 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9518 | SourceLocation Loc; |
| 9519 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9520 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9521 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9522 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9523 | if (isa<BinaryOperator>(E)) { |
| 9524 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9525 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9526 | return; |
| 9527 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9528 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9529 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9530 | // Greylist some idioms by putting them into a warning subcategory. |
| 9531 | if (ObjCMessageExpr *ME |
| 9532 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9533 | Selector Sel = ME->getSelector(); |
| 9534 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9535 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9536 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9537 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9538 | |
| 9539 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9540 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9541 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9542 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9543 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9544 | Loc = Op->getOperatorLoc(); |
| 9545 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9546 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9547 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9548 | return; |
| 9549 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9550 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9551 | Loc = Op->getOperatorLoc(); |
| 9552 | } else { |
| 9553 | // Not an assignment. |
| 9554 | return; |
| 9555 | } |
| 9556 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9557 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 9558 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9559 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9560 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9561 | |
| 9562 | if (IsOrAssign) |
| 9563 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9564 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9565 | else |
| 9566 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9567 | << FixItHint::CreateReplacement(Loc, "=="); |
| 9568 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9569 | Diag(Loc, diag::note_condition_assign_silence) |
| 9570 | << FixItHint::CreateInsertion(Open, "(") |
| 9571 | << FixItHint::CreateInsertion(Close, ")"); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9572 | } |
| 9573 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9574 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9575 | /// that the user intended an assignment used as condition. |
| 9576 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9577 | // Don't warn if the parens came from a macro. |
| 9578 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9579 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9580 | return; |
| 9581 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9582 | Expr *E = parenE->IgnoreParens(); |
| 9583 | |
| 9584 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9585 | if (opE->getOpcode() == BO_EQ && |
| 9586 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9587 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9588 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9589 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9590 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
| 9591 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9592 | << FixItHint::CreateReplacement(Loc, "="); |
| 9593 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9594 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9595 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9596 | } |
| 9597 | } |
| 9598 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9599 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 9600 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9601 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9602 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9603 | |
| 9604 | if (!E->isTypeDependent()) { |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9605 | if (E->isBoundMemberFunction(Context)) |
| 9606 | return Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9607 | << E->getSourceRange(); |
| 9608 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9609 | if (getLangOptions().CPlusPlus) |
| 9610 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9611 | |
| 9612 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9613 | |
| 9614 | QualType T = E->getType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9615 | if (!T->isScalarType()) // C99 6.8.4.1p1 |
| 9616 | return Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9617 | << T << E->getSourceRange(); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9618 | } |
| 9619 | |
| 9620 | return false; |
| 9621 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9622 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9623 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9624 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9625 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9626 | return ExprError(); |
| 9627 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9628 | if (CheckBooleanCondition(Sub, Loc)) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9629 | return ExprError(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9630 | |
| 9631 | return Owned(Sub); |
| 9632 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9633 | |
| 9634 | /// Check for operands with placeholder types and complain if found. |
| 9635 | /// Returns true if there was an error and no recovery was possible. |
| 9636 | ExprResult Sema::CheckPlaceholderExpr(Expr *E, SourceLocation Loc) { |
| 9637 | const BuiltinType *BT = E->getType()->getAs<BuiltinType>(); |
| 9638 | if (!BT || !BT->isPlaceholderType()) return Owned(E); |
| 9639 | |
| 9640 | // If this is overload, check for a single overload. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9641 | assert(BT->getKind() == BuiltinType::Overload); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9642 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9643 | if (FunctionDecl *Specialization |
| 9644 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
| 9645 | // The access doesn't really matter in this case. |
| 9646 | DeclAccessPair Found = DeclAccessPair::make(Specialization, |
| 9647 | Specialization->getAccess()); |
| 9648 | E = FixOverloadedFunctionReference(E, Found, Specialization); |
| 9649 | if (!E) return ExprError(); |
| 9650 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9651 | } |
| 9652 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9653 | Diag(Loc, diag::err_ovl_unresolvable) << E->getSourceRange(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9654 | return ExprError(); |
| 9655 | } |