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" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Lex/LiteralSupport.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 34 | #include "clang/Sema/DeclSpec.h" |
| 35 | #include "clang/Sema/Designator.h" |
| 36 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 38 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 39 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 44 | /// \brief Determine whether the use of this declaration is valid, and |
| 45 | /// emit any corresponding diagnostics. |
| 46 | /// |
| 47 | /// This routine diagnoses various problems with referencing |
| 48 | /// declarations that can occur when using a declaration. For example, |
| 49 | /// it might warn if a deprecated or unavailable declaration is being |
| 50 | /// used, or produce an error (and return true) if a C++0x deleted |
| 51 | /// function is being used. |
| 52 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 53 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 54 | /// decls. |
| 55 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 56 | /// \returns true if there was an error (this declaration cannot be |
| 57 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 58 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 59 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 60 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 61 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 62 | // If there were any diagnostics suppressed by template argument deduction, |
| 63 | // emit them now. |
| 64 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 65 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 66 | if (Pos != SuppressedDiagnostics.end()) { |
| 67 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
| 68 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 69 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 70 | |
| 71 | // Clear out the list of suppressed diagnostics, so that we don't emit |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 72 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 73 | // entry from the table, because we want to avoid ever emitting these |
| 74 | // diagnostics again. |
| 75 | Suppressed.clear(); |
| 76 | } |
| 77 | } |
| 78 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 79 | // 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] | 80 | if (ParsingInitForAutoVars.count(D)) { |
| 81 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 82 | << D->getDeclName(); |
| 83 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 86 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 87 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | if (FD->isDeleted()) { |
| 89 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 90 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 91 | return true; |
| 92 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 93 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 95 | // See if this declaration is unavailable or deprecated. |
| 96 | std::string Message; |
| 97 | switch (D->getAvailability(&Message)) { |
| 98 | case AR_Available: |
| 99 | case AR_NotYetIntroduced: |
| 100 | break; |
| 101 | |
| 102 | case AR_Deprecated: |
| 103 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 104 | break; |
| 105 | |
| 106 | case AR_Unavailable: |
Argyrios Kyrtzidis | 12189f5 | 2011-06-17 17:28:30 +0000 | [diff] [blame] | 107 | if (cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) { |
| 108 | if (Message.empty()) { |
| 109 | if (!UnknownObjCClass) |
| 110 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 111 | else |
| 112 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 113 | << D->getDeclName(); |
| 114 | } |
| 115 | else |
| 116 | Diag(Loc, diag::err_unavailable_message) |
| 117 | << D->getDeclName() << Message; |
| 118 | Diag(D->getLocation(), diag::note_unavailable_here) |
| 119 | << isa<FunctionDecl>(D) << false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 120 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 124 | // Warn if this is used but marked unused. |
| 125 | if (D->hasAttr<UnusedAttr>()) |
| 126 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 127 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 128 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 131 | /// \brief Retrieve the message suffix that should be added to a |
| 132 | /// diagnostic complaining about the given function being deleted or |
| 133 | /// unavailable. |
| 134 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 135 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 136 | // detected here so that we could improve diagnostics to say, e.g., |
| 137 | // "base class 'A' had a deleted copy constructor". |
| 138 | if (FD->isDeleted()) |
| 139 | return std::string(); |
| 140 | |
| 141 | std::string Message; |
| 142 | if (FD->getAvailability(&Message)) |
| 143 | return ": " + Message; |
| 144 | |
| 145 | return std::string(); |
| 146 | } |
| 147 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 148 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 150 | /// attribute. It warns if call does not have the sentinel argument. |
| 151 | /// |
| 152 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 154 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 156 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 157 | |
| 158 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 159 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 160 | int sentinelPos = attr->getSentinel(); |
| 161 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 163 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 164 | // 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] | 165 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 166 | bool warnNotEnoughArgs = false; |
| 167 | int isMethod = 0; |
| 168 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 169 | // skip over named parameters. |
| 170 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 171 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 172 | if (nullPos) |
| 173 | --nullPos; |
| 174 | else |
| 175 | ++i; |
| 176 | } |
| 177 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 178 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 179 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 180 | // skip over named parameters. |
| 181 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 182 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 183 | if (nullPos) |
| 184 | --nullPos; |
| 185 | else |
| 186 | ++i; |
| 187 | } |
| 188 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 189 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 190 | // block or function pointer call. |
| 191 | QualType Ty = V->getType(); |
| 192 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 194 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 195 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 196 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 197 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 198 | unsigned k; |
| 199 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 200 | if (nullPos) |
| 201 | --nullPos; |
| 202 | else |
| 203 | ++i; |
| 204 | } |
| 205 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 206 | } |
| 207 | if (Ty->isBlockPointerType()) |
| 208 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 209 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 210 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 211 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 212 | return; |
| 213 | |
| 214 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 215 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 216 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | int sentinel = i; |
| 220 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 221 | --sentinelPos; |
| 222 | ++i; |
| 223 | } |
| 224 | if (sentinelPos > 0) { |
| 225 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 226 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | while (i < NumArgs-1) { |
| 230 | ++i; |
| 231 | ++sentinel; |
| 232 | } |
| 233 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 234 | if (!sentinelExpr) return; |
| 235 | if (sentinelExpr->isTypeDependent()) return; |
| 236 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 237 | |
| 238 | // nullptr_t is always treated as null. |
| 239 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 240 | |
Fariborz Jahanian | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 241 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 242 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 243 | Expr::NPC_ValueDependentIsNull)) |
| 244 | return; |
| 245 | |
| 246 | // Unfortunately, __null has type 'int'. |
| 247 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 248 | |
| 249 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 250 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 253 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 254 | Expr *Ex = (Expr *)E; |
| 255 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 256 | } |
| 257 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 258 | //===----------------------------------------------------------------------===// |
| 259 | // Standard Promotions and Conversions |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 262 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 263 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 264 | QualType Ty = E->getType(); |
| 265 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 266 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 267 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 268 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 269 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 270 | else if (Ty->isArrayType()) { |
| 271 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 272 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 273 | // type 'array of type' is converted to an expression that has type 'pointer |
| 274 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 275 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 276 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 277 | // |
| 278 | // C++ 4.2p1: |
| 279 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 280 | // T" can be converted to an rvalue of type "pointer to T". |
| 281 | // |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 282 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 283 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 284 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 285 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 286 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 289 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 290 | // Check to see if we are dereferencing a null pointer. If so, |
| 291 | // and if not volatile-qualified, this is undefined behavior that the |
| 292 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 293 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 294 | // only handles the pattern "*null", which is a very syntactic check. |
| 295 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 296 | if (UO->getOpcode() == UO_Deref && |
| 297 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 298 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 299 | !UO->getType().isVolatileQualified()) { |
| 300 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 301 | S.PDiag(diag::warn_indirection_through_null) |
| 302 | << UO->getSubExpr()->getSourceRange()); |
| 303 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 304 | S.PDiag(diag::note_indirection_through_null)); |
| 305 | } |
| 306 | } |
| 307 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 308 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 309 | // C++ [conv.lval]p1: |
| 310 | // A glvalue of a non-function, non-array type T can be |
| 311 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 312 | if (!E->isGLValue()) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 313 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 314 | QualType T = E->getType(); |
| 315 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 316 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 317 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 318 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 319 | ExprResult Res = ConvertPropertyForRValue(E); |
| 320 | if (Res.isInvalid()) |
| 321 | return Owned(E); |
| 322 | E = Res.take(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 323 | if (!E->isGLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 324 | return Owned(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 325 | } |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 326 | |
| 327 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 328 | // expressions of certain types in C++. |
| 329 | if (getLangOptions().CPlusPlus && |
| 330 | (E->getType() == Context.OverloadTy || |
| 331 | T->isDependentType() || |
| 332 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 333 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 334 | |
| 335 | // The C standard is actually really unclear on this point, and |
| 336 | // DR106 tells us what the result should be but not why. It's |
| 337 | // generally best to say that void types just doesn't undergo |
| 338 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 339 | // 'void' type are never l-values, but qualified void can be. |
| 340 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 341 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 342 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 343 | CheckForNullPointerDereference(*this, E); |
| 344 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 345 | // C++ [conv.lval]p1: |
| 346 | // [...] If T is a non-class type, the type of the prvalue is the |
| 347 | // cv-unqualified version of T. Otherwise, the type of the |
| 348 | // rvalue is T. |
| 349 | // |
| 350 | // C99 6.3.2.1p2: |
| 351 | // If the lvalue has qualified type, the value has the unqualified |
| 352 | // version of the type of the lvalue; otherwise, the value has the |
| 353 | // type of the lvalue. |
| 354 | if (T.hasQualifiers()) |
| 355 | T = T.getUnqualifiedType(); |
| 356 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 357 | CheckArrayAccess(E); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 358 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 359 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 360 | E, 0, VK_RValue)); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 361 | } |
| 362 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 363 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 364 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 365 | if (Res.isInvalid()) |
| 366 | return ExprError(); |
| 367 | Res = DefaultLvalueConversion(Res.take()); |
| 368 | if (Res.isInvalid()) |
| 369 | return ExprError(); |
| 370 | return move(Res); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 374 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 376 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 377 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 378 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 379 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 380 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 381 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 382 | if (Res.isInvalid()) |
| 383 | return Owned(E); |
| 384 | E = Res.take(); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 385 | |
| 386 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 387 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 388 | |
| 389 | // Try to perform integral promotions if the object has a theoretically |
| 390 | // promotable type. |
| 391 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 392 | // C99 6.3.1.1p2: |
| 393 | // |
| 394 | // The following may be used in an expression wherever an int or |
| 395 | // unsigned int may be used: |
| 396 | // - an object or expression with an integer type whose integer |
| 397 | // conversion rank is less than or equal to the rank of int |
| 398 | // and unsigned int. |
| 399 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 400 | // |
| 401 | // If an int can represent all values of the original type, the |
| 402 | // value is converted to an int; otherwise, it is converted to an |
| 403 | // unsigned int. These are called the integer promotions. All |
| 404 | // other types are unchanged by the integer promotions. |
| 405 | |
| 406 | QualType PTy = Context.isPromotableBitField(E); |
| 407 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 408 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 409 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 410 | } |
| 411 | if (Ty->isPromotableIntegerType()) { |
| 412 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 413 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 414 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 415 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 416 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 417 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 420 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | /// 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] | 422 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 423 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 424 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 425 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 427 | ExprResult Res = UsualUnaryConversions(E); |
| 428 | if (Res.isInvalid()) |
| 429 | return Owned(E); |
| 430 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 432 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 433 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 434 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 435 | |
| 436 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 439 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 440 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 441 | /// interfaces passed by value. |
| 442 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 443 | FunctionDecl *FDecl) { |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 444 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 445 | if (ExprRes.isInvalid()) |
| 446 | return ExprError(); |
| 447 | |
| 448 | ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 449 | if (ExprRes.isInvalid()) |
| 450 | return ExprError(); |
| 451 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 453 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 454 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 455 | // etc. |
| 456 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 457 | return Owned(E); |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 459 | // Don't allow one to pass an Objective-C interface to a vararg. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 460 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 461 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 462 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 463 | << E->getType() << CT)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 464 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 465 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 466 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 467 | // C++0x [expr.call]p7: |
| 468 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 469 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 470 | // or a non-trivial destructor, with no corresponding parameter, |
| 471 | // is conditionally-supported with implementation-defined semantics. |
| 472 | bool TrivialEnough = false; |
| 473 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 474 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 475 | if (Record->hasTrivialCopyConstructor() && |
| 476 | Record->hasTrivialMoveConstructor() && |
| 477 | Record->hasTrivialDestructor()) |
| 478 | TrivialEnough = true; |
| 479 | } |
| 480 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 481 | |
| 482 | if (!TrivialEnough && |
| 483 | getLangOptions().ObjCAutoRefCount && |
| 484 | E->getType()->isObjCLifetimeType()) |
| 485 | TrivialEnough = true; |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 486 | |
| 487 | if (TrivialEnough) { |
| 488 | // Nothing to diagnose. This is okay. |
| 489 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 490 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 491 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 492 | << CT)) { |
| 493 | // Turn this into a trap. |
| 494 | CXXScopeSpec SS; |
| 495 | UnqualifiedId Name; |
| 496 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 497 | E->getLocStart()); |
| 498 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 499 | if (TrapFn.isInvalid()) |
| 500 | return ExprError(); |
| 501 | |
| 502 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 503 | MultiExprArg(), E->getLocEnd()); |
| 504 | if (Call.isInvalid()) |
| 505 | return ExprError(); |
| 506 | |
| 507 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 508 | Call.get(), E); |
| 509 | if (Comma.isInvalid()) |
| 510 | return ExprError(); |
| 511 | |
| 512 | E = Comma.get(); |
| 513 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 514 | } |
| 515 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 516 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 519 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 520 | /// 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] | 521 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 522 | /// responsible for emitting appropriate error diagnostics. |
| 523 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 524 | /// GCC. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 525 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 526 | bool isCompAssign) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 527 | if (!isCompAssign) { |
| 528 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 529 | if (lhsExpr.isInvalid()) |
| 530 | return QualType(); |
| 531 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 532 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 533 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 534 | if (rhsExpr.isInvalid()) |
| 535 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 538 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 539 | QualType lhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 540 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | QualType rhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 542 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 543 | |
| 544 | // If both types are identical, no conversion is needed. |
| 545 | if (lhs == rhs) |
| 546 | return lhs; |
| 547 | |
| 548 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 549 | // The caller can deal with this (e.g. pointer + int). |
| 550 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 551 | return lhs; |
| 552 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 553 | // Apply unary and bitfield promotions to the LHS's type. |
| 554 | QualType lhs_unpromoted = lhs; |
| 555 | if (lhs->isPromotableIntegerType()) |
| 556 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 557 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 558 | if (!LHSBitfieldPromoteTy.isNull()) |
| 559 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 560 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 561 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 562 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 563 | // If both types are identical, no conversion is needed. |
| 564 | if (lhs == rhs) |
| 565 | return lhs; |
| 566 | |
| 567 | // At this point, we have two different arithmetic types. |
| 568 | |
| 569 | // Handle complex types first (C99 6.3.1.8p1). |
| 570 | bool LHSComplexFloat = lhs->isComplexType(); |
| 571 | bool RHSComplexFloat = rhs->isComplexType(); |
| 572 | if (LHSComplexFloat || RHSComplexFloat) { |
| 573 | // if we have an integer operand, the result is the complex type. |
| 574 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 575 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 576 | if (rhs->isIntegerType()) { |
| 577 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 578 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 579 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 580 | } else { |
| 581 | assert(rhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 582 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 583 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 584 | return lhs; |
| 585 | } |
| 586 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 587 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 588 | if (!isCompAssign) { |
| 589 | // int -> float -> _Complex float |
| 590 | if (lhs->isIntegerType()) { |
| 591 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 592 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 593 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 594 | } else { |
| 595 | assert(lhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 596 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 599 | return rhs; |
| 600 | } |
| 601 | |
| 602 | // This handles complex/complex, complex/float, or float/complex. |
| 603 | // When both operands are complex, the shorter operand is converted to the |
| 604 | // type of the longer, and that is the type of the result. This corresponds |
| 605 | // to what is done when combining two real floating-point operands. |
| 606 | // The fun begins when size promotion occur across type domains. |
| 607 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 608 | // floating-point type, the less precise type is converted, within it's |
| 609 | // real or complex domain, to the precision of the other type. For example, |
| 610 | // when combining a "long double" with a "double _Complex", the |
| 611 | // "double _Complex" is promoted to "long double _Complex". |
| 612 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 613 | |
| 614 | // If both are complex, just cast to the more precise type. |
| 615 | if (LHSComplexFloat && RHSComplexFloat) { |
| 616 | if (order > 0) { |
| 617 | // _Complex float -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 618 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 619 | return lhs; |
| 620 | |
| 621 | } else if (order < 0) { |
| 622 | // _Complex float -> _Complex double |
| 623 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 624 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 625 | return rhs; |
| 626 | } |
| 627 | return lhs; |
| 628 | } |
| 629 | |
| 630 | // If just the LHS is complex, the RHS needs to be converted, |
| 631 | // and the LHS might need to be promoted. |
| 632 | if (LHSComplexFloat) { |
| 633 | if (order > 0) { // LHS is wider |
| 634 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 635 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 636 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 637 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 638 | return lhs; |
| 639 | } |
| 640 | |
| 641 | // RHS is at least as wide. Find its corresponding complex type. |
| 642 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 643 | |
| 644 | // double -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 645 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 646 | |
| 647 | // _Complex float -> _Complex double |
| 648 | if (!isCompAssign && order < 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 649 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 650 | |
| 651 | return result; |
| 652 | } |
| 653 | |
| 654 | // Just the RHS is complex, so the LHS needs to be converted |
| 655 | // and the RHS might need to be promoted. |
| 656 | assert(RHSComplexFloat); |
| 657 | |
| 658 | if (order < 0) { // RHS is wider |
| 659 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 660 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 661 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 662 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 663 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 664 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 665 | return rhs; |
| 666 | } |
| 667 | |
| 668 | // LHS is at least as wide. Find its corresponding complex type. |
| 669 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 670 | |
| 671 | // double -> _Complex double |
| 672 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 673 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 674 | |
| 675 | // _Complex float -> _Complex double |
| 676 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 677 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 678 | |
| 679 | return result; |
| 680 | } |
| 681 | |
| 682 | // Now handle "real" floating types (i.e. float, double, long double). |
| 683 | bool LHSFloat = lhs->isRealFloatingType(); |
| 684 | bool RHSFloat = rhs->isRealFloatingType(); |
| 685 | if (LHSFloat || RHSFloat) { |
| 686 | // If we have two real floating types, convert the smaller operand |
| 687 | // to the bigger result. |
| 688 | if (LHSFloat && RHSFloat) { |
| 689 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 690 | if (order > 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 691 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 692 | return lhs; |
| 693 | } |
| 694 | |
| 695 | assert(order < 0 && "illegal float comparison"); |
| 696 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 697 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 698 | return rhs; |
| 699 | } |
| 700 | |
| 701 | // If we have an integer operand, the result is the real floating type. |
| 702 | if (LHSFloat) { |
| 703 | if (rhs->isIntegerType()) { |
| 704 | // Convert rhs to the lhs floating point type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 705 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 706 | return lhs; |
| 707 | } |
| 708 | |
| 709 | // Convert both sides to the appropriate complex float. |
| 710 | assert(rhs->isComplexIntegerType()); |
| 711 | QualType result = Context.getComplexType(lhs); |
| 712 | |
| 713 | // _Complex int -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 714 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 715 | |
| 716 | // float -> _Complex float |
| 717 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 718 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 719 | |
| 720 | return result; |
| 721 | } |
| 722 | |
| 723 | assert(RHSFloat); |
| 724 | if (lhs->isIntegerType()) { |
| 725 | // Convert lhs to the rhs floating point type. |
| 726 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 727 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 728 | return rhs; |
| 729 | } |
| 730 | |
| 731 | // Convert both sides to the appropriate complex float. |
| 732 | assert(lhs->isComplexIntegerType()); |
| 733 | QualType result = Context.getComplexType(rhs); |
| 734 | |
| 735 | // _Complex int -> _Complex float |
| 736 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 737 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 738 | |
| 739 | // float -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 740 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 741 | |
| 742 | return result; |
| 743 | } |
| 744 | |
| 745 | // Handle GCC complex int extension. |
| 746 | // FIXME: if the operands are (int, _Complex long), we currently |
| 747 | // don't promote the complex. Also, signedness? |
| 748 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 749 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 750 | if (lhsComplexInt && rhsComplexInt) { |
| 751 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 752 | rhsComplexInt->getElementType()); |
| 753 | assert(order && "inequal types with equal element ordering"); |
| 754 | if (order > 0) { |
| 755 | // _Complex int -> _Complex long |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 756 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 757 | return lhs; |
| 758 | } |
| 759 | |
| 760 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 761 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 762 | return rhs; |
| 763 | } else if (lhsComplexInt) { |
| 764 | // int -> _Complex int |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 765 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 766 | return lhs; |
| 767 | } else if (rhsComplexInt) { |
| 768 | // int -> _Complex int |
| 769 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 770 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 771 | return rhs; |
| 772 | } |
| 773 | |
| 774 | // Finally, we have two differing integer types. |
| 775 | // The rules for this case are in C99 6.3.1.8 |
| 776 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 777 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 778 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 779 | if (lhsSigned == rhsSigned) { |
| 780 | // Same signedness; use the higher-ranked type |
| 781 | if (compare >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 782 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 783 | return lhs; |
| 784 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 785 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 786 | return rhs; |
| 787 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 788 | // The unsigned type has greater than or equal rank to the |
| 789 | // signed type, so use the unsigned type |
| 790 | if (rhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 791 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 792 | return lhs; |
| 793 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 794 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 795 | return rhs; |
| 796 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 797 | // The two types are different widths; if we are here, that |
| 798 | // means the signed type is larger than the unsigned type, so |
| 799 | // use the signed type. |
| 800 | if (lhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 801 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 802 | return lhs; |
| 803 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 804 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 805 | return rhs; |
| 806 | } else { |
| 807 | // The signed type is higher-ranked than the unsigned type, |
| 808 | // but isn't actually any bigger (like unsigned int and long |
| 809 | // on most 32-bit systems). Use the unsigned type corresponding |
| 810 | // to the signed type. |
| 811 | QualType result = |
| 812 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 813 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 814 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 815 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 816 | return result; |
| 817 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 820 | //===----------------------------------------------------------------------===// |
| 821 | // Semantic Analysis for various Expression Types |
| 822 | //===----------------------------------------------------------------------===// |
| 823 | |
| 824 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 825 | ExprResult |
| 826 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 827 | SourceLocation DefaultLoc, |
| 828 | SourceLocation RParenLoc, |
| 829 | Expr *ControllingExpr, |
| 830 | MultiTypeArg types, |
| 831 | MultiExprArg exprs) { |
| 832 | unsigned NumAssocs = types.size(); |
| 833 | assert(NumAssocs == exprs.size()); |
| 834 | |
| 835 | ParsedType *ParsedTypes = types.release(); |
| 836 | Expr **Exprs = exprs.release(); |
| 837 | |
| 838 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 839 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 840 | if (ParsedTypes[i]) |
| 841 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 842 | else |
| 843 | Types[i] = 0; |
| 844 | } |
| 845 | |
| 846 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 847 | ControllingExpr, Types, Exprs, |
| 848 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 849 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 850 | return ER; |
| 851 | } |
| 852 | |
| 853 | ExprResult |
| 854 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 855 | SourceLocation DefaultLoc, |
| 856 | SourceLocation RParenLoc, |
| 857 | Expr *ControllingExpr, |
| 858 | TypeSourceInfo **Types, |
| 859 | Expr **Exprs, |
| 860 | unsigned NumAssocs) { |
| 861 | bool TypeErrorFound = false, |
| 862 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 863 | ContainsUnexpandedParameterPack |
| 864 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 865 | |
| 866 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 867 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 868 | ContainsUnexpandedParameterPack = true; |
| 869 | |
| 870 | if (Types[i]) { |
| 871 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 872 | ContainsUnexpandedParameterPack = true; |
| 873 | |
| 874 | if (Types[i]->getType()->isDependentType()) { |
| 875 | IsResultDependent = true; |
| 876 | } else { |
| 877 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 878 | // complete object type other than a variably modified type." |
| 879 | unsigned D = 0; |
| 880 | if (Types[i]->getType()->isIncompleteType()) |
| 881 | D = diag::err_assoc_type_incomplete; |
| 882 | else if (!Types[i]->getType()->isObjectType()) |
| 883 | D = diag::err_assoc_type_nonobject; |
| 884 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 885 | D = diag::err_assoc_type_variably_modified; |
| 886 | |
| 887 | if (D != 0) { |
| 888 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 889 | << Types[i]->getTypeLoc().getSourceRange() |
| 890 | << Types[i]->getType(); |
| 891 | TypeErrorFound = true; |
| 892 | } |
| 893 | |
| 894 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 895 | // selection shall specify compatible types." |
| 896 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 897 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 898 | Context.typesAreCompatible(Types[i]->getType(), |
| 899 | Types[j]->getType())) { |
| 900 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 901 | diag::err_assoc_compatible_types) |
| 902 | << Types[j]->getTypeLoc().getSourceRange() |
| 903 | << Types[j]->getType() |
| 904 | << Types[i]->getType(); |
| 905 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 906 | diag::note_compat_assoc) |
| 907 | << Types[i]->getTypeLoc().getSourceRange() |
| 908 | << Types[i]->getType(); |
| 909 | TypeErrorFound = true; |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | if (TypeErrorFound) |
| 915 | return ExprError(); |
| 916 | |
| 917 | // If we determined that the generic selection is result-dependent, don't |
| 918 | // try to compute the result expression. |
| 919 | if (IsResultDependent) |
| 920 | return Owned(new (Context) GenericSelectionExpr( |
| 921 | Context, KeyLoc, ControllingExpr, |
| 922 | Types, Exprs, NumAssocs, DefaultLoc, |
| 923 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 924 | |
| 925 | llvm::SmallVector<unsigned, 1> CompatIndices; |
| 926 | unsigned DefaultIndex = -1U; |
| 927 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 928 | if (!Types[i]) |
| 929 | DefaultIndex = i; |
| 930 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 931 | Types[i]->getType())) |
| 932 | CompatIndices.push_back(i); |
| 933 | } |
| 934 | |
| 935 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 936 | // type compatible with at most one of the types named in its generic |
| 937 | // association list." |
| 938 | if (CompatIndices.size() > 1) { |
| 939 | // We strip parens here because the controlling expression is typically |
| 940 | // parenthesized in macro definitions. |
| 941 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 942 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 943 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 944 | << (unsigned) CompatIndices.size(); |
| 945 | for (llvm::SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
| 946 | E = CompatIndices.end(); I != E; ++I) { |
| 947 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 948 | diag::note_compat_assoc) |
| 949 | << Types[*I]->getTypeLoc().getSourceRange() |
| 950 | << Types[*I]->getType(); |
| 951 | } |
| 952 | return ExprError(); |
| 953 | } |
| 954 | |
| 955 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 956 | // its controlling expression shall have type compatible with exactly one of |
| 957 | // the types named in its generic association list." |
| 958 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 959 | // We strip parens here because the controlling expression is typically |
| 960 | // parenthesized in macro definitions. |
| 961 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 962 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 963 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 964 | return ExprError(); |
| 965 | } |
| 966 | |
| 967 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 968 | // type name that is compatible with the type of the controlling expression, |
| 969 | // then the result expression of the generic selection is the expression |
| 970 | // in that generic association. Otherwise, the result expression of the |
| 971 | // generic selection is the expression in the default generic association." |
| 972 | unsigned ResultIndex = |
| 973 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 974 | |
| 975 | return Owned(new (Context) GenericSelectionExpr( |
| 976 | Context, KeyLoc, ControllingExpr, |
| 977 | Types, Exprs, NumAssocs, DefaultLoc, |
| 978 | RParenLoc, ContainsUnexpandedParameterPack, |
| 979 | ResultIndex)); |
| 980 | } |
| 981 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 982 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 984 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 985 | /// multiple tokens. However, the common case is that StringToks points to one |
| 986 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 987 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 988 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 989 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 990 | assert(NumStringToks && "Must have at least one string!"); |
| 991 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 992 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 994 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | |
| 996 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 997 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 998 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 999 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1000 | QualType StrTy = Context.CharTy; |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1001 | if (Literal.AnyWide) |
| 1002 | StrTy = Context.getWCharType(); |
| 1003 | else if (Literal.Pascal) |
| 1004 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1005 | |
| 1006 | // 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] | 1007 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1008 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1010 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1011 | // the nul terminator character as well as the string length for pascal |
| 1012 | // strings. |
| 1013 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1014 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1015 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1017 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1018 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 1019 | Literal.AnyWide, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1020 | &StringTokLocs[0], |
| 1021 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1024 | enum CaptureResult { |
| 1025 | /// No capture is required. |
| 1026 | CR_NoCapture, |
| 1027 | |
| 1028 | /// A capture is required. |
| 1029 | CR_Capture, |
| 1030 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1031 | /// A by-ref capture is required. |
| 1032 | CR_CaptureByRef, |
| 1033 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1034 | /// An error occurred when trying to capture the given variable. |
| 1035 | CR_Error |
| 1036 | }; |
| 1037 | |
| 1038 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1039 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1040 | /// \param var - the variable referenced |
| 1041 | /// \param DC - the context which we couldn't capture through |
| 1042 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1043 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1044 | VarDecl *var, DeclContext *DC) { |
| 1045 | switch (S.ExprEvalContexts.back().Context) { |
| 1046 | case Sema::Unevaluated: |
| 1047 | // The argument will never be evaluated, so don't complain. |
| 1048 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1050 | case Sema::PotentiallyEvaluated: |
| 1051 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1052 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1053 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1054 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1055 | // FIXME: delay these! |
| 1056 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1057 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1059 | // Don't diagnose about capture if we're not actually in code right |
| 1060 | // now; in general, there are more appropriate places that will |
| 1061 | // diagnose this. |
| 1062 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1063 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1064 | // Certain madnesses can happen with parameter declarations, which |
| 1065 | // we want to ignore. |
| 1066 | if (isa<ParmVarDecl>(var)) { |
| 1067 | // - If the parameter still belongs to the translation unit, then |
| 1068 | // we're actually just using one parameter in the declaration of |
| 1069 | // the next. This is useful in e.g. VLAs. |
| 1070 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1071 | return CR_NoCapture; |
| 1072 | |
| 1073 | // - This particular madness can happen in ill-formed default |
| 1074 | // arguments; claim it's okay and let downstream code handle it. |
| 1075 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1076 | return CR_NoCapture; |
| 1077 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1078 | |
| 1079 | DeclarationName functionName; |
| 1080 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1081 | functionName = fn->getDeclName(); |
| 1082 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1083 | |
| 1084 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1085 | << var->getIdentifier() << functionName; |
| 1086 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1087 | << var->getIdentifier(); |
| 1088 | |
| 1089 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1092 | /// There is a well-formed capture at a particular scope level; |
| 1093 | /// propagate it through all the nested blocks. |
| 1094 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1095 | const BlockDecl::Capture &capture) { |
| 1096 | VarDecl *var = capture.getVariable(); |
| 1097 | |
| 1098 | // Update all the inner blocks with the capture information. |
| 1099 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1100 | i != e; ++i) { |
| 1101 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1102 | innerBlock->Captures.push_back( |
| 1103 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1104 | /*nested*/ true, capture.getCopyExpr())); |
| 1105 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1106 | } |
| 1107 | |
| 1108 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1109 | } |
| 1110 | |
| 1111 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1112 | /// given value in the current context requires a variable capture. |
| 1113 | /// |
| 1114 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1115 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1116 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1117 | ValueDecl *value) { |
| 1118 | // Only variables ever require capture. |
| 1119 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1120 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Fast path: variables from the current context never require capture. |
| 1123 | DeclContext *DC = S.CurContext; |
| 1124 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1125 | |
| 1126 | // Only variables with local storage require capture. |
| 1127 | // FIXME: What about 'const' variables in C++? |
| 1128 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1129 | |
| 1130 | // Otherwise, we need to capture. |
| 1131 | |
| 1132 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1133 | do { |
| 1134 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1135 | // scopes don't work. |
| 1136 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1137 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1138 | |
| 1139 | BlockScopeInfo *blockScope = |
| 1140 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1141 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1142 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1143 | // Check whether we've already captured it in this block. If so, |
| 1144 | // we're done. |
| 1145 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1146 | return propagateCapture(S, functionScopesIndex, |
| 1147 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1148 | |
| 1149 | functionScopesIndex--; |
| 1150 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1151 | } while (var->getDeclContext() != DC); |
| 1152 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1153 | // Okay, we descended all the way to the block that defines the variable. |
| 1154 | // Actually try to capture it. |
| 1155 | QualType type = var->getType(); |
| 1156 | |
| 1157 | // Prohibit variably-modified types. |
| 1158 | if (type->isVariablyModifiedType()) { |
| 1159 | S.Diag(loc, diag::err_ref_vm_type); |
| 1160 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1161 | return CR_Error; |
| 1162 | } |
| 1163 | |
| 1164 | // Prohibit arrays, even in __block variables, but not references to |
| 1165 | // them. |
| 1166 | if (type->isArrayType()) { |
| 1167 | S.Diag(loc, diag::err_ref_array_type); |
| 1168 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1169 | return CR_Error; |
| 1170 | } |
| 1171 | |
| 1172 | S.MarkDeclarationReferenced(loc, var); |
| 1173 | |
| 1174 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1175 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1176 | |
| 1177 | // Build a copy expression. |
| 1178 | Expr *copyExpr = 0; |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1179 | const RecordType *rtype; |
| 1180 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1181 | (rtype = type->getAs<RecordType>())) { |
| 1182 | |
| 1183 | // The capture logic needs the destructor, so make sure we mark it. |
| 1184 | // Usually this is unnecessary because most local variables have |
| 1185 | // their destructors marked at declaration time, but parameters are |
| 1186 | // an exception because it's technically only the call site that |
| 1187 | // actually requires the destructor. |
| 1188 | if (isa<ParmVarDecl>(var)) |
| 1189 | S.FinalizeVarWithDestructor(var, rtype); |
| 1190 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1191 | // According to the blocks spec, the capture of a variable from |
| 1192 | // the stack requires a const copy constructor. This is not true |
| 1193 | // of the copy/move done to move a __block variable to the heap. |
| 1194 | type.addConst(); |
| 1195 | |
| 1196 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1197 | ExprResult result = |
| 1198 | S.PerformCopyInitialization( |
| 1199 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1200 | type, false), |
| 1201 | loc, S.Owned(declRef)); |
| 1202 | |
| 1203 | // Build a full-expression copy expression if initialization |
| 1204 | // succeeded and used a non-trivial constructor. Recover from |
| 1205 | // errors by pretending that the copy isn't necessary. |
| 1206 | if (!result.isInvalid() && |
| 1207 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1208 | result = S.MaybeCreateExprWithCleanups(result); |
| 1209 | copyExpr = result.take(); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | // We're currently at the declarer; go back to the closure. |
| 1214 | functionScopesIndex++; |
| 1215 | BlockScopeInfo *blockScope = |
| 1216 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1217 | |
| 1218 | // Build a valid capture in this scope. |
| 1219 | blockScope->Captures.push_back( |
| 1220 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1221 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1222 | |
| 1223 | // Propagate that to inner captures if necessary. |
| 1224 | return propagateCapture(S, functionScopesIndex, |
| 1225 | blockScope->Captures.back()); |
| 1226 | } |
| 1227 | |
| 1228 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1229 | const DeclarationNameInfo &NameInfo, |
| 1230 | bool byRef) { |
| 1231 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1232 | |
| 1233 | VarDecl *var = cast<VarDecl>(vd); |
| 1234 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1235 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1236 | |
| 1237 | QualType exprType = var->getType().getNonReferenceType(); |
| 1238 | |
| 1239 | BlockDeclRefExpr *BDRE; |
| 1240 | if (!byRef) { |
| 1241 | // The variable will be bound by copy; make it const within the |
| 1242 | // closure, but record that this was done in the expression. |
| 1243 | bool constAdded = !exprType.isConstQualified(); |
| 1244 | exprType.addConst(); |
| 1245 | |
| 1246 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1247 | NameInfo.getLoc(), false, |
| 1248 | constAdded); |
| 1249 | } else { |
| 1250 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1251 | NameInfo.getLoc(), true); |
| 1252 | } |
| 1253 | |
| 1254 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1255 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1256 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1257 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1258 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1259 | SourceLocation Loc, |
| 1260 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1261 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1262 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1265 | /// BuildDeclRefExpr - Build an expression that references a |
| 1266 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1267 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1268 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1269 | const DeclarationNameInfo &NameInfo, |
| 1270 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1271 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1273 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1274 | SS? SS->getWithLocInContext(Context) |
| 1275 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1276 | D, NameInfo, Ty, VK); |
| 1277 | |
| 1278 | // Just in case we're building an illegal pointer-to-member. |
| 1279 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1280 | E->setObjectKind(OK_BitField); |
| 1281 | |
| 1282 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1285 | static ExprResult |
| 1286 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1287 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1288 | DeclAccessPair FoundDecl, |
| 1289 | const DeclarationNameInfo &MemberNameInfo); |
| 1290 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1291 | ExprResult |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1292 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1293 | SourceLocation loc, |
| 1294 | IndirectFieldDecl *indirectField, |
| 1295 | Expr *baseObjectExpr, |
| 1296 | SourceLocation opLoc) { |
| 1297 | // First, build the expression that refers to the base object. |
| 1298 | |
| 1299 | bool baseObjectIsPointer = false; |
| 1300 | Qualifiers baseQuals; |
| 1301 | |
| 1302 | // Case 1: the base of the indirect field is not a field. |
| 1303 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1304 | CXXScopeSpec EmptySS; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1305 | if (baseVariable) { |
| 1306 | assert(baseVariable->getType()->isRecordType()); |
| 1307 | |
| 1308 | // In principle we could have a member access expression that |
| 1309 | // accesses an anonymous struct/union that's a static member of |
| 1310 | // the base object's class. However, under the current standard, |
| 1311 | // static data members cannot be anonymous structs or unions. |
| 1312 | // Supporting this is as easy as building a MemberExpr here. |
| 1313 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1314 | |
| 1315 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1316 | |
| 1317 | ExprResult result = |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1318 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1319 | if (result.isInvalid()) return ExprError(); |
| 1320 | |
| 1321 | baseObjectExpr = result.take(); |
| 1322 | baseObjectIsPointer = false; |
| 1323 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1324 | |
| 1325 | // Case 2: the base of the indirect field is a field and the user |
| 1326 | // wrote a member expression. |
| 1327 | } else if (baseObjectExpr) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1328 | // The caller provided the base object expression. Determine |
| 1329 | // whether its a pointer and whether it adds any qualifiers to the |
| 1330 | // anonymous struct/union fields we're looking into. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1331 | QualType objectType = baseObjectExpr->getType(); |
| 1332 | |
| 1333 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1334 | baseObjectIsPointer = true; |
| 1335 | objectType = ptr->getPointeeType(); |
| 1336 | } else { |
| 1337 | baseObjectIsPointer = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1338 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1339 | baseQuals = objectType.getQualifiers(); |
| 1340 | |
| 1341 | // Case 3: the base of the indirect field is a field and we should |
| 1342 | // build an implicit member access. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1343 | } else { |
| 1344 | // We've found a member of an anonymous struct/union that is |
| 1345 | // inside a non-anonymous struct/union, so in a well-formed |
| 1346 | // program our base object expression is "this". |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1347 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 1348 | if (ThisTy.isNull()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1349 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1350 | << indirectField->getDeclName(); |
| 1351 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1354 | // Our base object expression is "this". |
| 1355 | baseObjectExpr = |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1356 | new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1357 | baseObjectIsPointer = true; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1358 | baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | // Build the implicit member references to the field of the |
| 1362 | // anonymous struct/union. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1363 | Expr *result = baseObjectExpr; |
| 1364 | IndirectFieldDecl::chain_iterator |
| 1365 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1366 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1367 | // Build the first member access in the chain with full information. |
| 1368 | if (!baseVariable) { |
| 1369 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1370 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1371 | // FIXME: use the real found-decl info! |
| 1372 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1373 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1374 | // Make a nameInfo that properly uses the anonymous name. |
| 1375 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1376 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1377 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1378 | EmptySS, field, foundDecl, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1379 | memberNameInfo).take(); |
| 1380 | baseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1381 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1382 | // FIXME: check qualified member access |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1385 | // In all cases, we should now skip the first declaration in the chain. |
| 1386 | ++FI; |
| 1387 | |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1388 | while (FI != FEnd) { |
| 1389 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1390 | |
| 1391 | // FIXME: these are somewhat meaningless |
| 1392 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1393 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1394 | |
| 1395 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1396 | (FI == FEnd? SS : EmptySS), field, |
| 1397 | foundDecl, memberNameInfo) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1398 | .take(); |
| 1399 | } |
| 1400 | |
| 1401 | return Owned(result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1404 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1405 | /// possibly a list of template arguments. |
| 1406 | /// |
| 1407 | /// If this produces template arguments, it is permitted to call |
| 1408 | /// DecomposeTemplateName. |
| 1409 | /// |
| 1410 | /// This actually loses a lot of source location information for |
| 1411 | /// non-standard name kinds; we should consider preserving that in |
| 1412 | /// some way. |
| 1413 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1414 | const UnqualifiedId &Id, |
| 1415 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1416 | DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1417 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1418 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1419 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1420 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1421 | |
| 1422 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1423 | Id.TemplateId->getTemplateArgs(), |
| 1424 | Id.TemplateId->NumArgs); |
| 1425 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1426 | TemplateArgsPtr.release(); |
| 1427 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1428 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1429 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1430 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1431 | TemplateArgs = &Buffer; |
| 1432 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1433 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1434 | TemplateArgs = 0; |
| 1435 | } |
| 1436 | } |
| 1437 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1438 | /// Determines if the given class is provably not derived from all of |
| 1439 | /// the prospective base classes. |
| 1440 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1441 | CXXRecordDecl *Record, |
| 1442 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1443 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1446 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1447 | if (!RD) return false; |
| 1448 | Record = cast<CXXRecordDecl>(RD); |
| 1449 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1450 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1451 | E = Record->bases_end(); I != E; ++I) { |
| 1452 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1453 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1454 | if (!BaseRT) return false; |
| 1455 | |
| 1456 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1457 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | return true; |
| 1462 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1463 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1464 | enum IMAKind { |
| 1465 | /// The reference is definitely not an instance member access. |
| 1466 | IMA_Static, |
| 1467 | |
| 1468 | /// The reference may be an implicit instance member access. |
| 1469 | IMA_Mixed, |
| 1470 | |
| 1471 | /// The reference may be to an instance member, but it is invalid if |
| 1472 | /// so, because the context is not an instance method. |
| 1473 | IMA_Mixed_StaticContext, |
| 1474 | |
| 1475 | /// The reference may be to an instance member, but it is invalid if |
| 1476 | /// so, because the context is from an unrelated class. |
| 1477 | IMA_Mixed_Unrelated, |
| 1478 | |
| 1479 | /// The reference is definitely an implicit instance member access. |
| 1480 | IMA_Instance, |
| 1481 | |
| 1482 | /// The reference may be to an unresolved using declaration. |
| 1483 | IMA_Unresolved, |
| 1484 | |
| 1485 | /// The reference may be to an unresolved using declaration and the |
| 1486 | /// context is not an instance method. |
| 1487 | IMA_Unresolved_StaticContext, |
| 1488 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1489 | /// All possible referrents are instance members and the current |
| 1490 | /// context is not an instance method. |
| 1491 | IMA_Error_StaticContext, |
| 1492 | |
| 1493 | /// All possible referrents are instance members of an unrelated |
| 1494 | /// class. |
| 1495 | IMA_Error_Unrelated |
| 1496 | }; |
| 1497 | |
| 1498 | /// The given lookup names class member(s) and is not being used for |
| 1499 | /// an address-of-member expression. Classify the type of access |
| 1500 | /// according to whether it's possible that this reference names an |
| 1501 | /// instance member. This is best-effort; it is okay to |
| 1502 | /// conservatively answer "yes", in which case some errors will simply |
| 1503 | /// not be caught until template-instantiation. |
| 1504 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1505 | Scope *CurScope, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1506 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1507 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1508 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1509 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1510 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1511 | bool isStaticContext = |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1512 | (!isa<CXXMethodDecl>(DC) || |
| 1513 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1514 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1515 | // C++0x [expr.prim]p4: |
| 1516 | // Otherwise, if a member-declarator declares a non-static data member |
| 1517 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 1518 | // within the optional brace-or-equal-initializer. |
| 1519 | if (CurScope->getFlags() & Scope::ThisScope) |
| 1520 | isStaticContext = false; |
| 1521 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1522 | if (R.isUnresolvableResult()) |
| 1523 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1524 | |
| 1525 | // Collect all the declaring classes of instance members we find. |
| 1526 | bool hasNonInstance = false; |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1527 | bool hasField = false; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1528 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1529 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1530 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1531 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1532 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1533 | if (dyn_cast<FieldDecl>(D)) |
| 1534 | hasField = true; |
| 1535 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1536 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1537 | Classes.insert(R->getCanonicalDecl()); |
| 1538 | } |
| 1539 | else |
| 1540 | hasNonInstance = true; |
| 1541 | } |
| 1542 | |
| 1543 | // If we didn't find any instance members, it can't be an implicit |
| 1544 | // member reference. |
| 1545 | if (Classes.empty()) |
| 1546 | return IMA_Static; |
| 1547 | |
| 1548 | // If the current context is not an instance method, it can't be |
| 1549 | // an implicit member reference. |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1550 | if (isStaticContext) { |
| 1551 | if (hasNonInstance) |
| 1552 | return IMA_Mixed_StaticContext; |
| 1553 | |
| 1554 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1555 | // C++0x [expr.prim.general]p10: |
| 1556 | // An id-expression that denotes a non-static data member or non-static |
| 1557 | // member function of a class can only be used: |
| 1558 | // (...) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1559 | // - if that id-expression denotes a non-static data member and it |
| 1560 | // appears in an unevaluated operand. |
| 1561 | const Sema::ExpressionEvaluationContextRecord& record |
| 1562 | = SemaRef.ExprEvalContexts.back(); |
| 1563 | bool isUnevaluatedExpression = (record.Context == Sema::Unevaluated); |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1564 | if (isUnevaluatedExpression) |
| 1565 | return IMA_Mixed_StaticContext; |
| 1566 | } |
| 1567 | |
| 1568 | return IMA_Error_StaticContext; |
| 1569 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1570 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1571 | CXXRecordDecl *contextClass; |
| 1572 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
| 1573 | contextClass = MD->getParent()->getCanonicalDecl(); |
| 1574 | else |
| 1575 | contextClass = cast<CXXRecordDecl>(DC); |
Argyrios Kyrtzidis | 0d8dc46 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1576 | |
| 1577 | // [class.mfct.non-static]p3: |
| 1578 | // ...is used in the body of a non-static member function of class X, |
| 1579 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 1580 | // non-static non-type member of some class C [...] |
| 1581 | // ...if C is not X or a base class of X, the class member access expression |
| 1582 | // is ill-formed. |
| 1583 | if (R.getNamingClass() && |
| 1584 | contextClass != R.getNamingClass()->getCanonicalDecl() && |
| 1585 | contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) |
| 1586 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1587 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1588 | // If we can prove that the current context is unrelated to all the |
| 1589 | // declaring classes, it can't be an implicit member reference (in |
| 1590 | // which case it's an error if any of those members are selected). |
Argyrios Kyrtzidis | 0d8dc46 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1591 | if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1592 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1593 | |
| 1594 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1595 | } |
| 1596 | |
| 1597 | /// Diagnose a reference to a field with no object available. |
| 1598 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1599 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1600 | NamedDecl *rep, |
| 1601 | const DeclarationNameInfo &nameInfo) { |
| 1602 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1603 | SourceRange Range(Loc); |
| 1604 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1605 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1606 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1607 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1608 | if (MD->isStatic()) { |
| 1609 | // "invalid use of member 'x' in static member function" |
| 1610 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1611 | << Range << nameInfo.getName(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1617 | << nameInfo.getName() << Range; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1624 | /// Diagnose an empty lookup. |
| 1625 | /// |
| 1626 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1627 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1628 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1629 | DeclarationName Name = R.getLookupName(); |
| 1630 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1631 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1632 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1633 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1634 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1635 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1636 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1637 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1638 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1640 | // If the original lookup was an unqualified lookup, fake an |
| 1641 | // unqualified lookup. This is useful when (for example) the |
| 1642 | // original lookup would not have found something because it was a |
| 1643 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1644 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1645 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1646 | if (isa<CXXRecordDecl>(DC)) { |
| 1647 | LookupQualifiedName(R, DC); |
| 1648 | |
| 1649 | if (!R.empty()) { |
| 1650 | // Don't give errors about ambiguities in this lookup. |
| 1651 | R.suppressDiagnostics(); |
| 1652 | |
| 1653 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1654 | bool isInstance = CurMethod && |
| 1655 | CurMethod->isInstance() && |
| 1656 | DC == CurMethod->getParent(); |
| 1657 | |
| 1658 | // Give a code modification hint to insert 'this->'. |
| 1659 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1660 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1661 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1662 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1663 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1664 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1665 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1666 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1667 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1668 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1669 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1670 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1671 | R.getNameLoc(), DepThisType, false); |
| 1672 | TemplateArgumentListInfo TList; |
| 1673 | if (ULE->hasExplicitTemplateArgs()) |
| 1674 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1676 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1677 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1678 | CXXDependentScopeMemberExpr *DepExpr = |
| 1679 | CXXDependentScopeMemberExpr::Create( |
| 1680 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1681 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1682 | R.getLookupNameInfo(), &TList); |
| 1683 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1684 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1685 | // FIXME: we should be able to handle this case too. It is correct |
| 1686 | // to add this-> here. This is a workaround for PR7947. |
| 1687 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1688 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1689 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1690 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1691 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Do we really want to note all of these? |
| 1694 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1695 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1696 | |
| 1697 | // Tell the callee to try to recover. |
| 1698 | return false; |
| 1699 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1700 | |
| 1701 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1705 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1706 | DeclarationName Corrected; |
Daniel Dunbar | dc32cdf | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1707 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1708 | if (!R.empty()) { |
| 1709 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1710 | if (SS.isEmpty()) |
| 1711 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1712 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1713 | R.getLookupName().getAsString()); |
| 1714 | else |
| 1715 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1716 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1717 | << SS.getRange() |
| 1718 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1719 | R.getLookupName().getAsString()); |
| 1720 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1721 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1722 | << ND->getDeclName(); |
| 1723 | |
| 1724 | // Tell the callee to try to recover. |
| 1725 | return false; |
| 1726 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1727 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1728 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1729 | // FIXME: If we ended up with a typo for a type name or |
| 1730 | // Objective-C class name, we're in trouble because the parser |
| 1731 | // is in the wrong place to recover. Suggest the typo |
| 1732 | // correction, but don't make it a fix-it since we're not going |
| 1733 | // to recover well anyway. |
| 1734 | if (SS.isEmpty()) |
| 1735 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1736 | else |
| 1737 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1738 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1739 | << SS.getRange(); |
| 1740 | |
| 1741 | // Don't try to recover; it won't work. |
| 1742 | return true; |
| 1743 | } |
| 1744 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1745 | // 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] | 1746 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1747 | if (SS.isEmpty()) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1748 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1749 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1750 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1751 | << Name << computeDeclContext(SS, false) << Corrected |
| 1752 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1753 | return true; |
| 1754 | } |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1755 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | // Emit a special diagnostic for failed member lookups. |
| 1759 | // FIXME: computing the declaration context might fail here (?) |
| 1760 | if (!SS.isEmpty()) { |
| 1761 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1762 | << Name << computeDeclContext(SS, false) |
| 1763 | << SS.getRange(); |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1767 | // Give up, we can't recover. |
| 1768 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1772 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1773 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1774 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1775 | if (!IDecl) |
| 1776 | return 0; |
| 1777 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1778 | if (!ClassImpDecl) |
| 1779 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1780 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1781 | if (!property) |
| 1782 | return 0; |
| 1783 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1784 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1785 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1786 | return 0; |
| 1787 | return property; |
| 1788 | } |
| 1789 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1790 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1791 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1792 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1793 | if (!IDecl) |
| 1794 | return false; |
| 1795 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1796 | if (!ClassImpDecl) |
| 1797 | return false; |
| 1798 | if (ObjCPropertyImplDecl *PIDecl |
| 1799 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1800 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1801 | PIDecl->getPropertyIvarDecl()) |
| 1802 | return false; |
| 1803 | |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1807 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1808 | IdentifierInfo *II, |
| 1809 | SourceLocation NameLoc) { |
| 1810 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1811 | bool LookForIvars; |
| 1812 | if (Lookup.empty()) |
| 1813 | LookForIvars = true; |
| 1814 | else if (CurMeth->isClassMethod()) |
| 1815 | LookForIvars = false; |
| 1816 | else |
| 1817 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1818 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1819 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1820 | if (!LookForIvars) |
| 1821 | return 0; |
| 1822 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1823 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1824 | if (!IDecl) |
| 1825 | return 0; |
| 1826 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1827 | if (!ClassImpDecl) |
| 1828 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1829 | bool DynamicImplSeen = false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1830 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1831 | if (!property) |
| 1832 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1833 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1834 | DynamicImplSeen = |
| 1835 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1836 | // property implementation has a designated ivar. No need to assume a new |
| 1837 | // one. |
| 1838 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1839 | return 0; |
| 1840 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1841 | if (!DynamicImplSeen) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1842 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1843 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1844 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1845 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1846 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1847 | (Expr *)0, true); |
| 1848 | ClassImpDecl->addDecl(Ivar); |
| 1849 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1850 | property->setPropertyIvarDecl(Ivar); |
| 1851 | return Ivar; |
| 1852 | } |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1856 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1857 | CXXScopeSpec &SS, |
| 1858 | UnqualifiedId &Id, |
| 1859 | bool HasTrailingLParen, |
| 1860 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1861 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1862 | "cannot be direct & operand and have a trailing lparen"); |
| 1863 | |
| 1864 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1865 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1866 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1867 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1870 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1871 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1872 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1873 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1874 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1875 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1876 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1877 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1878 | // C++ [temp.dep.expr]p3: |
| 1879 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1880 | // -- an identifier that was declared with a dependent type, |
| 1881 | // (note: handled after lookup) |
| 1882 | // -- a template-id that is dependent, |
| 1883 | // (note: handled in BuildTemplateIdExpr) |
| 1884 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1885 | // -- a nested-name-specifier that contains a class-name that |
| 1886 | // names a dependent type. |
| 1887 | // Determine whether this is a member of an unknown specialization; |
| 1888 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1889 | bool DependentID = false; |
| 1890 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1891 | Name.getCXXNameType()->isDependentType()) { |
| 1892 | DependentID = true; |
| 1893 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1894 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1895 | if (RequireCompleteDeclContext(SS, DC)) |
| 1896 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1897 | } else { |
| 1898 | DependentID = true; |
| 1899 | } |
| 1900 | } |
| 1901 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1902 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1903 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1904 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1905 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1906 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1907 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1908 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1909 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1910 | // Lookup the template name again to correctly establish the context in |
| 1911 | // which it was found. This is really unfortunate as we already did the |
| 1912 | // lookup to determine that it was a template name in the first place. If |
| 1913 | // this becomes a performance hit, we can work harder to preserve those |
| 1914 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1915 | bool MemberOfUnknownSpecialization; |
| 1916 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1917 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1918 | |
| 1919 | if (MemberOfUnknownSpecialization || |
| 1920 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1921 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1922 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1923 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1924 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1925 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1927 | // If the result might be in a dependent base class, this is a dependent |
| 1928 | // id-expression. |
| 1929 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1930 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1931 | TemplateArgs); |
| 1932 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1933 | // If this reference is in an Objective-C method, then we need to do |
| 1934 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1935 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1936 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1937 | if (E.isInvalid()) |
| 1938 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1940 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1941 | return Owned(Ex); |
| 1942 | |
| 1943 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1944 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1945 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1946 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1947 | if (const ObjCPropertyDecl *Property = |
| 1948 | canSynthesizeProvisionalIvar(II)) { |
| 1949 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1950 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1951 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1952 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1953 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1954 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1955 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1956 | // for further use, this must be set to false if in class method. |
| 1957 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1958 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1959 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1960 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1961 | if (R.isAmbiguous()) |
| 1962 | return ExprError(); |
| 1963 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1964 | // Determine whether this name might be a candidate for |
| 1965 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1966 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1967 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1968 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1969 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1970 | // in C90, extension in C99, forbidden in C++). |
| 1971 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1972 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1973 | if (D) R.addDecl(D); |
| 1974 | } |
| 1975 | |
| 1976 | // If this name wasn't predeclared and if this is not a function |
| 1977 | // call, diagnose the problem. |
| 1978 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1979 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1980 | return ExprError(); |
| 1981 | |
| 1982 | assert(!R.empty() && |
| 1983 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1984 | |
| 1985 | // If we found an Objective-C instance variable, let |
| 1986 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1987 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1988 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1989 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1990 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1991 | assert(E.isInvalid() || E.get()); |
| 1992 | return move(E); |
| 1993 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1994 | } |
| 1995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1997 | // This is guaranteed from this point on. |
| 1998 | assert(!R.empty() || ADL); |
| 1999 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2000 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 2001 | // C++ [class.mfct.non-static]p3: |
| 2002 | // When an id-expression that is not part of a class member access |
| 2003 | // syntax and not used to form a pointer to member is used in the |
| 2004 | // body of a non-static member function of class X, if name lookup |
| 2005 | // resolves the name in the id-expression to a non-static non-type |
| 2006 | // member of some class C, the id-expression is transformed into a |
| 2007 | // class member access expression using (*this) as the |
| 2008 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2009 | // |
| 2010 | // But we don't actually need to do this for '&' operands if R |
| 2011 | // resolved to a function or overloaded function set, because the |
| 2012 | // expression is ill-formed if it actually works out to be a |
| 2013 | // non-static member function: |
| 2014 | // |
| 2015 | // C++ [expr.ref]p4: |
| 2016 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 2017 | // [t]he expression can be used only as the left-hand operand of a |
| 2018 | // member function call. |
| 2019 | // |
| 2020 | // There are other safeguards against such uses, but it's important |
| 2021 | // to get this right here so that we don't end up making a |
| 2022 | // spuriously dependent expression if we're inside a dependent |
| 2023 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2024 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2025 | bool MightBeImplicitMember; |
| 2026 | if (!isAddressOfOperand) |
| 2027 | MightBeImplicitMember = true; |
| 2028 | else if (!SS.isEmpty()) |
| 2029 | MightBeImplicitMember = false; |
| 2030 | else if (R.isOverloadedResult()) |
| 2031 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 2032 | else if (R.isUnresolvableResult()) |
| 2033 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2034 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2035 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 2036 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2037 | |
| 2038 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2039 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2042 | if (TemplateArgs) |
| 2043 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2044 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2045 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 2046 | } |
| 2047 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2048 | /// Builds an expression which might be an implicit member expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2049 | ExprResult |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2050 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2051 | LookupResult &R, |
| 2052 | const TemplateArgumentListInfo *TemplateArgs) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2053 | switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2054 | case IMA_Instance: |
| 2055 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 2056 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2057 | case IMA_Mixed: |
| 2058 | case IMA_Mixed_Unrelated: |
| 2059 | case IMA_Unresolved: |
| 2060 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 2061 | |
| 2062 | case IMA_Static: |
| 2063 | case IMA_Mixed_StaticContext: |
| 2064 | case IMA_Unresolved_StaticContext: |
| 2065 | if (TemplateArgs) |
| 2066 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 2067 | return BuildDeclarationNameExpr(SS, R, false); |
| 2068 | |
| 2069 | case IMA_Error_StaticContext: |
| 2070 | case IMA_Error_Unrelated: |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2071 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 2072 | R.getLookupNameInfo()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2073 | return ExprError(); |
| 2074 | } |
| 2075 | |
| 2076 | llvm_unreachable("unexpected instance member access kind"); |
| 2077 | return ExprError(); |
| 2078 | } |
| 2079 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2080 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 2081 | /// declaration name, generally during template instantiation. |
| 2082 | /// There's a large number of things which don't need to be done along |
| 2083 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2084 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2085 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2086 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2087 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2088 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2089 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2090 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2091 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2092 | return ExprError(); |
| 2093 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2094 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2095 | LookupQualifiedName(R, DC); |
| 2096 | |
| 2097 | if (R.isAmbiguous()) |
| 2098 | return ExprError(); |
| 2099 | |
| 2100 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2101 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 2102 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2103 | return ExprError(); |
| 2104 | } |
| 2105 | |
| 2106 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 2107 | } |
| 2108 | |
| 2109 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 2110 | /// detected that we're currently inside an ObjC method. Perform some |
| 2111 | /// additional lookup. |
| 2112 | /// |
| 2113 | /// Ideally, most of this would be done by lookup, but there's |
| 2114 | /// actually quite a lot of extra work involved. |
| 2115 | /// |
| 2116 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2117 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2118 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2119 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2120 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2121 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2122 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2123 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 2124 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 2125 | // found a decl, but that decl is outside the current instance method (i.e. |
| 2126 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 2127 | // this name, if the lookup sucedes, we replace it our current decl. |
| 2128 | |
| 2129 | // If we're in a class method, we don't normally want to look for |
| 2130 | // ivars. But if we don't find anything else, and there's an |
| 2131 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2132 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2133 | |
| 2134 | bool LookForIvars; |
| 2135 | if (Lookup.empty()) |
| 2136 | LookForIvars = true; |
| 2137 | else if (IsClassMethod) |
| 2138 | LookForIvars = false; |
| 2139 | else |
| 2140 | LookForIvars = (Lookup.isSingleResult() && |
| 2141 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2142 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2143 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2144 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2145 | ObjCInterfaceDecl *ClassDeclared; |
| 2146 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2147 | // Diagnose using an ivar in a class method. |
| 2148 | if (IsClassMethod) |
| 2149 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2150 | << IV->getDeclName()); |
| 2151 | |
| 2152 | // If we're referencing an invalid decl, just return this as a silent |
| 2153 | // error node. The error diagnostic was already emitted on the decl. |
| 2154 | if (IV->isInvalidDecl()) |
| 2155 | return ExprError(); |
| 2156 | |
| 2157 | // Check if referencing a field with __attribute__((deprecated)). |
| 2158 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2159 | return ExprError(); |
| 2160 | |
| 2161 | // Diagnose the use of an ivar outside of the declaring class. |
| 2162 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 2163 | ClassDeclared != IFace) |
| 2164 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2165 | |
| 2166 | // FIXME: This should use a new expr for a direct reference, don't |
| 2167 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2168 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2169 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2170 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2171 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2172 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2173 | SelfName, false, false); |
| 2174 | if (SelfExpr.isInvalid()) |
| 2175 | return ExprError(); |
| 2176 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2177 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2178 | if (SelfExpr.isInvalid()) |
| 2179 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2180 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2181 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2182 | Expr *base = SelfExpr.take(); |
| 2183 | base = base->IgnoreParenImpCasts(); |
| 2184 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 2185 | const NamedDecl *ND = DE->getDecl(); |
| 2186 | if (!isa<ImplicitParamDecl>(ND)) { |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2187 | // relax the rule such that it is allowed to have a shadow 'self' |
| 2188 | // where stand-alone ivar can be found in this 'self' object. |
| 2189 | // This is to match gcc's behavior. |
| 2190 | ObjCInterfaceDecl *selfIFace = 0; |
| 2191 | if (const ObjCObjectPointerType *OPT = |
| 2192 | base->getType()->getAsObjCInterfacePointerType()) |
| 2193 | selfIFace = OPT->getInterfaceDecl(); |
| 2194 | if (!selfIFace || |
| 2195 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2196 | Diag(Loc, diag::error_implicit_ivar_access) |
| 2197 | << IV->getDeclName(); |
| 2198 | Diag(ND->getLocation(), diag::note_declared_at); |
| 2199 | return ExprError(); |
| 2200 | } |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2201 | } |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2202 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2203 | return Owned(new (Context) |
| 2204 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2205 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2206 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2207 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2208 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2209 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2210 | ObjCInterfaceDecl *ClassDeclared; |
| 2211 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2212 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 2213 | IFace == ClassDeclared) |
| 2214 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2215 | } |
| 2216 | } |
| 2217 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2218 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2219 | // FIXME. Consolidate this with similar code in LookupName. |
| 2220 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 2221 | if (!(getLangOptions().CPlusPlus && |
| 2222 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2223 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2224 | S, Lookup.isForRedeclaration(), |
| 2225 | Lookup.getNameLoc()); |
| 2226 | if (D) Lookup.addDecl(D); |
| 2227 | } |
| 2228 | } |
| 2229 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2230 | // Sentinel value saying that we didn't do anything special. |
| 2231 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2232 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2233 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2234 | /// \brief Cast a base object to a member's actual type. |
| 2235 | /// |
| 2236 | /// Logically this happens in three phases: |
| 2237 | /// |
| 2238 | /// * First we cast from the base type to the naming class. |
| 2239 | /// The naming class is the class into which we were looking |
| 2240 | /// when we found the member; it's the qualifier type if a |
| 2241 | /// qualifier was provided, and otherwise it's the base type. |
| 2242 | /// |
| 2243 | /// * Next we cast from the naming class to the declaring class. |
| 2244 | /// If the member we found was brought into a class's scope by |
| 2245 | /// a using declaration, this is that class; otherwise it's |
| 2246 | /// the class declaring the member. |
| 2247 | /// |
| 2248 | /// * Finally we cast from the declaring class to the "true" |
| 2249 | /// declaring class of the member. This conversion does not |
| 2250 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2251 | ExprResult |
| 2252 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2253 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2254 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2255 | NamedDecl *Member) { |
| 2256 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2257 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2258 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2259 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2260 | QualType DestRecordType; |
| 2261 | QualType DestType; |
| 2262 | QualType FromRecordType; |
| 2263 | QualType FromType = From->getType(); |
| 2264 | bool PointerConversions = false; |
| 2265 | if (isa<FieldDecl>(Member)) { |
| 2266 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2268 | if (FromType->getAs<PointerType>()) { |
| 2269 | DestType = Context.getPointerType(DestRecordType); |
| 2270 | FromRecordType = FromType->getPointeeType(); |
| 2271 | PointerConversions = true; |
| 2272 | } else { |
| 2273 | DestType = DestRecordType; |
| 2274 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2275 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2276 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2277 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2278 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2280 | DestType = Method->getThisType(Context); |
| 2281 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2283 | if (FromType->getAs<PointerType>()) { |
| 2284 | FromRecordType = FromType->getPointeeType(); |
| 2285 | PointerConversions = true; |
| 2286 | } else { |
| 2287 | FromRecordType = FromType; |
| 2288 | DestType = DestRecordType; |
| 2289 | } |
| 2290 | } else { |
| 2291 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2292 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2293 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2295 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2296 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2297 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2298 | // If the unqualified types are the same, no conversion is necessary. |
| 2299 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2300 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2301 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2302 | SourceRange FromRange = From->getSourceRange(); |
| 2303 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2304 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2305 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2307 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2308 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2309 | // class name. |
| 2310 | // |
| 2311 | // If the member was a qualified name and the qualified referred to a |
| 2312 | // specific base subobject type, we'll cast to that intermediate type |
| 2313 | // first and then to the object in which the member is declared. That allows |
| 2314 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2315 | // |
| 2316 | // class Base { public: int x; }; |
| 2317 | // class Derived1 : public Base { }; |
| 2318 | // class Derived2 : public Base { }; |
| 2319 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2320 | // |
| 2321 | // void VeryDerived::f() { |
| 2322 | // x = 17; // error: ambiguous base subobjects |
| 2323 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2324 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2325 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2326 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2327 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2328 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2329 | |
| 2330 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2331 | |
| 2332 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2333 | // type of the object type, in which case we just ignore it. |
| 2334 | // Otherwise build the appropriate casts. |
| 2335 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2336 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2337 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2338 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2339 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2341 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2342 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2343 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2344 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2345 | |
| 2346 | FromType = QType; |
| 2347 | FromRecordType = QRecordType; |
| 2348 | |
| 2349 | // If the qualifier type was the same as the destination type, |
| 2350 | // we're done. |
| 2351 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2352 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2355 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2356 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2357 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2358 | // If we actually found the member through a using declaration, cast |
| 2359 | // down to the using declaration's type. |
| 2360 | // |
| 2361 | // Pointer equality is fine here because only one declaration of a |
| 2362 | // class ever has member declarations. |
| 2363 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2364 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2365 | QualType URecordType = Context.getTypeDeclType( |
| 2366 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2367 | |
| 2368 | // We only need to do this if the naming-class to declaring-class |
| 2369 | // conversion is non-trivial. |
| 2370 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2371 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2372 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2373 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2374 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2375 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2376 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2377 | QualType UType = URecordType; |
| 2378 | if (PointerConversions) |
| 2379 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2380 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2381 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2382 | FromType = UType; |
| 2383 | FromRecordType = URecordType; |
| 2384 | } |
| 2385 | |
| 2386 | // We don't do access control for the conversion from the |
| 2387 | // declaring class to the true declaring class. |
| 2388 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2389 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2390 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2391 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2392 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2393 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2394 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2395 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2396 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2397 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2398 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2399 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2401 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2403 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2404 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2405 | const DeclarationNameInfo &MemberNameInfo, |
| 2406 | QualType Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2407 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2408 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2409 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2410 | Member, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2411 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2414 | static ExprResult |
| 2415 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2416 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2417 | DeclAccessPair FoundDecl, |
| 2418 | const DeclarationNameInfo &MemberNameInfo) { |
| 2419 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2420 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2421 | // that *x is always an l-value), except that if the base isn't |
| 2422 | // an ordinary object then we must have an rvalue. |
| 2423 | ExprValueKind VK = VK_LValue; |
| 2424 | ExprObjectKind OK = OK_Ordinary; |
| 2425 | if (!IsArrow) { |
| 2426 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2427 | VK = BaseExpr->getValueKind(); |
| 2428 | else |
| 2429 | VK = VK_RValue; |
| 2430 | } |
| 2431 | if (VK != VK_RValue && Field->isBitField()) |
| 2432 | OK = OK_BitField; |
| 2433 | |
| 2434 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2435 | QualType MemberType = Field->getType(); |
| 2436 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2437 | MemberType = Ref->getPointeeType(); |
| 2438 | VK = VK_LValue; |
| 2439 | } else { |
| 2440 | QualType BaseType = BaseExpr->getType(); |
| 2441 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2442 | |
| 2443 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2444 | |
| 2445 | // GC attributes are never picked up by members. |
| 2446 | BaseQuals.removeObjCGCAttr(); |
| 2447 | |
| 2448 | // CVR attributes from the base are picked up by members, |
| 2449 | // except that 'mutable' members don't pick up 'const'. |
| 2450 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2451 | |
| 2452 | Qualifiers MemberQuals |
| 2453 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2454 | |
| 2455 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2456 | assert(!MemberQuals.hasAddressSpace()); |
| 2457 | |
| 2458 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2459 | if (Combined != MemberQuals) |
| 2460 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2461 | } |
| 2462 | |
| 2463 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2464 | ExprResult Base = |
| 2465 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2466 | FoundDecl, Field); |
| 2467 | if (Base.isInvalid()) |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2468 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2469 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2470 | Field, FoundDecl, MemberNameInfo, |
| 2471 | MemberType, VK, OK)); |
| 2472 | } |
| 2473 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2474 | /// Builds an implicit member access expression. The current context |
| 2475 | /// is known to be an instance method, and the given unqualified lookup |
| 2476 | /// set is known to contain only instance members, at least one of which |
| 2477 | /// is from an appropriate type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2478 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2479 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2480 | LookupResult &R, |
| 2481 | const TemplateArgumentListInfo *TemplateArgs, |
| 2482 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2483 | assert(!R.empty() && !R.isAmbiguous()); |
| 2484 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2485 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2487 | // We may have found a field within an anonymous union or struct |
| 2488 | // (C++ [class.union]). |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2489 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2490 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2491 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2492 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2493 | // If this is known to be an instance access, go ahead and build an |
| 2494 | // implicit 'this' expression now. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2495 | // 'this' expression now. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2496 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 2497 | assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2498 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2499 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2500 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2501 | SourceLocation Loc = R.getNameLoc(); |
| 2502 | if (SS.getRange().isValid()) |
| 2503 | Loc = SS.getRange().getBegin(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2504 | baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2507 | return BuildMemberReferenceExpr(baseExpr, ThisTy, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2508 | /*OpLoc*/ SourceLocation(), |
| 2509 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2510 | SS, |
| 2511 | /*FirstQualifierInScope*/ 0, |
| 2512 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2515 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2516 | const LookupResult &R, |
| 2517 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2518 | // Only when used directly as the postfix-expression of a call. |
| 2519 | if (!HasTrailingLParen) |
| 2520 | return false; |
| 2521 | |
| 2522 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2523 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2524 | return false; |
| 2525 | |
| 2526 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2527 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2528 | return false; |
| 2529 | |
| 2530 | // Turn off ADL when we find certain kinds of declarations during |
| 2531 | // normal lookup: |
| 2532 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2533 | NamedDecl *D = *I; |
| 2534 | |
| 2535 | // C++0x [basic.lookup.argdep]p3: |
| 2536 | // -- a declaration of a class member |
| 2537 | // Since using decls preserve this property, we check this on the |
| 2538 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2539 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2540 | return false; |
| 2541 | |
| 2542 | // C++0x [basic.lookup.argdep]p3: |
| 2543 | // -- a block-scope function declaration that is not a |
| 2544 | // using-declaration |
| 2545 | // NOTE: we also trigger this for function templates (in fact, we |
| 2546 | // don't check the decl type at all, since all other decl types |
| 2547 | // turn off ADL anyway). |
| 2548 | if (isa<UsingShadowDecl>(D)) |
| 2549 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2550 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2551 | return false; |
| 2552 | |
| 2553 | // C++0x [basic.lookup.argdep]p3: |
| 2554 | // -- a declaration that is neither a function or a function |
| 2555 | // template |
| 2556 | // And also for builtin functions. |
| 2557 | if (isa<FunctionDecl>(D)) { |
| 2558 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2559 | |
| 2560 | // But also builtin functions. |
| 2561 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2562 | return false; |
| 2563 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2564 | return false; |
| 2565 | } |
| 2566 | |
| 2567 | return true; |
| 2568 | } |
| 2569 | |
| 2570 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2571 | /// Diagnoses obvious problems with the use of the given declaration |
| 2572 | /// as an expression. This is only actually called for lookups that |
| 2573 | /// were not overloaded, and it doesn't promise that the declaration |
| 2574 | /// will in fact be used. |
| 2575 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2576 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2577 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2578 | return true; |
| 2579 | } |
| 2580 | |
| 2581 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2582 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2583 | return true; |
| 2584 | } |
| 2585 | |
| 2586 | if (isa<NamespaceDecl>(D)) { |
| 2587 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2588 | return true; |
| 2589 | } |
| 2590 | |
| 2591 | return false; |
| 2592 | } |
| 2593 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2594 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2595 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2596 | LookupResult &R, |
| 2597 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2598 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2599 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2600 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2601 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2602 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2603 | |
| 2604 | // We only need to check the declaration if there's exactly one |
| 2605 | // result, because in the overloaded case the results can only be |
| 2606 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2607 | if (R.isSingleResult() && |
| 2608 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2609 | return ExprError(); |
| 2610 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2611 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2612 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2613 | // we've picked a target. |
| 2614 | R.suppressDiagnostics(); |
| 2615 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2616 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2617 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2618 | SS.getWithLocInContext(Context), |
| 2619 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2620 | NeedsADL, R.isOverloadedResult(), |
| 2621 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2622 | |
| 2623 | return Owned(ULE); |
| 2624 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2625 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2626 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2627 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2628 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2629 | const DeclarationNameInfo &NameInfo, |
| 2630 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2631 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2632 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2633 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2634 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2635 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2636 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2637 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2639 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2640 | // Specifically diagnose references to class templates that are missing |
| 2641 | // a template argument list. |
| 2642 | Diag(Loc, diag::err_template_decl_ref) |
| 2643 | << Template << SS.getRange(); |
| 2644 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2645 | return ExprError(); |
| 2646 | } |
| 2647 | |
| 2648 | // Make sure that we're referring to a value. |
| 2649 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2650 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2651 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2652 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2653 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2654 | return ExprError(); |
| 2655 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2656 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2657 | // Check whether this declaration can be used. Note that we suppress |
| 2658 | // this check when we're going to perform argument-dependent lookup |
| 2659 | // on this function name, because this might not be the function |
| 2660 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2661 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2662 | return ExprError(); |
| 2663 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2664 | // Only create DeclRefExpr's for valid Decl's. |
| 2665 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2666 | return ExprError(); |
| 2667 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2668 | // Handle members of anonymous structs and unions. If we got here, |
| 2669 | // and the reference is to a class member indirect field, then this |
| 2670 | // must be the subject of a pointer-to-member expression. |
| 2671 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2672 | if (!indirectField->isCXXClassMember()) |
| 2673 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2674 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2675 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2676 | // If the identifier reference is inside a block, and it refers to a value |
| 2677 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2678 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2679 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2680 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2681 | // We do not do this for things like enum constants, global variables, etc, |
| 2682 | // as they do not get snapshotted. |
| 2683 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2684 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2685 | case CR_Error: |
| 2686 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2687 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2688 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2689 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2690 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2691 | |
| 2692 | case CR_CaptureByRef: |
| 2693 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2694 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2695 | |
| 2696 | case CR_NoCapture: { |
| 2697 | // If this reference is not in a block or if the referenced |
| 2698 | // variable is within the block, create a normal DeclRefExpr. |
| 2699 | |
| 2700 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2701 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2702 | |
| 2703 | switch (D->getKind()) { |
| 2704 | // Ignore all the non-ValueDecl kinds. |
| 2705 | #define ABSTRACT_DECL(kind) |
| 2706 | #define VALUE(type, base) |
| 2707 | #define DECL(type, base) \ |
| 2708 | case Decl::type: |
| 2709 | #include "clang/AST/DeclNodes.inc" |
| 2710 | llvm_unreachable("invalid value decl kind"); |
| 2711 | return ExprError(); |
| 2712 | |
| 2713 | // These shouldn't make it here. |
| 2714 | case Decl::ObjCAtDefsField: |
| 2715 | case Decl::ObjCIvar: |
| 2716 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2717 | return ExprError(); |
| 2718 | |
| 2719 | // Enum constants are always r-values and never references. |
| 2720 | // Unresolved using declarations are dependent. |
| 2721 | case Decl::EnumConstant: |
| 2722 | case Decl::UnresolvedUsingValue: |
| 2723 | valueKind = VK_RValue; |
| 2724 | break; |
| 2725 | |
| 2726 | // Fields and indirect fields that got here must be for |
| 2727 | // pointer-to-member expressions; we just call them l-values for |
| 2728 | // internal consistency, because this subexpression doesn't really |
| 2729 | // exist in the high-level semantics. |
| 2730 | case Decl::Field: |
| 2731 | case Decl::IndirectField: |
| 2732 | assert(getLangOptions().CPlusPlus && |
| 2733 | "building reference to field in C?"); |
| 2734 | |
| 2735 | // These can't have reference type in well-formed programs, but |
| 2736 | // for internal consistency we do this anyway. |
| 2737 | type = type.getNonReferenceType(); |
| 2738 | valueKind = VK_LValue; |
| 2739 | break; |
| 2740 | |
| 2741 | // Non-type template parameters are either l-values or r-values |
| 2742 | // depending on the type. |
| 2743 | case Decl::NonTypeTemplateParm: { |
| 2744 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2745 | type = reftype->getPointeeType(); |
| 2746 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2747 | break; |
| 2748 | } |
| 2749 | |
| 2750 | // For non-references, we need to strip qualifiers just in case |
| 2751 | // the template parameter was declared as 'const int' or whatever. |
| 2752 | valueKind = VK_RValue; |
| 2753 | type = type.getUnqualifiedType(); |
| 2754 | break; |
| 2755 | } |
| 2756 | |
| 2757 | case Decl::Var: |
| 2758 | // In C, "extern void blah;" is valid and is an r-value. |
| 2759 | if (!getLangOptions().CPlusPlus && |
| 2760 | !type.hasQualifiers() && |
| 2761 | type->isVoidType()) { |
| 2762 | valueKind = VK_RValue; |
| 2763 | break; |
| 2764 | } |
| 2765 | // fallthrough |
| 2766 | |
| 2767 | case Decl::ImplicitParam: |
| 2768 | case Decl::ParmVar: |
| 2769 | // These are always l-values. |
| 2770 | valueKind = VK_LValue; |
| 2771 | type = type.getNonReferenceType(); |
| 2772 | break; |
| 2773 | |
| 2774 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2775 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2776 | |
| 2777 | // If we're referring to a function with an __unknown_anytype |
| 2778 | // result type, make the entire expression __unknown_anytype. |
| 2779 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2780 | type = Context.UnknownAnyTy; |
| 2781 | valueKind = VK_RValue; |
| 2782 | break; |
| 2783 | } |
| 2784 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2785 | // Functions are l-values in C++. |
| 2786 | if (getLangOptions().CPlusPlus) { |
| 2787 | valueKind = VK_LValue; |
| 2788 | break; |
| 2789 | } |
| 2790 | |
| 2791 | // C99 DR 316 says that, if a function type comes from a |
| 2792 | // function definition (without a prototype), that type is only |
| 2793 | // used for checking compatibility. Therefore, when referencing |
| 2794 | // the function, we pretend that we don't have the full function |
| 2795 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2796 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2797 | isa<FunctionProtoType>(fty)) |
| 2798 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2799 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2800 | |
| 2801 | // Functions are r-values in C. |
| 2802 | valueKind = VK_RValue; |
| 2803 | break; |
| 2804 | } |
| 2805 | |
| 2806 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2807 | // If we're referring to a method with an __unknown_anytype |
| 2808 | // result type, make the entire expression __unknown_anytype. |
| 2809 | // This should only be possible with a type written directly. |
| 2810 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2811 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2812 | type = Context.UnknownAnyTy; |
| 2813 | valueKind = VK_RValue; |
| 2814 | break; |
| 2815 | } |
| 2816 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2817 | // C++ methods are l-values if static, r-values if non-static. |
| 2818 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2819 | valueKind = VK_LValue; |
| 2820 | break; |
| 2821 | } |
| 2822 | // fallthrough |
| 2823 | |
| 2824 | case Decl::CXXConversion: |
| 2825 | case Decl::CXXDestructor: |
| 2826 | case Decl::CXXConstructor: |
| 2827 | valueKind = VK_RValue; |
| 2828 | break; |
| 2829 | } |
| 2830 | |
| 2831 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2832 | } |
| 2833 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2834 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2835 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2836 | llvm_unreachable("unknown capture result"); |
| 2837 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2840 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2841 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2842 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2843 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2844 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2845 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2846 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2847 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2848 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2849 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2850 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2851 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2853 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2854 | if (!currentDecl && getCurBlock()) |
| 2855 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2856 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2857 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2858 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2859 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2860 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2861 | QualType ResTy; |
| 2862 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2863 | ResTy = Context.DependentTy; |
| 2864 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2865 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2866 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2867 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2868 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2869 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2870 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2871 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2874 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2875 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2876 | bool Invalid = false; |
| 2877 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2878 | if (Invalid) |
| 2879 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2880 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2881 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2882 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2883 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2884 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2886 | QualType Ty; |
| 2887 | if (!getLangOptions().CPlusPlus) |
| 2888 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2889 | else if (Literal.isWide()) |
| 2890 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2891 | else if (Literal.isMultiChar()) |
| 2892 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2893 | else |
| 2894 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2895 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2896 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2897 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2898 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2901 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2902 | // 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] | 2903 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2904 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2905 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2906 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2907 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2908 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2909 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2910 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2911 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2912 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2913 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2914 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2916 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2917 | bool Invalid = false; |
| 2918 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2919 | if (Invalid) |
| 2920 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2921 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2923 | Tok.getLocation(), PP); |
| 2924 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2925 | return ExprError(); |
| 2926 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2927 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2928 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2929 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2930 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2931 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2932 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2933 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2934 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2935 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2936 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2937 | |
| 2938 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2939 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2940 | using llvm::APFloat; |
| 2941 | APFloat Val(Format); |
| 2942 | |
| 2943 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2944 | |
| 2945 | // Overflow is always an error, but underflow is only an error if |
| 2946 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2947 | if ((result & APFloat::opOverflow) || |
| 2948 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2949 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2950 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2951 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2952 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2953 | APFloat::getLargest(Format).toString(buffer); |
| 2954 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2955 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2956 | APFloat::getSmallest(Format).toString(buffer); |
| 2957 | } |
| 2958 | |
| 2959 | Diag(Tok.getLocation(), diagnostic) |
| 2960 | << Ty |
| 2961 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2962 | } |
| 2963 | |
| 2964 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2965 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2966 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2967 | if (Ty == Context.DoubleTy) { |
| 2968 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2969 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2970 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2971 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2972 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2973 | } |
| 2974 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2975 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2976 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2977 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2978 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2979 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2980 | // long long is a C99 feature. |
| 2981 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2982 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2983 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2984 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2985 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2986 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2987 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2988 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2989 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2990 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2991 | Ty = Context.UnsignedLongLongTy; |
| 2992 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2993 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2994 | } else { |
| 2995 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2996 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2998 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2999 | // be an unsigned int. |
| 3000 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 3001 | |
| 3002 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3003 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 3004 | if (!Literal.isLong && !Literal.isLongLong) { |
| 3005 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3006 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3007 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3008 | // Does it fit in a unsigned int? |
| 3009 | if (ResultVal.isIntN(IntSize)) { |
| 3010 | // Does it fit in a signed int? |
| 3011 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3012 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3013 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3014 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3015 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3016 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3017 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3018 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3019 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3020 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3021 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3022 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3023 | // Does it fit in a unsigned long? |
| 3024 | if (ResultVal.isIntN(LongSize)) { |
| 3025 | // Does it fit in a signed long? |
| 3026 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3027 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3028 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3029 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3030 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3031 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3034 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3035 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3036 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3037 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3038 | // Does it fit in a unsigned long long? |
| 3039 | if (ResultVal.isIntN(LongLongSize)) { |
| 3040 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 3041 | // To be compatible with MSVC, hex integer literals ending with the |
| 3042 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 3043 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 3044 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3045 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3046 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3047 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3048 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3051 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3052 | // If we still couldn't decide a type, we probably have something that |
| 3053 | // 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] | 3054 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3056 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3057 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3058 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3060 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3061 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3062 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3063 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3064 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3065 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 3066 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 3067 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3069 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3070 | |
| 3071 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3074 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3075 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3076 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3077 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3080 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 3081 | SourceLocation Loc, |
| 3082 | SourceRange ArgRange) { |
| 3083 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 3084 | // scalar or vector data type argument..." |
| 3085 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 3086 | // type (C99 6.2.5p18) or void. |
| 3087 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 3088 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 3089 | << T << ArgRange; |
| 3090 | return true; |
| 3091 | } |
| 3092 | |
| 3093 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 3094 | "Scalar types should always be complete"); |
| 3095 | return false; |
| 3096 | } |
| 3097 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3098 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 3099 | SourceLocation Loc, |
| 3100 | SourceRange ArgRange, |
| 3101 | UnaryExprOrTypeTrait TraitKind) { |
| 3102 | // C99 6.5.3.4p1: |
| 3103 | if (T->isFunctionType()) { |
| 3104 | // alignof(function) is allowed as an extension. |
| 3105 | if (TraitKind == UETT_SizeOf) |
| 3106 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 3107 | return false; |
| 3108 | } |
| 3109 | |
| 3110 | // Allow sizeof(void)/alignof(void) as an extension. |
| 3111 | if (T->isVoidType()) { |
| 3112 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 3113 | return false; |
| 3114 | } |
| 3115 | |
| 3116 | return true; |
| 3117 | } |
| 3118 | |
| 3119 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 3120 | SourceLocation Loc, |
| 3121 | SourceRange ArgRange, |
| 3122 | UnaryExprOrTypeTrait TraitKind) { |
| 3123 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 3124 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 3125 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 3126 | << T << (TraitKind == UETT_SizeOf) |
| 3127 | << ArgRange; |
| 3128 | return true; |
| 3129 | } |
| 3130 | |
| 3131 | return false; |
| 3132 | } |
| 3133 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3134 | /// \brief Check the constrains on expression operands to unary type expression |
| 3135 | /// and type traits. |
| 3136 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3137 | /// Completes any types necessary and validates the constraints on the operand |
| 3138 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 3139 | /// the expression as it completes the type for that expression through template |
| 3140 | /// instantiation, etc. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3141 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 3142 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3143 | QualType ExprTy = Op->getType(); |
| 3144 | |
| 3145 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3146 | // the result is the size of the referenced type." |
| 3147 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3148 | // result shall be the alignment of the referenced type." |
| 3149 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3150 | ExprTy = Ref->getPointeeType(); |
| 3151 | |
| 3152 | if (ExprKind == UETT_VecStep) |
| 3153 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3154 | Op->getSourceRange()); |
| 3155 | |
| 3156 | // Whitelist some types as extensions |
| 3157 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3158 | Op->getSourceRange(), ExprKind)) |
| 3159 | return false; |
| 3160 | |
| 3161 | if (RequireCompleteExprType(Op, |
| 3162 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 3163 | << ExprKind << Op->getSourceRange(), |
| 3164 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 3165 | return true; |
| 3166 | |
| 3167 | // Completeing the expression's type may have changed it. |
| 3168 | ExprTy = Op->getType(); |
| 3169 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3170 | ExprTy = Ref->getPointeeType(); |
| 3171 | |
| 3172 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 3173 | Op->getSourceRange(), ExprKind)) |
| 3174 | return true; |
| 3175 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3176 | if (ExprKind == UETT_SizeOf) { |
| 3177 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 3178 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 3179 | QualType OType = PVD->getOriginalType(); |
| 3180 | QualType Type = PVD->getType(); |
| 3181 | if (Type->isPointerType() && OType->isArrayType()) { |
| 3182 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 3183 | << Type << OType; |
| 3184 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3190 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /// \brief Check the constraints on operands to unary expression and type |
| 3194 | /// traits. |
| 3195 | /// |
| 3196 | /// This will complete any types necessary, and validate the various constraints |
| 3197 | /// on those operands. |
| 3198 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3199 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3200 | /// C99 6.3.2.1p[2-4] all state: |
| 3201 | /// Except when it is the operand of the sizeof operator ... |
| 3202 | /// |
| 3203 | /// C++ [expr.sizeof]p4 |
| 3204 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3205 | /// standard conversions are not applied to the operand of sizeof. |
| 3206 | /// |
| 3207 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3208 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 3209 | SourceLocation OpLoc, |
| 3210 | SourceRange ExprRange, |
| 3211 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3212 | if (exprType->isDependentType()) |
| 3213 | return false; |
| 3214 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3215 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3216 | // the result is the size of the referenced type." |
| 3217 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3218 | // result shall be the alignment of the referenced type." |
| 3219 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 3220 | exprType = Ref->getPointeeType(); |
| 3221 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3222 | if (ExprKind == UETT_VecStep) |
| 3223 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3224 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3225 | // Whitelist some types as extensions |
| 3226 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 3227 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3228 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3230 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 3231 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3232 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3233 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3235 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 3236 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3237 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3239 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3242 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3243 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3244 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3246 | if (isa<DeclRefExpr>(E)) |
| 3247 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3248 | |
| 3249 | // Cannot know anything else if the expression is dependent. |
| 3250 | if (E->isTypeDependent()) |
| 3251 | return false; |
| 3252 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3253 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3254 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3255 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3256 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3257 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3258 | |
| 3259 | // Alignment of a field access is always okay, so long as it isn't a |
| 3260 | // bit-field. |
| 3261 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3262 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3263 | return false; |
| 3264 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3265 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3268 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3269 | E = E->IgnoreParens(); |
| 3270 | |
| 3271 | // Cannot know anything else if the expression is dependent. |
| 3272 | if (E->isTypeDependent()) |
| 3273 | return false; |
| 3274 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3275 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3278 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3279 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3280 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3281 | SourceLocation OpLoc, |
| 3282 | UnaryExprOrTypeTrait ExprKind, |
| 3283 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3284 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3285 | return ExprError(); |
| 3286 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3287 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3289 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3290 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3291 | return ExprError(); |
| 3292 | |
| 3293 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3294 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3295 | Context.getSizeType(), |
| 3296 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /// \brief Build a sizeof or alignof expression given an expression |
| 3300 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3301 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3302 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3303 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame^] | 3304 | ExprResult PE = CheckPlaceholderExpr(E); |
| 3305 | if (PE.isInvalid()) |
| 3306 | return ExprError(); |
| 3307 | |
| 3308 | E = PE.get(); |
| 3309 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3310 | // Verify that the operand is valid. |
| 3311 | bool isInvalid = false; |
| 3312 | if (E->isTypeDependent()) { |
| 3313 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3314 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3315 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3316 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3317 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3318 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3319 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3320 | isInvalid = true; |
| 3321 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3322 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | if (isInvalid) |
| 3326 | return ExprError(); |
| 3327 | |
| 3328 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3329 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3330 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3331 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3334 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3335 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3336 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3337 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3338 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 3339 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 3340 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3341 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3342 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3343 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3344 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3345 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3346 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3347 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3350 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3351 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3352 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3355 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3356 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3357 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3358 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3360 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3361 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3362 | V = S.DefaultLvalueConversion(V.take()); |
| 3363 | if (V.isInvalid()) |
| 3364 | return QualType(); |
| 3365 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3367 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3368 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3369 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3371 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3372 | if (V.get()->getType()->isArithmeticType()) |
| 3373 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3375 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3376 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3377 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3378 | if (PR.get() != V.get()) { |
| 3379 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3380 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3383 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3384 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3385 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3386 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3390 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3391 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3392 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3393 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3394 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3395 | switch (Kind) { |
| 3396 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3397 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3398 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3399 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3400 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3401 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3404 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3405 | /// having l-value type. These are: |
| 3406 | /// - 'void', but not qualified void |
| 3407 | /// - function types |
| 3408 | /// |
| 3409 | /// The exact rule here is C99 6.3.2.1: |
| 3410 | /// An lvalue is an expression with an object type or an incomplete |
| 3411 | /// type other than void. |
| 3412 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3413 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3414 | T->isFunctionType()); |
| 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::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3419 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3420 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3421 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3422 | if (Result.isInvalid()) return ExprError(); |
| 3423 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3424 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3425 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3427 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3428 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3429 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3430 | Context.DependentTy, |
| 3431 | VK_LValue, OK_Ordinary, |
| 3432 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3436 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3437 | LHSExp->getType()->isEnumeralType() || |
| 3438 | RHSExp->getType()->isRecordType() || |
| 3439 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3440 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3443 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3447 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3448 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3449 | Expr *Idx, SourceLocation RLoc) { |
| 3450 | Expr *LHSExp = Base; |
| 3451 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3453 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3454 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3455 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3456 | if (Result.isInvalid()) |
| 3457 | return ExprError(); |
| 3458 | LHSExp = Result.take(); |
| 3459 | } |
| 3460 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3461 | if (Result.isInvalid()) |
| 3462 | return ExprError(); |
| 3463 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3464 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3465 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3466 | ExprValueKind VK = VK_LValue; |
| 3467 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3469 | // 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] | 3470 | // 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] | 3471 | // 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] | 3472 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3473 | Expr *BaseExpr, *IndexExpr; |
| 3474 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3475 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3476 | BaseExpr = LHSExp; |
| 3477 | IndexExpr = RHSExp; |
| 3478 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3479 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3480 | BaseExpr = LHSExp; |
| 3481 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3482 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3483 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3484 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3485 | BaseExpr = RHSExp; |
| 3486 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3487 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3489 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3490 | BaseExpr = LHSExp; |
| 3491 | IndexExpr = RHSExp; |
| 3492 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3494 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3495 | // Handle the uncommon case of "123[Ptr]". |
| 3496 | BaseExpr = RHSExp; |
| 3497 | IndexExpr = LHSExp; |
| 3498 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3499 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3500 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3501 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3502 | VK = LHSExp->getValueKind(); |
| 3503 | if (VK != VK_RValue) |
| 3504 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3505 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3506 | // FIXME: need to deal with const... |
| 3507 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3508 | } else if (LHSTy->isArrayType()) { |
| 3509 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3510 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3511 | // wasn't promoted because of the C90 rule that doesn't |
| 3512 | // allow promoting non-lvalue arrays. Warn, then |
| 3513 | // force the promotion here. |
| 3514 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3515 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3516 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3517 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3518 | LHSTy = LHSExp->getType(); |
| 3519 | |
| 3520 | BaseExpr = LHSExp; |
| 3521 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3522 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3523 | } else if (RHSTy->isArrayType()) { |
| 3524 | // Same as previous, except for 123[f().a] case |
| 3525 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3526 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3527 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3528 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3529 | RHSTy = RHSExp->getType(); |
| 3530 | |
| 3531 | BaseExpr = RHSExp; |
| 3532 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3533 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3534 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3535 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3536 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3537 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3538 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3539 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3540 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3541 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3542 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3543 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3544 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3545 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3546 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3547 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3548 | // 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] | 3549 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3550 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3551 | // incomplete types are not object types. |
| 3552 | if (ResultType->isFunctionType()) { |
| 3553 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3554 | << ResultType << BaseExpr->getSourceRange(); |
| 3555 | return ExprError(); |
| 3556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3558 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3559 | // GNU extension: subscripting on pointer to void |
| 3560 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3561 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3562 | |
| 3563 | // C forbids expressions of unqualified void type from being l-values. |
| 3564 | // See IsCForbiddenLValueType. |
| 3565 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3566 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3568 | PDiag(diag::err_subscript_incomplete_type) |
| 3569 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3570 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3572 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3573 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3574 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3575 | << ResultType << BaseExpr->getSourceRange(); |
| 3576 | return ExprError(); |
| 3577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3579 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3580 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3581 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3582 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3583 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3586 | /// Check an ext-vector component access expression. |
| 3587 | /// |
| 3588 | /// VK should be set in advance to the value kind of the base |
| 3589 | /// expression. |
| 3590 | static QualType |
| 3591 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3592 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3593 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3594 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3595 | // see FIXME there. |
| 3596 | // |
| 3597 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3598 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3599 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3600 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3601 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3602 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3603 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3604 | // 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] | 3605 | // special names that indicate a subset of exactly half the elements are |
| 3606 | // to be selected. |
| 3607 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3608 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3609 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3610 | // 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] | 3611 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3612 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3613 | bool HasRepeated = false; |
| 3614 | bool HasIndex[16] = {}; |
| 3615 | |
| 3616 | int Idx; |
| 3617 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3618 | // Check that we've found one of the special components, or that the component |
| 3619 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3620 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3621 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3622 | HalvingSwizzle = true; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3623 | } else if (!HexSwizzle && |
| 3624 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3625 | do { |
| 3626 | if (HasIndex[Idx]) HasRepeated = true; |
| 3627 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3628 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3629 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3630 | } else { |
| 3631 | if (HexSwizzle) compStr++; |
| 3632 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3633 | if (HasIndex[Idx]) HasRepeated = true; |
| 3634 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3635 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3636 | } |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3637 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3638 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3639 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3640 | // We didn't get to the end of the string. This means the component names |
| 3641 | // 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] | 3642 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3643 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3644 | return QualType(); |
| 3645 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3646 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3647 | // Ensure no component accessor exceeds the width of the vector type it |
| 3648 | // operates on. |
| 3649 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3650 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3651 | |
| 3652 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3653 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3654 | |
| 3655 | while (*compStr) { |
| 3656 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3657 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3658 | << baseType << SourceRange(CompLoc); |
| 3659 | return QualType(); |
| 3660 | } |
| 3661 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3662 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3663 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3664 | // 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] | 3665 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3666 | // 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] | 3667 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3668 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3669 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3670 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3671 | if (HexSwizzle) |
| 3672 | CompSize--; |
| 3673 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3674 | if (CompSize == 1) |
| 3675 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3676 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3677 | if (HasRepeated) VK = VK_RValue; |
| 3678 | |
| 3679 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3680 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3681 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3682 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3683 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3684 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3685 | } |
| 3686 | 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] | 3687 | } |
| 3688 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3689 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3690 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3691 | const Selector &Sel, |
| 3692 | ASTContext &Context) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3693 | if (Member) |
| 3694 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3695 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3696 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3697 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3699 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3700 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3701 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3702 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3703 | return D; |
| 3704 | } |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3708 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3709 | IdentifierInfo *Member, |
| 3710 | const Selector &Sel, |
| 3711 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3712 | // Check protocols on qualified interfaces. |
| 3713 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3714 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3715 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3716 | if (Member) |
| 3717 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3718 | GDecl = PD; |
| 3719 | break; |
| 3720 | } |
| 3721 | // 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] | 3722 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3723 | GDecl = OMD; |
| 3724 | break; |
| 3725 | } |
| 3726 | } |
| 3727 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3728 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3729 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3730 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3731 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3732 | Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3733 | if (GDecl) |
| 3734 | return GDecl; |
| 3735 | } |
| 3736 | } |
| 3737 | return GDecl; |
| 3738 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3739 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3740 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3741 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3742 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3743 | const CXXScopeSpec &SS, |
| 3744 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3745 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3746 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3747 | // Even in dependent contexts, try to diagnose base expressions with |
| 3748 | // obviously wrong types, e.g.: |
| 3749 | // |
| 3750 | // T* t; |
| 3751 | // t.f; |
| 3752 | // |
| 3753 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3754 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3755 | // allows this, while still reporting an error if T is a struct pointer. |
| 3756 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3757 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3758 | if (PT && (!getLangOptions().ObjC1 || |
| 3759 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3760 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3761 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3762 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3763 | return ExprError(); |
| 3764 | } |
| 3765 | } |
| 3766 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3767 | assert(BaseType->isDependentType() || |
| 3768 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3769 | isDependentScopeSpecifier(SS)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3770 | |
| 3771 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3772 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3773 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3774 | IsArrow, OpLoc, |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3775 | SS.getWithLocInContext(Context), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3776 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3777 | NameInfo, TemplateArgs)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | /// We know that the given qualified member reference points only to |
| 3781 | /// declarations which do not belong to the static type of the base |
| 3782 | /// expression. Diagnose the problem. |
| 3783 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3784 | Expr *BaseExpr, |
| 3785 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3786 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3787 | NamedDecl *rep, |
| 3788 | const DeclarationNameInfo &nameInfo) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3789 | // If this is an implicit member access, use a different set of |
| 3790 | // diagnostics. |
| 3791 | if (!BaseExpr) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3792 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3793 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3794 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3795 | << SS.getRange() << rep << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | // Check whether the declarations we found through a nested-name |
| 3799 | // specifier in a member expression are actually members of the base |
| 3800 | // type. The restriction here is: |
| 3801 | // |
| 3802 | // C++ [expr.ref]p2: |
| 3803 | // ... In these cases, the id-expression shall name a |
| 3804 | // member of the class or of one of its base classes. |
| 3805 | // |
| 3806 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3807 | // an unrelated class, and for us to find an overload set including |
| 3808 | // decls from classes which are not superclasses, as long as the decl |
| 3809 | // we actually pick through overload resolution is from a superclass. |
| 3810 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3811 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3812 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3813 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3814 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3815 | if (!BaseRT) { |
| 3816 | // We can't check this yet because the base type is still |
| 3817 | // dependent. |
| 3818 | assert(BaseType->isDependentType()); |
| 3819 | return false; |
| 3820 | } |
| 3821 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3822 | |
| 3823 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3824 | // If this is an implicit member reference and we find a |
| 3825 | // non-instance member, it's not an error. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3826 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3827 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3828 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3829 | // 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] | 3830 | DeclContext *DC = (*I)->getDeclContext(); |
| 3831 | while (DC->isTransparentContext()) |
| 3832 | DC = DC->getParent(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | 9d4bb94 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3834 | if (!DC->isRecord()) |
| 3835 | continue; |
| 3836 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3837 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3838 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3839 | |
| 3840 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3841 | return false; |
| 3842 | } |
| 3843 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3844 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3845 | R.getRepresentativeDecl(), |
| 3846 | R.getLookupNameInfo()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3847 | return true; |
| 3848 | } |
| 3849 | |
| 3850 | static bool |
| 3851 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3852 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3853 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3854 | bool HasTemplateArgs) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3855 | RecordDecl *RDecl = RTy->getDecl(); |
| 3856 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3857 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3858 | << BaseRange)) |
| 3859 | return true; |
| 3860 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3861 | if (HasTemplateArgs) { |
| 3862 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3863 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3864 | |
| 3865 | bool MOUS; |
| 3866 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3867 | return false; |
| 3868 | } |
| 3869 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3870 | DeclContext *DC = RDecl; |
| 3871 | if (SS.isSet()) { |
| 3872 | // If the member name was a qualified-id, look into the |
| 3873 | // nested-name-specifier. |
| 3874 | DC = SemaRef.computeDeclContext(SS, false); |
| 3875 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3876 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3877 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3878 | << SS.getRange() << DC; |
| 3879 | return true; |
| 3880 | } |
| 3881 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3882 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3883 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3884 | if (!isa<TypeDecl>(DC)) { |
| 3885 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3886 | << DC << SS.getRange(); |
| 3887 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3888 | } |
| 3889 | } |
| 3890 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3891 | // The record definition is complete, now look up the member. |
| 3892 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3893 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3894 | if (!R.empty()) |
| 3895 | return false; |
| 3896 | |
| 3897 | // We didn't find anything with the given name, so try to correct |
| 3898 | // for typos. |
| 3899 | DeclarationName Name = R.getLookupName(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3900 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3901 | !R.empty() && |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3902 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3903 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3904 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3905 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3906 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3907 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3908 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3909 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3910 | return false; |
| 3911 | } else { |
| 3912 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3913 | R.setLookupName(Name); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3916 | return false; |
| 3917 | } |
| 3918 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3919 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3920 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3921 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3922 | CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3923 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3924 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3925 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3926 | if (BaseType->isDependentType() || |
| 3927 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3928 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3929 | IsArrow, OpLoc, |
| 3930 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3931 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3932 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3933 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3934 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3935 | // Implicit member accesses. |
| 3936 | if (!Base) { |
| 3937 | QualType RecordTy = BaseType; |
| 3938 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3939 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3940 | RecordTy->getAs<RecordType>(), |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3941 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3942 | return ExprError(); |
| 3943 | |
| 3944 | // Explicit member accesses. |
| 3945 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3946 | ExprResult BaseResult = Owned(Base); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3947 | ExprResult Result = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3948 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3949 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3950 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3951 | if (BaseResult.isInvalid()) |
| 3952 | return ExprError(); |
| 3953 | Base = BaseResult.take(); |
| 3954 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3955 | if (Result.isInvalid()) { |
| 3956 | Owned(Base); |
| 3957 | return ExprError(); |
| 3958 | } |
| 3959 | |
| 3960 | if (Result.get()) |
| 3961 | return move(Result); |
Sebastian Redl | f3e6337 | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3962 | |
| 3963 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3964 | BaseType = Base->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3967 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3968 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3969 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3972 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3973 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3974 | SourceLocation OpLoc, bool IsArrow, |
| 3975 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3976 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3977 | LookupResult &R, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3978 | const TemplateArgumentListInfo *TemplateArgs, |
| 3979 | bool SuppressQualifierCheck) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3980 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3981 | if (IsArrow) { |
| 3982 | assert(BaseType->isPointerType()); |
| 3983 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3984 | } |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3985 | R.setBaseObjectType(BaseType); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3986 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3987 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3988 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3989 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3990 | |
| 3991 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3992 | return ExprError(); |
| 3993 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3994 | if (R.empty()) { |
| 3995 | // Rederive where we looked up. |
| 3996 | DeclContext *DC = (SS.isSet() |
| 3997 | ? computeDeclContext(SS, false) |
| 3998 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3999 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4000 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4001 | << MemberName << DC |
| 4002 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4003 | return ExprError(); |
| 4004 | } |
| 4005 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4006 | // Diagnose lookups that find only declarations from a non-base |
| 4007 | // type. This is possible for either qualified lookups (which may |
| 4008 | // have been qualified with an unrelated type) or implicit member |
| 4009 | // expressions (which were found with unqualified lookup and thus |
| 4010 | // may have come from an enclosing scope). Note that it's okay for |
| 4011 | // lookup to find declarations from a non-base type as long as those |
| 4012 | // aren't the ones picked by overload resolution. |
| 4013 | if ((SS.isSet() || !BaseExpr || |
| 4014 | (isa<CXXThisExpr>(BaseExpr) && |
| 4015 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4016 | !SuppressQualifierCheck && |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4017 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4018 | return ExprError(); |
| 4019 | |
| 4020 | // Construct an unresolved result if we in fact got an unresolved |
| 4021 | // result. |
| 4022 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4023 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 4024 | // pick a member. |
| 4025 | R.suppressDiagnostics(); |
| 4026 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4027 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4028 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4029 | BaseExpr, BaseExprType, |
| 4030 | IsArrow, OpLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4031 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4032 | MemberNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4033 | TemplateArgs, R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4034 | |
| 4035 | return Owned(MemExpr); |
| 4036 | } |
| 4037 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4038 | assert(R.isSingleResult()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4039 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4040 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 4041 | |
| 4042 | // FIXME: diagnose the presence of template arguments now. |
| 4043 | |
| 4044 | // If the decl being referenced had an error, return an error for this |
| 4045 | // sub-expr without emitting another error, in order to avoid cascading |
| 4046 | // error cases. |
| 4047 | if (MemberDecl->isInvalidDecl()) |
| 4048 | return ExprError(); |
| 4049 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4050 | // Handle the implicit-member-access case. |
| 4051 | if (!BaseExpr) { |
| 4052 | // 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] | 4053 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4054 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4055 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 4056 | SourceLocation Loc = R.getNameLoc(); |
| 4057 | if (SS.getRange().isValid()) |
| 4058 | Loc = SS.getRange().getBegin(); |
| 4059 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4062 | bool ShouldCheckUse = true; |
| 4063 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 4064 | // Don't diagnose the use of a virtual member function unless it's |
| 4065 | // explicitly qualified. |
| 4066 | if (MD->isVirtual() && !SS.isSet()) |
| 4067 | ShouldCheckUse = false; |
| 4068 | } |
| 4069 | |
| 4070 | // Check the use of this member. |
| 4071 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 4072 | Owned(BaseExpr); |
| 4073 | return ExprError(); |
| 4074 | } |
| 4075 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4076 | // Perform a property load on the base regardless of whether we |
| 4077 | // actually need it for the declaration. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4078 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 4079 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 4080 | if (Result.isInvalid()) |
| 4081 | return ExprError(); |
| 4082 | BaseExpr = Result.take(); |
| 4083 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4084 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4085 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 4086 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 4087 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4088 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4089 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 4090 | // We may have found a field within an anonymous union or struct |
| 4091 | // (C++ [class.union]). |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 4092 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4093 | BaseExpr, OpLoc); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4094 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4095 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 4096 | MarkDeclarationReferenced(MemberLoc, Var); |
| 4097 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4098 | Var, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4099 | Var->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4100 | VK_LValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4103 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4104 | ExprValueKind valueKind; |
| 4105 | QualType type; |
| 4106 | if (MemberFn->isInstance()) { |
| 4107 | valueKind = VK_RValue; |
| 4108 | type = Context.BoundMemberTy; |
| 4109 | } else { |
| 4110 | valueKind = VK_LValue; |
| 4111 | type = MemberFn->getType(); |
| 4112 | } |
| 4113 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4114 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4115 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4116 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4117 | type, valueKind, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4118 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4119 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4120 | |
| 4121 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 4122 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4123 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4124 | Enum, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4125 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | Owned(BaseExpr); |
| 4129 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4130 | // We found something that we didn't expect. Complain. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4131 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4132 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4133 | << MemberName << BaseType << int(IsArrow); |
| 4134 | else |
| 4135 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 4136 | << MemberName << BaseType << int(IsArrow); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4137 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4138 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 4139 | << MemberName; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4140 | R.suppressDiagnostics(); |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4141 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4144 | /// Given that normal member access failed on the given expression, |
| 4145 | /// and given that the expression's type involves builtin-id or |
| 4146 | /// builtin-Class, decide whether substituting in the redefinition |
| 4147 | /// types would be profitable. The redefinition type is whatever |
| 4148 | /// this translation unit tried to typedef to id/Class; we store |
| 4149 | /// it to the side and then re-use it in places like this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4150 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4151 | const ObjCObjectPointerType *opty |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4152 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4153 | if (!opty) return false; |
| 4154 | |
| 4155 | const ObjCObjectType *ty = opty->getObjectType(); |
| 4156 | |
| 4157 | QualType redef; |
| 4158 | if (ty->isObjCId()) { |
| 4159 | redef = S.Context.ObjCIdRedefinitionType; |
| 4160 | } else if (ty->isObjCClass()) { |
| 4161 | redef = S.Context.ObjCClassRedefinitionType; |
| 4162 | } else { |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | // Do the substitution as long as the redefinition type isn't just a |
| 4167 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 4168 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 4169 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 4170 | return false; |
| 4171 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4172 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4173 | return true; |
| 4174 | } |
| 4175 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4176 | /// Look up the given member of the given non-type-dependent |
| 4177 | /// expression. This can return in one of two ways: |
| 4178 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 4179 | /// assume that lookup was performed and the results written into |
| 4180 | /// the provided structure. It will take over from there. |
| 4181 | /// * Otherwise, the returned expression will be produced in place of |
| 4182 | /// an ordinary member expression. |
| 4183 | /// |
| 4184 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 4185 | /// fixed for ObjC++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4186 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4187 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 4188 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4189 | CXXScopeSpec &SS, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4190 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4191 | assert(BaseExpr.get() && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 4193 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4194 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4195 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4196 | if (IsArrow) { |
| 4197 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4198 | if (BaseExpr.isInvalid()) |
| 4199 | return ExprError(); |
| 4200 | } |
| 4201 | |
| 4202 | QualType BaseType = BaseExpr.get()->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4203 | assert(!BaseType->isDependentType()); |
| 4204 | |
| 4205 | DeclarationName MemberName = R.getLookupName(); |
| 4206 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4207 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4208 | // For later type-checking purposes, turn arrow accesses into dot |
| 4209 | // accesses. The only access type we support that doesn't follow |
| 4210 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 4211 | // and those never use arrows, so this is unaffected. |
| 4212 | if (IsArrow) { |
| 4213 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4214 | BaseType = Ptr->getPointeeType(); |
| 4215 | else if (const ObjCObjectPointerType *Ptr |
| 4216 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 4217 | BaseType = Ptr->getPointeeType(); |
| 4218 | else if (BaseType->isRecordType()) { |
| 4219 | // Recover from arrow accesses to records, e.g.: |
| 4220 | // struct MyRecord foo; |
| 4221 | // foo->bar |
| 4222 | // This is actually well-formed in C++ if MyRecord has an |
| 4223 | // overloaded operator->, but that should have been dealt with |
| 4224 | // by now. |
| 4225 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4226 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4227 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4228 | IsArrow = false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4229 | } else if (BaseType == Context.BoundMemberTy) { |
| 4230 | goto fail; |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4231 | } else { |
| 4232 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4233 | << BaseType << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4234 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4235 | } |
| 4236 | } |
| 4237 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4238 | // Handle field access to simple records. |
| 4239 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4240 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4241 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 4242 | return ExprError(); |
| 4243 | |
| 4244 | // Returning valid-but-null is how we indicate to the caller that |
| 4245 | // the lookup result was filled in. |
| 4246 | return Owned((Expr*) 0); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4247 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4248 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4249 | // Handle ivar access to Objective-C objects. |
| 4250 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4251 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4252 | |
| 4253 | // There are three cases for the base type: |
| 4254 | // - builtin id (qualified or unqualified) |
| 4255 | // - builtin Class (qualified or unqualified) |
| 4256 | // - an interface |
| 4257 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 4258 | if (!IDecl) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4259 | if (getLangOptions().ObjCAutoRefCount && |
| 4260 | (OTy->isObjCId() || OTy->isObjCClass())) |
| 4261 | goto fail; |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4262 | // There's an implicit 'isa' ivar on all objects. |
| 4263 | // But we only actually find it this way on objects of type 'id', |
| 4264 | // apparently. |
| 4265 | if (OTy->isObjCId() && Member->isStr("isa")) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4266 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4267 | Context.getObjCClassType())); |
| 4268 | |
| 4269 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4270 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4271 | ObjCImpDecl, HasTemplateArgs); |
| 4272 | goto fail; |
| 4273 | } |
| 4274 | |
| 4275 | ObjCInterfaceDecl *ClassDeclared; |
| 4276 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 4277 | |
| 4278 | if (!IV) { |
| 4279 | // Attempt to correct for typos in ivar names. |
| 4280 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 4281 | LookupMemberName); |
| 4282 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 4283 | IsArrow ? CTC_ObjCIvarLookup |
| 4284 | : CTC_ObjCPropertyLookup) && |
| 4285 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 4286 | Diag(R.getNameLoc(), |
| 4287 | diag::err_typecheck_member_reference_ivar_suggest) |
| 4288 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 4289 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 4290 | IV->getNameAsString()); |
| 4291 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 4292 | << IV->getDeclName(); |
| 4293 | } else { |
| 4294 | Res.clear(); |
| 4295 | Res.setLookupName(Member); |
| 4296 | |
| 4297 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 4298 | << IDecl->getDeclName() << MemberName |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4299 | << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4300 | return ExprError(); |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | // If the decl being referenced had an error, return an error for this |
| 4305 | // sub-expr without emitting another error, in order to avoid cascading |
| 4306 | // error cases. |
| 4307 | if (IV->isInvalidDecl()) |
| 4308 | return ExprError(); |
| 4309 | |
| 4310 | // Check whether we can reference this field. |
| 4311 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 4312 | return ExprError(); |
| 4313 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 4314 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 4315 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 4316 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 4317 | ClassOfMethodDecl = MD->getClassInterface(); |
| 4318 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 4319 | // Case of a c-function declared inside an objc implementation. |
| 4320 | // FIXME: For a c-style function nested inside an objc implementation |
| 4321 | // class, there is no implementation context available, so we pass |
| 4322 | // down the context as argument to this routine. Ideally, this context |
| 4323 | // need be passed down in the AST node and somehow calculated from the |
| 4324 | // AST for a function decl. |
| 4325 | if (ObjCImplementationDecl *IMPD = |
| 4326 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 4327 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 4328 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 4329 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 4330 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 4331 | } |
| 4332 | |
| 4333 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 4334 | if (ClassDeclared != IDecl || |
| 4335 | ClassOfMethodDecl != ClassDeclared) |
| 4336 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 4337 | << IV->getDeclName(); |
| 4338 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 4339 | // @protected |
| 4340 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 4341 | << IV->getDeclName(); |
| 4342 | } |
Fariborz Jahanian | b1f7d24 | 2011-06-16 17:29:56 +0000 | [diff] [blame] | 4343 | if (getLangOptions().ObjCAutoRefCount) { |
| 4344 | Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); |
| 4345 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp)) |
| 4346 | if (UO->getOpcode() == UO_Deref) |
| 4347 | BaseExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 4348 | |
| 4349 | if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp)) |
| 4350 | if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4351 | Diag(DE->getLocation(), diag::error_arc_weak_ivar_access); |
| 4352 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4353 | |
| 4354 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4355 | MemberLoc, BaseExpr.take(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4356 | IsArrow)); |
| 4357 | } |
| 4358 | |
| 4359 | // Objective-C property access. |
| 4360 | const ObjCObjectPointerType *OPT; |
| 4361 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 4362 | // This actually uses the base as an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4363 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4364 | if (BaseExpr.isInvalid()) |
| 4365 | return ExprError(); |
| 4366 | |
| 4367 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4368 | |
| 4369 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 4370 | |
| 4371 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 4372 | |
| 4373 | // id, with and without qualifiers. |
| 4374 | if (OT->isObjCId()) { |
| 4375 | // Check protocols on qualified interfaces. |
| 4376 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 4377 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 4378 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 4379 | // Check the use of this declaration |
| 4380 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 4381 | return ExprError(); |
| 4382 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4383 | QualType T = PD->getType(); |
| 4384 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 4385 | T = getMessageSendResultType(BaseType, Getter, false, false); |
| 4386 | |
| 4387 | return Owned(new (Context) ObjCPropertyRefExpr(PD, T, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4388 | VK_LValue, |
| 4389 | OK_ObjCProperty, |
| 4390 | MemberLoc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4391 | BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4395 | // Check the use of this method. |
| 4396 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4397 | return ExprError(); |
| 4398 | Selector SetterSel = |
| 4399 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4400 | PP.getSelectorTable(), Member); |
| 4401 | ObjCMethodDecl *SMD = 0; |
| 4402 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4403 | SetterSel, Context)) |
| 4404 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4405 | QualType PType = getMessageSendResultType(BaseType, OMD, false, |
| 4406 | false); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4407 | |
| 4408 | ExprValueKind VK = VK_LValue; |
| 4409 | if (!getLangOptions().CPlusPlus && |
| 4410 | IsCForbiddenLValueType(Context, PType)) |
| 4411 | VK = VK_RValue; |
| 4412 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4413 | |
| 4414 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4415 | VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4416 | MemberLoc, BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4417 | } |
| 4418 | } |
Fariborz Jahanian | 4eb7f69 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4419 | // Use of id.member can only be for a property reference. Do not |
| 4420 | // use the 'id' redefinition in this case. |
| 4421 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4422 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4423 | ObjCImpDecl, HasTemplateArgs); |
| 4424 | |
| 4425 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4426 | << MemberName << BaseType); |
| 4427 | } |
| 4428 | |
| 4429 | // 'Class', unqualified only. |
| 4430 | if (OT->isObjCClass()) { |
| 4431 | // Only works in a method declaration (??!). |
| 4432 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4433 | if (!MD) { |
| 4434 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4435 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4436 | ObjCImpDecl, HasTemplateArgs); |
| 4437 | |
| 4438 | goto fail; |
| 4439 | } |
| 4440 | |
| 4441 | // Also must look for a getter name which uses property syntax. |
| 4442 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4443 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4444 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4445 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4446 | // Check the use of this method. |
| 4447 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4448 | return ExprError(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4449 | } else |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4450 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4451 | // If we found a getter then this may be a valid dot-reference, we |
| 4452 | // will look for the matching setter, in case it is needed. |
| 4453 | Selector SetterSel = |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4454 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4455 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4456 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4457 | if (!Setter) { |
| 4458 | // If this reference is in an @implementation, also check for 'private' |
| 4459 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4460 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4461 | } |
| 4462 | // Look through local category implementations associated with the class. |
| 4463 | if (!Setter) |
| 4464 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4465 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4466 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4467 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4468 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4469 | if (Getter || Setter) { |
| 4470 | QualType PType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4471 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4472 | ExprValueKind VK = VK_LValue; |
| 4473 | if (Getter) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4474 | PType = getMessageSendResultType(QualType(OT, 0), Getter, true, |
| 4475 | false); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4476 | if (!getLangOptions().CPlusPlus && |
| 4477 | IsCForbiddenLValueType(Context, PType)) |
| 4478 | VK = VK_RValue; |
| 4479 | } else { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4480 | // Get the expression type from Setter's incoming parameter. |
| 4481 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4482 | } |
| 4483 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4484 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4485 | // FIXME: we must check that the setter has property type. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4486 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4487 | PType, VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4488 | MemberLoc, BaseExpr.take())); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4489 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4490 | |
| 4491 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4492 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4493 | ObjCImpDecl, HasTemplateArgs); |
| 4494 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4495 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4496 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4497 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4498 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4499 | // Normal property access. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4500 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4501 | SourceLocation(), QualType(), false); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4502 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4503 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4504 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4505 | if (BaseType->isExtVectorType()) { |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4506 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4507 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4508 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4509 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4510 | Member, MemberLoc); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4511 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4512 | return ExprError(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4513 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4514 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4515 | *Member, MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4516 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4517 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4518 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4519 | // not just a pointer to builtin-sel again. |
| 4520 | if (IsArrow && |
| 4521 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4522 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4523 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4524 | CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4525 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4526 | ObjCImpDecl, HasTemplateArgs); |
| 4527 | } |
| 4528 | |
| 4529 | // Failure cases. |
| 4530 | fail: |
| 4531 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4532 | // Recover from dot accesses to pointers, e.g.: |
| 4533 | // type *foo; |
| 4534 | // foo.bar |
| 4535 | // This is actually well-formed in two cases: |
| 4536 | // - 'type' is an Objective C type |
| 4537 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4538 | // the appropriate pointer type |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4539 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4540 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4541 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4542 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4543 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4544 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4545 | |
| 4546 | // Recurse as an -> access. |
| 4547 | IsArrow = true; |
| 4548 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4549 | ObjCImpDecl, HasTemplateArgs); |
| 4550 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4553 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4554 | // because they forgot parentheses to call that function. |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4555 | QualType ZeroArgCallTy; |
| 4556 | UnresolvedSet<4> Overloads; |
| 4557 | if (isExprCallable(*BaseExpr.get(), ZeroArgCallTy, Overloads)) { |
| 4558 | if (ZeroArgCallTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4559 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4560 | << (Overloads.size() > 1) << 0 << BaseExpr.get()->getSourceRange(); |
| 4561 | UnresolvedSet<2> PlausibleOverloads; |
| 4562 | for (OverloadExpr::decls_iterator It = Overloads.begin(), |
| 4563 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 4564 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); |
| 4565 | QualType OverloadResultTy = OverloadDecl->getResultType(); |
| 4566 | if ((!IsArrow && OverloadResultTy->isRecordType()) || |
| 4567 | (IsArrow && OverloadResultTy->isPointerType() && |
| 4568 | OverloadResultTy->getPointeeType()->isRecordType())) |
| 4569 | PlausibleOverloads.addDecl(It.getDecl()); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4570 | } |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4571 | NoteOverloads(PlausibleOverloads, BaseExpr.get()->getExprLoc()); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4572 | return ExprError(); |
| 4573 | } |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4574 | if ((!IsArrow && ZeroArgCallTy->isRecordType()) || |
| 4575 | (IsArrow && ZeroArgCallTy->isPointerType() && |
| 4576 | ZeroArgCallTy->getPointeeType()->isRecordType())) { |
| 4577 | // At this point, we know BaseExpr looks like it's potentially callable |
| 4578 | // with 0 arguments, and that it returns something of a reasonable type, |
| 4579 | // so we can emit a fixit and carry on pretending that BaseExpr was |
| 4580 | // actually a CallExpr. |
| 4581 | SourceLocation ParenInsertionLoc = |
| 4582 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4583 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
| 4584 | << (Overloads.size() > 1) << 1 << BaseExpr.get()->getSourceRange() |
| 4585 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4586 | // FIXME: Try this before emitting the fixit, and suppress diagnostics |
| 4587 | // while doing so. |
| 4588 | ExprResult NewBase = |
| 4589 | ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
| 4590 | MultiExprArg(*this, 0, 0), |
| 4591 | ParenInsertionLoc.getFileLocWithOffset(1)); |
| 4592 | if (NewBase.isInvalid()) |
| 4593 | return ExprError(); |
| 4594 | BaseExpr = NewBase; |
| 4595 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| 4596 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4597 | ObjCImpDecl, HasTemplateArgs); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4598 | } |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4601 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4602 | << BaseType << BaseExpr.get()->getSourceRange(); |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4604 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4607 | /// The main callback when the parser finds something like |
| 4608 | /// expression . [nested-name-specifier] identifier |
| 4609 | /// expression -> [nested-name-specifier] identifier |
| 4610 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4611 | /// possibilities, including destructor and operator references. |
| 4612 | /// |
| 4613 | /// \param OpKind either tok::arrow or tok::period |
| 4614 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4615 | /// is used to diagnose mis-uses of special members that can |
| 4616 | /// only be called |
| 4617 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4618 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4619 | /// aren't properly put in the context chain |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4620 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4621 | SourceLocation OpLoc, |
| 4622 | tok::TokenKind OpKind, |
| 4623 | CXXScopeSpec &SS, |
| 4624 | UnqualifiedId &Id, |
| 4625 | Decl *ObjCImpDecl, |
| 4626 | bool HasTrailingLParen) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4627 | if (SS.isSet() && SS.isInvalid()) |
| 4628 | return ExprError(); |
| 4629 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4630 | // Warn about the explicit constructor calls Microsoft extension. |
| 4631 | if (getLangOptions().Microsoft && |
| 4632 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4633 | Diag(Id.getSourceRange().getBegin(), |
| 4634 | diag::ext_ms_explicit_constructor_call); |
| 4635 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4636 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4637 | |
| 4638 | // Decompose the name into its component parts. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4639 | DeclarationNameInfo NameInfo; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4640 | const TemplateArgumentListInfo *TemplateArgs; |
| 4641 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4642 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4643 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4644 | DeclarationName Name = NameInfo.getName(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4645 | bool IsArrow = (OpKind == tok::arrow); |
| 4646 | |
| 4647 | NamedDecl *FirstQualifierInScope |
| 4648 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4649 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4650 | |
| 4651 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4652 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4653 | if (Result.isInvalid()) return ExprError(); |
| 4654 | Base = Result.take(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4656 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4657 | isDependentScopeSpecifier(SS)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4658 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4659 | IsArrow, OpLoc, |
| 4660 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4661 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4662 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4663 | LookupResult R(*this, NameInfo, LookupMemberName); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4664 | ExprResult BaseResult = Owned(Base); |
| 4665 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4666 | SS, ObjCImpDecl, TemplateArgs != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4667 | if (BaseResult.isInvalid()) |
| 4668 | return ExprError(); |
| 4669 | Base = BaseResult.take(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4670 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4671 | if (Result.isInvalid()) { |
| 4672 | Owned(Base); |
| 4673 | return ExprError(); |
| 4674 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4675 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4676 | if (Result.get()) { |
| 4677 | // The only way a reference to a destructor can be used is to |
| 4678 | // immediately call it, which falls into this case. If the |
| 4679 | // next token is not a '(', produce a diagnostic and build the |
| 4680 | // call now. |
| 4681 | if (!HasTrailingLParen && |
| 4682 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4683 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4684 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4685 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4688 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4689 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4690 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4696 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4697 | FunctionDecl *FD, |
| 4698 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4699 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4700 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4701 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4702 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4704 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4705 | return ExprError(); |
| 4706 | } |
| 4707 | |
| 4708 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4709 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4711 | // Instantiate the expression. |
| 4712 | MultiLevelTemplateArgumentList ArgList |
| 4713 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4714 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4715 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4716 | = ArgList.getInnermost(); |
| 4717 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4718 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4719 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4720 | ExprResult Result; |
| 4721 | { |
| 4722 | // C++ [dcl.fct.default]p5: |
| 4723 | // The names in the [default argument] expression are bound, and |
| 4724 | // the semantic constraints are checked, at the point where the |
| 4725 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4726 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4727 | Result = SubstExpr(UninstExpr, ArgList); |
| 4728 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4729 | if (Result.isInvalid()) |
| 4730 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4732 | // Check the expression as an initializer for the parameter. |
| 4733 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4734 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4735 | InitializationKind Kind |
| 4736 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4737 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4738 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4740 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4741 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4742 | MultiExprArg(*this, &ResultE, 1)); |
| 4743 | if (Result.isInvalid()) |
| 4744 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4746 | // Build the default argument expression. |
| 4747 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4748 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4751 | // If the default expression creates temporaries, we need to |
| 4752 | // push them to the current stack of expression temporaries so they'll |
| 4753 | // be properly destroyed. |
| 4754 | // FIXME: We should really be rebuilding the default argument with new |
| 4755 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4756 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4757 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4758 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4759 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4760 | ExprTemporaries.push_back(Temporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4761 | ExprNeedsCleanups = true; |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4762 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4763 | |
| 4764 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4765 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4766 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4767 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4768 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4771 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4772 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4773 | /// function prototype Proto. Call is the call expression itself, and |
| 4774 | /// Fn is the function expression. For a C++ member function, this |
| 4775 | /// routine does not attempt to convert the object argument. Returns |
| 4776 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4777 | bool |
| 4778 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4779 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4780 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4781 | Expr **Args, unsigned NumArgs, |
| 4782 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4783 | // Bail out early if calling a builtin with custom typechecking. |
| 4784 | // We don't need to do this in the |
| 4785 | if (FDecl) |
| 4786 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4787 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4788 | return false; |
| 4789 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4790 | // 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] | 4791 | // assignment, to the types of the corresponding parameter, ... |
| 4792 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4793 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4795 | // If too few arguments are available (and we don't have default |
| 4796 | // arguments for the remaining parameters), don't make the call. |
| 4797 | if (NumArgs < NumArgsInProto) { |
| 4798 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4799 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4800 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4801 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4802 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | // If too many are passed and not variadic, error on the extras and drop |
| 4806 | // them. |
| 4807 | if (NumArgs > NumArgsInProto) { |
| 4808 | if (!Proto->isVariadic()) { |
| 4809 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4810 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4811 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4812 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4813 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4814 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4815 | |
| 4816 | // Emit the location of the prototype. |
| 4817 | if (FDecl && !FDecl->getBuiltinID()) |
| 4818 | Diag(FDecl->getLocStart(), |
| 4819 | diag::note_typecheck_call_too_many_args) |
| 4820 | << FDecl; |
| 4821 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4822 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4823 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4824 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4825 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4826 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4827 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4828 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4829 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4830 | if (Fn->getType()->isBlockPointerType()) |
| 4831 | CallType = VariadicBlock; // Block |
| 4832 | else if (isa<MemberExpr>(Fn)) |
| 4833 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4834 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4835 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4836 | if (Invalid) |
| 4837 | return true; |
| 4838 | unsigned TotalNumArgs = AllArgs.size(); |
| 4839 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4840 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4841 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4842 | return false; |
| 4843 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4844 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4845 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4846 | FunctionDecl *FDecl, |
| 4847 | const FunctionProtoType *Proto, |
| 4848 | unsigned FirstProtoArg, |
| 4849 | Expr **Args, unsigned NumArgs, |
| 4850 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4851 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4852 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4853 | unsigned NumArgsToCheck = NumArgs; |
| 4854 | bool Invalid = false; |
| 4855 | if (NumArgs != NumArgsInProto) |
| 4856 | // Use default arguments for missing arguments |
| 4857 | NumArgsToCheck = NumArgsInProto; |
| 4858 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4859 | // 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] | 4860 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4861 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4862 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4863 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4864 | if (ArgIx < NumArgs) { |
| 4865 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4866 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4867 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4868 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4869 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4870 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4871 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4873 | // Pass the argument |
| 4874 | ParmVarDecl *Param = 0; |
| 4875 | if (FDecl && i < FDecl->getNumParams()) |
| 4876 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4878 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4879 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4880 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 4881 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4882 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4883 | SourceLocation(), |
| 4884 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4885 | if (ArgE.isInvalid()) |
| 4886 | return true; |
| 4887 | |
| 4888 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4889 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4890 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4891 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4892 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4893 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4894 | if (ArgExpr.isInvalid()) |
| 4895 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4896 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4897 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4898 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4899 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4900 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4901 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4902 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4903 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4904 | |
| 4905 | // Assume that extern "C" functions with variadic arguments that |
| 4906 | // return __unknown_anytype aren't *really* variadic. |
| 4907 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4908 | FDecl && FDecl->isExternC()) { |
| 4909 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4910 | ExprResult arg; |
| 4911 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4912 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4913 | else |
| 4914 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4915 | Invalid |= arg.isInvalid(); |
| 4916 | AllArgs.push_back(arg.take()); |
| 4917 | } |
| 4918 | |
| 4919 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4920 | } else { |
| 4921 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4922 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4923 | Invalid |= Arg.isInvalid(); |
| 4924 | AllArgs.push_back(Arg.take()); |
| 4925 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4926 | } |
| 4927 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4928 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4931 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4932 | /// to have a function type. |
| 4933 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4934 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4935 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4936 | /// This provides the location of the left/right parens and a list of comma |
| 4937 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4938 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4939 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4940 | MultiExprArg args, SourceLocation RParenLoc, |
| 4941 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4942 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4943 | |
| 4944 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4945 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4946 | if (Result.isInvalid()) return ExprError(); |
| 4947 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4949 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4950 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4951 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4952 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4953 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4954 | if (NumArgs > 0) { |
| 4955 | // Pseudo-destructor calls should not have any arguments. |
| 4956 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4957 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4958 | SourceRange(Args[0]->getLocStart(), |
| 4959 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4961 | NumArgs = 0; |
| 4962 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4964 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4965 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4968 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4969 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4970 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4971 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4972 | bool Dependent = false; |
| 4973 | if (Fn->isTypeDependent()) |
| 4974 | Dependent = true; |
| 4975 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4976 | Dependent = true; |
| 4977 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4978 | if (Dependent) { |
| 4979 | if (ExecConfig) { |
| 4980 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4981 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4982 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4983 | } else { |
| 4984 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4985 | Context.DependentTy, VK_RValue, |
| 4986 | RParenLoc)); |
| 4987 | } |
| 4988 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4989 | |
| 4990 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4991 | if (Fn->getType()->isRecordType()) |
| 4992 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4993 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4994 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4995 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4996 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4997 | if (result.isInvalid()) return ExprError(); |
| 4998 | Fn = result.take(); |
| 4999 | } |
| 5000 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5001 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5002 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5003 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5004 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5005 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 5006 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5007 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 5008 | if (Fn->getType() == Context.OverloadTy) { |
| 5009 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 5010 | |
| 5011 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 5012 | if (!find.IsAddressOfOperand) { |
| 5013 | OverloadExpr *ovl = find.Expression; |
| 5014 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 5015 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 5016 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5017 | RParenLoc, ExecConfig); |
| 5018 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5019 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5020 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 5021 | } |
| 5022 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5025 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5026 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 5027 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 5028 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5029 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 5030 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 5031 | if (UnOp->getOpcode() == UO_AddrOf) |
| 5032 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 5033 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5034 | if (isa<DeclRefExpr>(NakedFn)) |
| 5035 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5036 | else if (isa<MemberExpr>(NakedFn)) |
| 5037 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5038 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5039 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 5040 | ExecConfig); |
| 5041 | } |
| 5042 | |
| 5043 | ExprResult |
| 5044 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 5045 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 5046 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 5047 | if (!ConfigDecl) |
| 5048 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 5049 | << "cudaConfigureCall"); |
| 5050 | QualType ConfigQTy = ConfigDecl->getType(); |
| 5051 | |
| 5052 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 5053 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 5054 | |
| 5055 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5058 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 5059 | /// |
| 5060 | /// __builtin_astype( value, dst type ) |
| 5061 | /// |
| 5062 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 5063 | SourceLocation BuiltinLoc, |
| 5064 | SourceLocation RParenLoc) { |
| 5065 | ExprValueKind VK = VK_RValue; |
| 5066 | ExprObjectKind OK = OK_Ordinary; |
| 5067 | QualType DstTy = GetTypeFromParser(destty); |
| 5068 | QualType SrcTy = expr->getType(); |
| 5069 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 5070 | return ExprError(Diag(BuiltinLoc, |
| 5071 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 5072 | << DstTy |
| 5073 | << SrcTy |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5074 | << expr->getSourceRange()); |
| 5075 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, RParenLoc)); |
| 5076 | } |
| 5077 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5078 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 5079 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5080 | /// unary-convert to an expression of function-pointer or |
| 5081 | /// block-pointer type. |
| 5082 | /// |
| 5083 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5084 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5085 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 5086 | SourceLocation LParenLoc, |
| 5087 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5088 | SourceLocation RParenLoc, |
| 5089 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5090 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 5091 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5092 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5093 | ExprResult Result = UsualUnaryConversions(Fn); |
| 5094 | if (Result.isInvalid()) |
| 5095 | return ExprError(); |
| 5096 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5097 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5098 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 5099 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5100 | CallExpr *TheCall; |
| 5101 | if (Config) { |
| 5102 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 5103 | cast<CallExpr>(Config), |
| 5104 | Args, NumArgs, |
| 5105 | Context.BoolTy, |
| 5106 | VK_RValue, |
| 5107 | RParenLoc); |
| 5108 | } else { |
| 5109 | TheCall = new (Context) CallExpr(Context, Fn, |
| 5110 | Args, NumArgs, |
| 5111 | Context.BoolTy, |
| 5112 | VK_RValue, |
| 5113 | RParenLoc); |
| 5114 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5115 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5116 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 5117 | |
| 5118 | // Bail out early if calling a builtin with custom typechecking. |
| 5119 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 5120 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 5121 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5122 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5123 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5124 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5125 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 5126 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5127 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5128 | if (FuncT == 0) |
| 5129 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5130 | << Fn->getType() << Fn->getSourceRange()); |
| 5131 | } else if (const BlockPointerType *BPT = |
| 5132 | Fn->getType()->getAs<BlockPointerType>()) { |
| 5133 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 5134 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5135 | // Handle calls to expressions of unknown-any type. |
| 5136 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 5137 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5138 | if (rewrite.isInvalid()) return ExprError(); |
| 5139 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 5140 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5141 | goto retry; |
| 5142 | } |
| 5143 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5144 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5145 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5146 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5147 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 5148 | if (getLangOptions().CUDA) { |
| 5149 | if (Config) { |
| 5150 | // CUDA: Kernel calls must be to global functions |
| 5151 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 5152 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 5153 | << FDecl->getName() << Fn->getSourceRange()); |
| 5154 | |
| 5155 | // CUDA: Kernel function must have 'void' return type |
| 5156 | if (!FuncT->getResultType()->isVoidType()) |
| 5157 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 5158 | << Fn->getType() << Fn->getSourceRange()); |
| 5159 | } |
| 5160 | } |
| 5161 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5162 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5163 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5164 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5165 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5166 | return ExprError(); |
| 5167 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5168 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5169 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5170 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5172 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5173 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5174 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5175 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5176 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5177 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5178 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5179 | if (FDecl) { |
| 5180 | // Check if we have too few/too many template arguments, based |
| 5181 | // on our knowledge of the function definition. |
| 5182 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 5183 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5184 | const FunctionProtoType *Proto |
| 5185 | = Def->getType()->getAs<FunctionProtoType>(); |
| 5186 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5187 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 5188 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5189 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5190 | |
| 5191 | // If the function we're calling isn't a function prototype, but we have |
| 5192 | // a function prototype from a prior declaratiom, use that prototype. |
| 5193 | if (!FDecl->hasPrototype()) |
| 5194 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5197 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5198 | for (unsigned i = 0; i != NumArgs; i++) { |
| 5199 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5200 | |
| 5201 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5202 | InitializedEntity Entity |
| 5203 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5204 | Proto->getArgType(i), |
| 5205 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5206 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 5207 | SourceLocation(), |
| 5208 | Owned(Arg)); |
| 5209 | if (ArgE.isInvalid()) |
| 5210 | return true; |
| 5211 | |
| 5212 | Arg = ArgE.takeAs<Expr>(); |
| 5213 | |
| 5214 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5215 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 5216 | |
| 5217 | if (ArgE.isInvalid()) |
| 5218 | return true; |
| 5219 | |
| 5220 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 5223 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 5224 | Arg->getType(), |
| 5225 | PDiag(diag::err_call_incomplete_argument) |
| 5226 | << Arg->getSourceRange())) |
| 5227 | return ExprError(); |
| 5228 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5229 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5230 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5231 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5232 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5233 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 5234 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5235 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 5236 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5237 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 5238 | // Check for sentinels |
| 5239 | if (NDecl) |
| 5240 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5242 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5243 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5244 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5245 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5246 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5247 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 5248 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5249 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5250 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5251 | return ExprError(); |
| 5252 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5253 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5254 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5257 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5258 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5259 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5260 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 5261 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5262 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5263 | |
| 5264 | TypeSourceInfo *TInfo; |
| 5265 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 5266 | if (!TInfo) |
| 5267 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 5268 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5269 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5272 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5273 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5274 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5275 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 5276 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5277 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 5278 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 5279 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 5280 | << SourceRange(LParenLoc, |
| 5281 | literalExpr->getSourceRange().getEnd()))) |
| 5282 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5283 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5284 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 5285 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 5286 | } else if (!literalType->isDependentType() && |
| 5287 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5288 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5290 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5291 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5293 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5294 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5295 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5296 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 5297 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5298 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5299 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5300 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5301 | &literalType); |
| 5302 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5303 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5304 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5305 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5306 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5307 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5308 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5309 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5310 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5311 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5312 | // In C, compound literals are l-values for some reason. |
| 5313 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5314 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 5315 | return MaybeBindToTemporary( |
| 5316 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| 5317 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5320 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5321 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5322 | SourceLocation RBraceLoc) { |
| 5323 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5324 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5325 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5326 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5327 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5328 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5329 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5330 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5331 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5332 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5335 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5336 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5337 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5338 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5339 | // Also, callers should have filtered out the invalid cases with |
| 5340 | // pointers. Everything else should be possible. |
| 5341 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5342 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5343 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5344 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5345 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5346 | switch (SrcTy->getScalarTypeKind()) { |
| 5347 | case Type::STK_MemberPointer: |
| 5348 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5349 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5350 | case Type::STK_Pointer: |
| 5351 | switch (DestTy->getScalarTypeKind()) { |
| 5352 | case Type::STK_Pointer: |
| 5353 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5354 | CK_AnyPointerToObjCPointerCast : |
| 5355 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5356 | case Type::STK_Bool: |
| 5357 | return CK_PointerToBoolean; |
| 5358 | case Type::STK_Integral: |
| 5359 | return CK_PointerToIntegral; |
| 5360 | case Type::STK_Floating: |
| 5361 | case Type::STK_FloatingComplex: |
| 5362 | case Type::STK_IntegralComplex: |
| 5363 | case Type::STK_MemberPointer: |
| 5364 | llvm_unreachable("illegal cast from pointer"); |
| 5365 | } |
| 5366 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5367 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5368 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5369 | case Type::STK_Integral: |
| 5370 | switch (DestTy->getScalarTypeKind()) { |
| 5371 | case Type::STK_Pointer: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5372 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5373 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5374 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5375 | case Type::STK_Bool: |
| 5376 | return CK_IntegralToBoolean; |
| 5377 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5378 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5379 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5380 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5381 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5382 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5383 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5384 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5385 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5386 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5387 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5388 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5389 | case Type::STK_MemberPointer: |
| 5390 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5391 | } |
| 5392 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5393 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5394 | case Type::STK_Floating: |
| 5395 | switch (DestTy->getScalarTypeKind()) { |
| 5396 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5397 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | case Type::STK_Bool: |
| 5399 | return CK_FloatingToBoolean; |
| 5400 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5401 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5402 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5403 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5404 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5405 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5406 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5407 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5408 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5409 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5410 | case Type::STK_Pointer: |
| 5411 | llvm_unreachable("valid float->pointer cast?"); |
| 5412 | case Type::STK_MemberPointer: |
| 5413 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5414 | } |
| 5415 | break; |
| 5416 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5417 | case Type::STK_FloatingComplex: |
| 5418 | switch (DestTy->getScalarTypeKind()) { |
| 5419 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5420 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5421 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5422 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5423 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5424 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5425 | if (S.Context.hasSameType(ET, DestTy)) |
| 5426 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5427 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5428 | return CK_FloatingCast; |
| 5429 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5430 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5431 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5432 | case Type::STK_Integral: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5433 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5434 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5435 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5436 | case Type::STK_Pointer: |
| 5437 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5438 | case Type::STK_MemberPointer: |
| 5439 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5440 | } |
| 5441 | break; |
| 5442 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5443 | case Type::STK_IntegralComplex: |
| 5444 | switch (DestTy->getScalarTypeKind()) { |
| 5445 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5446 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5447 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5448 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5449 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5450 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5451 | if (S.Context.hasSameType(ET, DestTy)) |
| 5452 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5453 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5454 | return CK_IntegralCast; |
| 5455 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5456 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5457 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5458 | case Type::STK_Floating: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5459 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5460 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5461 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5462 | case Type::STK_Pointer: |
| 5463 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5464 | case Type::STK_MemberPointer: |
| 5465 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5466 | } |
| 5467 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5468 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5469 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5470 | llvm_unreachable("Unhandled scalar cast"); |
| 5471 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5474 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5475 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 5476 | QualType castType, Expr *castExpr, |
| 5477 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5478 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5479 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5480 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5481 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5482 | if (getLangOptions().CPlusPlus) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5483 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5484 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5485 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5486 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5487 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5488 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5489 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5490 | // We only support r-value casts in C. |
| 5491 | VK = VK_RValue; |
| 5492 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5493 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5494 | // type needs to be scalar. |
| 5495 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5496 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5497 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5498 | if (castExprRes.isInvalid()) |
| 5499 | return ExprError(); |
| 5500 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5501 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5502 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5503 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5504 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5505 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5506 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5507 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5508 | if (castExprRes.isInvalid()) |
| 5509 | return ExprError(); |
| 5510 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5511 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5512 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5513 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5514 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5515 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5516 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5517 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5518 | (castType->isStructureType() || castType->isUnionType())) { |
| 5519 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5520 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5521 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5522 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5523 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5524 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5525 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5526 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5527 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5528 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5529 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5530 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5531 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5532 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5533 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5534 | castExpr->getType()) && |
| 5535 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5536 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5537 | << castExpr->getSourceRange(); |
| 5538 | break; |
| 5539 | } |
| 5540 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5541 | if (Field == FieldEnd) { |
| 5542 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5543 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5544 | return ExprError(); |
| 5545 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5546 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5547 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5548 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5549 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5550 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5551 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5552 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5553 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5554 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5555 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5556 | // The type we're casting to is known to be a scalar or vector. |
| 5557 | |
| 5558 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5559 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5560 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5561 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5562 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5563 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5564 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5565 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5566 | |
| 5567 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5568 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5569 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5570 | if (castType->isVectorType()) { |
| 5571 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5572 | VectorType::AltiVecVector && |
| 5573 | (castExpr->getType()->isIntegerType() || |
| 5574 | castExpr->getType()->isFloatingType())) { |
| 5575 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5576 | return Owned(castExpr); |
| 5577 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5578 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5579 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5580 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5581 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5582 | if (castExpr->getType()->isVectorType()) { |
| 5583 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5584 | return ExprError(); |
| 5585 | else |
| 5586 | return Owned(castExpr); |
| 5587 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5588 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5589 | // The source and target types are both scalars, i.e. |
| 5590 | // - arithmetic types (fundamental, enum, and complex) |
| 5591 | // - all kinds of pointers |
| 5592 | // Note that member pointers were filtered out with C++, above. |
| 5593 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5595 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5596 | return ExprError(); |
| 5597 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5598 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5599 | // If either type is a pointer, the other type has to be either an |
| 5600 | // integer or a pointer. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5601 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5602 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5603 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5604 | castExprType->isArithmeticType()) { |
| 5605 | Diag(castExpr->getLocStart(), |
| 5606 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5607 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5608 | return ExprError(); |
| 5609 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5610 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5611 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5612 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5613 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5614 | return ExprError(); |
| 5615 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5616 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5617 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5618 | if (getLangOptions().ObjCAutoRefCount) { |
| 5619 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 5620 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 5621 | castType, castExpr, CCK_CStyleCast); |
| 5622 | |
| 5623 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 5624 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 5625 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 5626 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 5627 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 5628 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 5629 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 5630 | Diag(castExpr->getLocStart(), |
| 5631 | diag::err_typecheck_incompatible_lifetime) |
| 5632 | << castExprType << castType << AA_Casting |
| 5633 | << castExpr->getSourceRange(); |
| 5634 | |
| 5635 | return ExprError(); |
| 5636 | } |
| 5637 | } |
| 5638 | } |
| 5639 | } |
| 5640 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | castExprRes = Owned(castExpr); |
| 5642 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5643 | if (castExprRes.isInvalid()) |
| 5644 | return ExprError(); |
| 5645 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5646 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5647 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5648 | CheckCastAlign(castExpr, castType, TyR); |
| 5649 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5650 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5653 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5654 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5655 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5656 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5657 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5658 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5659 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5660 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5661 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5662 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5663 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5664 | } else |
| 5665 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5666 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5667 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5668 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5669 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5670 | return false; |
| 5671 | } |
| 5672 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5673 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5674 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5675 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5676 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5677 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5678 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5679 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5680 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5681 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5682 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5683 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5684 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5685 | return ExprError(); |
| 5686 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5687 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5688 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5691 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5692 | // conversion will take place first from scalar to elt type, and then |
| 5693 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5694 | if (SrcTy->isPointerType()) |
| 5695 | return Diag(R.getBegin(), |
| 5696 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5697 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5698 | |
| 5699 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5700 | ExprResult CastExprRes = Owned(CastExpr); |
| 5701 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5702 | if (CastExprRes.isInvalid()) |
| 5703 | return ExprError(); |
| 5704 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5705 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5706 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5707 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5710 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5711 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5712 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5713 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5714 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5715 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5716 | TypeSourceInfo *castTInfo; |
| 5717 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5718 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5719 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5720 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5721 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5722 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5723 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5724 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5725 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5726 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5727 | } |
| 5728 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5729 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5730 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5731 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5732 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5733 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5734 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5735 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5736 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 5737 | castExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5738 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5739 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5740 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5741 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5742 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5743 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5744 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5745 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5748 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5749 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5750 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5751 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5752 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5753 | if (!E) |
| 5754 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5755 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5756 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5757 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5758 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5759 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5760 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5761 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5762 | if (Result.isInvalid()) return ExprError(); |
| 5763 | |
| 5764 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5767 | ExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5768 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5769 | SourceLocation RParenLoc, Expr *Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5770 | TypeSourceInfo *TInfo) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5771 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5772 | QualType Ty = TInfo->getType(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5773 | bool isVectorLiteral = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5774 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5775 | // Check for an altivec or OpenCL literal, |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5776 | // i.e. all the elements are integer constants. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5777 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5778 | if (PE->getNumExprs() == 0) { |
| 5779 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5780 | return ExprError(); |
| 5781 | } |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5782 | if (PE->getNumExprs() == 1) { |
| 5783 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5784 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5785 | } |
| 5786 | else |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5787 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5788 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5789 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5790 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5791 | // then handle it as such. |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5792 | if (isVectorLiteral) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5793 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5794 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5795 | // initializers must be one or must match the size of the vector. |
| 5796 | // If a single value is specified in the initializer then it will be |
| 5797 | // replicated to all the components of the vector |
| 5798 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5799 | VectorType::AltiVecVector) { |
| 5800 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5801 | // The number of initializers must be one or must match the size of the |
| 5802 | // vector. If a single value is specified in the initializer then it will |
| 5803 | // be replicated to all the components of the vector |
| 5804 | if (PE->getNumExprs() == 1) { |
| 5805 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5806 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5807 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5808 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5809 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5810 | } |
| 5811 | else if (PE->getNumExprs() < numElems) { |
| 5812 | Diag(PE->getExprLoc(), |
| 5813 | diag::err_incorrect_number_of_vector_initializers); |
| 5814 | return ExprError(); |
| 5815 | } |
| 5816 | else |
| 5817 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5818 | initExprs.push_back(PE->getExpr(i)); |
| 5819 | } |
| 5820 | else |
| 5821 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5822 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5823 | |
| 5824 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5825 | // braces instead of the original commas. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5826 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5827 | &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5828 | initExprs.size(), RParenLoc); |
| 5829 | E->setType(Ty); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5830 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5831 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5832 | // 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] | 5833 | // sequence of BinOp comma operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5834 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5835 | if (Result.isInvalid()) return ExprError(); |
| 5836 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5837 | } |
| 5838 | } |
| 5839 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5840 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5841 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5842 | MultiExprArg Val, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5843 | ParsedType TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5844 | unsigned nexprs = Val.size(); |
| 5845 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5846 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5847 | Expr *expr; |
| 5848 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5849 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5850 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5851 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 5852 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5853 | return Owned(expr); |
| 5854 | } |
| 5855 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5856 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5857 | /// constant and the other is not a pointer. |
| 5858 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5859 | SourceLocation QuestionLoc) { |
| 5860 | Expr *NullExpr = LHS; |
| 5861 | Expr *NonPointerExpr = RHS; |
| 5862 | Expr::NullPointerConstantKind NullKind = |
| 5863 | NullExpr->isNullPointerConstant(Context, |
| 5864 | Expr::NPC_ValueDependentIsNotNull); |
| 5865 | |
| 5866 | if (NullKind == Expr::NPCK_NotNull) { |
| 5867 | NullExpr = RHS; |
| 5868 | NonPointerExpr = LHS; |
| 5869 | NullKind = |
| 5870 | NullExpr->isNullPointerConstant(Context, |
| 5871 | Expr::NPC_ValueDependentIsNotNull); |
| 5872 | } |
| 5873 | |
| 5874 | if (NullKind == Expr::NPCK_NotNull) |
| 5875 | return false; |
| 5876 | |
| 5877 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5878 | // In this case, check to make sure that we got here from a "NULL" |
| 5879 | // string in the source code. |
| 5880 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5881 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5882 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5883 | return false; |
| 5884 | } |
| 5885 | |
| 5886 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5887 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5888 | << NonPointerExpr->getType() << DiagType |
| 5889 | << NonPointerExpr->getSourceRange(); |
| 5890 | return true; |
| 5891 | } |
| 5892 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5893 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5894 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5895 | /// C99 6.5.15 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5896 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5897 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5898 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5899 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5900 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5901 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5902 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5903 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5904 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5905 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5906 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5907 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5908 | // C++ is sufficiently different to merit its own checker. |
| 5909 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5910 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5911 | |
| 5912 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5913 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5914 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5915 | Cond = UsualUnaryConversions(Cond.take()); |
| 5916 | if (Cond.isInvalid()) |
| 5917 | return QualType(); |
| 5918 | LHS = UsualUnaryConversions(LHS.take()); |
| 5919 | if (LHS.isInvalid()) |
| 5920 | return QualType(); |
| 5921 | RHS = UsualUnaryConversions(RHS.take()); |
| 5922 | if (RHS.isInvalid()) |
| 5923 | return QualType(); |
| 5924 | |
| 5925 | QualType CondTy = Cond.get()->getType(); |
| 5926 | QualType LHSTy = LHS.get()->getType(); |
| 5927 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5928 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5929 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5930 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5931 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5932 | // Throw an error if its not either. |
| 5933 | if (getLangOptions().OpenCL) { |
| 5934 | if (!CondTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5935 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5936 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5937 | << CondTy; |
| 5938 | return QualType(); |
| 5939 | } |
| 5940 | } |
| 5941 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5942 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5943 | << CondTy; |
| 5944 | return QualType(); |
| 5945 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5946 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5947 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5948 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5949 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5950 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5951 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5952 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5953 | // attempt to implicity convert them to the vector type to act like the |
| 5954 | // built in select. |
| 5955 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5956 | // Both operands should be of scalar type. |
| 5957 | if (!LHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5958 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5959 | << CondTy; |
| 5960 | return QualType(); |
| 5961 | } |
| 5962 | if (!RHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5963 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5964 | << CondTy; |
| 5965 | return QualType(); |
| 5966 | } |
| 5967 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5968 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5969 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5972 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5973 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5974 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5975 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5976 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5977 | return QualType(); |
| 5978 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5979 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5980 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5981 | // If both operands are the same structure or union type, the result is that |
| 5982 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5983 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5984 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5985 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5986 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5987 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5988 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5989 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5990 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5991 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5992 | // 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] | 5993 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5994 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5995 | if (!LHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5996 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5997 | << RHS.get()->getSourceRange(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5998 | if (!RHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5999 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 6000 | << LHS.get()->getSourceRange(); |
| 6001 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 6002 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 6003 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 6004 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 6005 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 6006 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6007 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6008 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6009 | // promote the null to a pointer. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6010 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6011 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 6012 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6013 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6014 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 6015 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6016 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 6017 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6018 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6019 | // All objective-c pointer type analysis is done here. |
| 6020 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 6021 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6022 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 6023 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6024 | if (!compositeType.isNull()) |
| 6025 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6026 | |
| 6027 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6028 | // Handle block pointer types. |
| 6029 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 6030 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 6031 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 6032 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6033 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 6034 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6035 | return destType; |
| 6036 | } |
| 6037 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6038 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6039 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6040 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6041 | // We have 2 block pointer types. |
| 6042 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6043 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6044 | return LHSTy; |
| 6045 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6046 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6047 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 6048 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6049 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6050 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 6051 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6052 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6053 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6054 | // In this situation, we assume void* type. No especially good |
| 6055 | // reason, but this is what gcc does, and we do have to pick |
| 6056 | // to get a consistent AST. |
| 6057 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6058 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6059 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6060 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6061 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6062 | // The block pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6063 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6064 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 6065 | return LHSTy; |
| 6066 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6067 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6068 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 6069 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 6070 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6071 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6072 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6073 | |
| 6074 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 6075 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 6076 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6077 | QualType destPointee |
| 6078 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6079 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6080 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6081 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6082 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6083 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6084 | return destType; |
| 6085 | } |
| 6086 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6087 | QualType destPointee |
| 6088 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6089 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6090 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6091 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6092 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6093 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6094 | return destType; |
| 6095 | } |
| 6096 | |
| 6097 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6098 | // Two identical pointer types are always compatible. |
| 6099 | return LHSTy; |
| 6100 | } |
| 6101 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 6102 | rhptee.getUnqualifiedType())) { |
| 6103 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6104 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6105 | // In this situation, we assume void* type. No especially good |
| 6106 | // reason, but this is what gcc does, and we do have to pick |
| 6107 | // to get a consistent AST. |
| 6108 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6109 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6110 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6111 | return incompatTy; |
| 6112 | } |
| 6113 | // The pointer types are compatible. |
| 6114 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 6115 | // differently qualified versions of compatible types, the result type is |
| 6116 | // a pointer to an appropriately qualified version of the *composite* |
| 6117 | // type. |
| 6118 | // FIXME: Need to calculate the composite type. |
| 6119 | // FIXME: Need to add qualifiers |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6120 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6121 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6122 | return LHSTy; |
| 6123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6124 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6125 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 6126 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6127 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 6128 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6129 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6130 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6131 | return RHSTy; |
| 6132 | } |
| 6133 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 6134 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6135 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6136 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6137 | return LHSTy; |
| 6138 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 6139 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6140 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 6141 | // constant and the other is not a pointer type. In this case, the user most |
| 6142 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6143 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6144 | return QualType(); |
| 6145 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 6146 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6147 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6148 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6149 | return QualType(); |
| 6150 | } |
| 6151 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6152 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 6153 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6154 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6155 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6156 | QualType LHSTy = LHS.get()->getType(); |
| 6157 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6158 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6159 | // Handle things like Class and struct objc_class*. Here we case the result |
| 6160 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 6161 | // redefinition type if an attempt is made to access its fields. |
| 6162 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6163 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6164 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6165 | return LHSTy; |
| 6166 | } |
| 6167 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6168 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6169 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6170 | return RHSTy; |
| 6171 | } |
| 6172 | // And the same for struct objc_object* / id |
| 6173 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6174 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6175 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6176 | return LHSTy; |
| 6177 | } |
| 6178 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6179 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6180 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6181 | return RHSTy; |
| 6182 | } |
| 6183 | // And the same for struct objc_selector* / SEL |
| 6184 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6185 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6186 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6187 | return LHSTy; |
| 6188 | } |
| 6189 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6190 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6191 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6192 | return RHSTy; |
| 6193 | } |
| 6194 | // Check constraints for Objective-C object pointers types. |
| 6195 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6196 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6197 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6198 | // Two identical object pointer types are always compatible. |
| 6199 | return LHSTy; |
| 6200 | } |
| 6201 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 6202 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 6203 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6204 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6205 | // If both operands are interfaces and either operand can be |
| 6206 | // assigned to the other, use that type as the composite |
| 6207 | // type. This allows |
| 6208 | // xxx ? (A*) a : (B*) b |
| 6209 | // where B is a subclass of A. |
| 6210 | // |
| 6211 | // Additionally, as for assignment, if either type is 'id' |
| 6212 | // allow silent coercion. Finally, if the types are |
| 6213 | // incompatible then make sure to use 'id' as the composite |
| 6214 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6215 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6216 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 6217 | // It could return the composite type. |
| 6218 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 6219 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 6220 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 6221 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 6222 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 6223 | RHSTy->isObjCQualifiedIdType()) && |
| 6224 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 6225 | // Need to handle "id<xx>" explicitly. |
| 6226 | // GCC allows qualified id and any Objective-C type to devolve to |
| 6227 | // id. Currently localizing to here until clear this should be |
| 6228 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 6229 | compositeType = Context.getObjCIdType(); |
| 6230 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 6231 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6232 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6233 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 6234 | ; |
| 6235 | else { |
| 6236 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 6237 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6238 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6239 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6240 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6241 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6242 | return incompatTy; |
| 6243 | } |
| 6244 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6245 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 6246 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6247 | return compositeType; |
| 6248 | } |
| 6249 | // Check Objective-C object pointer types and 'void *' |
| 6250 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 6251 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6252 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6253 | QualType destPointee |
| 6254 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 6255 | QualType destType = Context.getPointerType(destPointee); |
| 6256 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6257 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6258 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6259 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6260 | return destType; |
| 6261 | } |
| 6262 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 6263 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6264 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 6265 | QualType destPointee |
| 6266 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 6267 | QualType destType = Context.getPointerType(destPointee); |
| 6268 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6269 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6270 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6271 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6272 | return destType; |
| 6273 | } |
| 6274 | return QualType(); |
| 6275 | } |
| 6276 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6277 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6278 | /// ParenRange in parentheses. |
| 6279 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6280 | const PartialDiagnostic &Note, |
| 6281 | SourceRange ParenRange) { |
| 6282 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6283 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 6284 | EndLoc.isValid()) { |
| 6285 | Self.Diag(Loc, Note) |
| 6286 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6287 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6288 | } else { |
| 6289 | // We can't display the parentheses, so just show the bare note. |
| 6290 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6291 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 6295 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 6296 | } |
| 6297 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6298 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 6299 | /// expression, either using a built-in or overloaded operator, |
| 6300 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 6301 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 6302 | Expr **RHS) { |
| 6303 | E = E->IgnoreParenImpCasts(); |
| 6304 | E = E->IgnoreConversionOperator(); |
| 6305 | E = E->IgnoreParenImpCasts(); |
| 6306 | |
| 6307 | // Built-in binary operator. |
| 6308 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 6309 | if (IsArithmeticOp(OP->getOpcode())) { |
| 6310 | *Opcode = OP->getOpcode(); |
| 6311 | *RHS = OP->getRHS(); |
| 6312 | return true; |
| 6313 | } |
| 6314 | } |
| 6315 | |
| 6316 | // Overloaded operator. |
| 6317 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 6318 | if (Call->getNumArgs() != 2) |
| 6319 | return false; |
| 6320 | |
| 6321 | // Make sure this is really a binary operator that is safe to pass into |
| 6322 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 6323 | OverloadedOperatorKind OO = Call->getOperator(); |
| 6324 | if (OO < OO_Plus || OO > OO_Arrow) |
| 6325 | return false; |
| 6326 | |
| 6327 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 6328 | if (IsArithmeticOp(OpKind)) { |
| 6329 | *Opcode = OpKind; |
| 6330 | *RHS = Call->getArg(1); |
| 6331 | return true; |
| 6332 | } |
| 6333 | } |
| 6334 | |
| 6335 | return false; |
| 6336 | } |
| 6337 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6338 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 6339 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 6340 | } |
| 6341 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6342 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 6343 | /// or is a logical expression such as (x==y) which has int type, but is |
| 6344 | /// commonly interpreted as boolean. |
| 6345 | static bool ExprLooksBoolean(Expr *E) { |
| 6346 | E = E->IgnoreParenImpCasts(); |
| 6347 | |
| 6348 | if (E->getType()->isBooleanType()) |
| 6349 | return true; |
| 6350 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 6351 | return IsLogicOp(OP->getOpcode()); |
| 6352 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 6353 | return OP->getOpcode() == UO_LNot; |
| 6354 | |
| 6355 | return false; |
| 6356 | } |
| 6357 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6358 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 6359 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 6360 | /// the conditional operator has higher precedence, for example: |
| 6361 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 6362 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 6363 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6364 | Expr *Condition, |
| 6365 | Expr *LHS, |
| 6366 | Expr *RHS) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6367 | BinaryOperatorKind CondOpcode; |
| 6368 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6369 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6370 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6371 | return; |
| 6372 | if (!ExprLooksBoolean(CondRHS)) |
| 6373 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6374 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6375 | // The condition is an arithmetic binary expression, with a right- |
| 6376 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6377 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6378 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6379 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6380 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6381 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6382 | SuggestParentheses(Self, OpLoc, |
| 6383 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 6384 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 6385 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 6386 | |
| 6387 | SuggestParentheses(Self, OpLoc, |
| 6388 | Self.PDiag(diag::note_precedence_conditional_first), |
| 6389 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6392 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6393 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6394 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6395 | SourceLocation ColonLoc, |
| 6396 | Expr *CondExpr, Expr *LHSExpr, |
| 6397 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 6398 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 6399 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6400 | OpaqueValueExpr *opaqueValue = 0; |
| 6401 | Expr *commonExpr = 0; |
| 6402 | if (LHSExpr == 0) { |
| 6403 | commonExpr = CondExpr; |
| 6404 | |
| 6405 | // We usually want to apply unary conversions *before* saving, except |
| 6406 | // in the special case of a C++ l-value conditional. |
| 6407 | if (!(getLangOptions().CPlusPlus |
| 6408 | && !commonExpr->isTypeDependent() |
| 6409 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 6410 | && commonExpr->isGLValue() |
| 6411 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 6412 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 6413 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6414 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 6415 | if (commonRes.isInvalid()) |
| 6416 | return ExprError(); |
| 6417 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 6421 | commonExpr->getType(), |
| 6422 | commonExpr->getValueKind(), |
| 6423 | commonExpr->getObjectKind()); |
| 6424 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 6425 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6426 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6427 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6428 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6429 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 6430 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6431 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6432 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 6433 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6434 | return ExprError(); |
| 6435 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6436 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 6437 | RHS.get()); |
| 6438 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6439 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6440 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 6441 | LHS.take(), ColonLoc, |
| 6442 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6443 | |
| 6444 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6445 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 6446 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6449 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6450 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6451 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6452 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6453 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6454 | static Sema::AssignConvertType |
| 6455 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6456 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6457 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6458 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6459 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6460 | const Type *lhptee, *rhptee; |
| 6461 | Qualifiers lhq, rhq; |
| 6462 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6463 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6464 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6465 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6466 | |
| 6467 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6468 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6469 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6470 | Qualifiers lq; |
| 6471 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6472 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 6473 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 6474 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 6475 | // Ignore lifetime for further calculation. |
| 6476 | lhq.removeObjCLifetime(); |
| 6477 | rhq.removeObjCLifetime(); |
| 6478 | } |
| 6479 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6480 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6481 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6482 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6483 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6484 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6485 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6486 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6487 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 6488 | .compatiblyIncludes( |
| 6489 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6490 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6491 | ; // keep old |
| 6492 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6493 | // Treat lifetime mismatches as fatal. |
| 6494 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 6495 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6496 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6497 | // For GCC compatibility, other qualifier mismatches are treated |
| 6498 | // as still compatible in C. |
| 6499 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6500 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6501 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6502 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6503 | // 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] | 6504 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6505 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6506 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6507 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6508 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6509 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6510 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6511 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6512 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6513 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6514 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6515 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6516 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6517 | |
| 6518 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6519 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6520 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6521 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6522 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6523 | // 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] | 6524 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6525 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6526 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6527 | // Check if the pointee types are compatible ignoring the sign. |
| 6528 | // We explicitly check for char so that we catch "char" vs |
| 6529 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6530 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6531 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6532 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6533 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6534 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6535 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6536 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6537 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6538 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6539 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6540 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6541 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6542 | // takes priority over sign incompatibility because the sign |
| 6543 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6544 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6545 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6546 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6547 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6548 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6549 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6550 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6551 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6552 | // the eventual target type is the same and the pointers have the same |
| 6553 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6554 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6555 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6556 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6557 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6558 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6559 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6560 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6561 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6562 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6563 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6564 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6565 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6566 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6567 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6570 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6571 | /// block pointer types are compatible or whether a block and normal pointer |
| 6572 | /// are compatible. It is more restrict than comparing two function pointer |
| 6573 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6574 | static Sema::AssignConvertType |
| 6575 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6576 | QualType rhsType) { |
| 6577 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6578 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6579 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6580 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6581 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6582 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6583 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6584 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6585 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6586 | // In C++, the types have to match exactly. |
| 6587 | if (S.getLangOptions().CPlusPlus) |
| 6588 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6589 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6590 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6591 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6592 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6593 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6594 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6595 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6596 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6597 | return Sema::IncompatibleBlockPointer; |
| 6598 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6599 | return ConvTy; |
| 6600 | } |
| 6601 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6602 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6603 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6604 | static Sema::AssignConvertType |
| 6605 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6606 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6607 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6608 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6609 | if (lhsType->isObjCBuiltinType()) { |
| 6610 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6611 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6612 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6613 | return Sema::IncompatiblePointer; |
| 6614 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6615 | } |
| 6616 | if (rhsType->isObjCBuiltinType()) { |
| 6617 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6618 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6619 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6620 | return Sema::IncompatiblePointer; |
| 6621 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6622 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6623 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6624 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6625 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6626 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6627 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6628 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6629 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6630 | |
| 6631 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6632 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6633 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6634 | return Sema::IncompatibleObjCQualifiedId; |
| 6635 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6638 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6639 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6640 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6641 | // Fake up an opaque expression. We don't actually care about what |
| 6642 | // cast operations are required, so if CheckAssignmentConstraints |
| 6643 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6644 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6645 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6646 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6647 | CastKind K = CK_Invalid; |
| 6648 | |
| 6649 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6650 | } |
| 6651 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6652 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6653 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6654 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6655 | /// |
| 6656 | /// int a, *pint; |
| 6657 | /// short *pshort; |
| 6658 | /// struct foo *pfoo; |
| 6659 | /// |
| 6660 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6661 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6662 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6663 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6664 | /// |
| 6665 | /// 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] | 6666 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6667 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6668 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6669 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6670 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6671 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6672 | QualType rhsType = rhs.get()->getType(); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6673 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6674 | // Get canonical types. We're not formatting these types, just comparing |
| 6675 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6676 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6677 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6678 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6679 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6680 | if (lhsType == rhsType) { |
| 6681 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6682 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6685 | // If the left-hand side is a reference type, then we are in a |
| 6686 | // (rare!) case where we've allowed the use of references in C, |
| 6687 | // e.g., as a parameter type in a built-in function. In this case, |
| 6688 | // just make sure that the type referenced is compatible with the |
| 6689 | // right-hand side type. The caller is responsible for adjusting |
| 6690 | // lhsType so that the resulting expression does not have reference |
| 6691 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6692 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6693 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6694 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6695 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6696 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6697 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6698 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6699 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6700 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6701 | // to the same ExtVector type. |
| 6702 | if (lhsType->isExtVectorType()) { |
| 6703 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6704 | return Incompatible; |
| 6705 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6706 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6707 | // element type. |
| 6708 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6709 | if (elType != rhsType) { |
| 6710 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6711 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6712 | } |
| 6713 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6714 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6715 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6717 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6718 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6719 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6720 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6721 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6722 | // vector type and vice versa |
| 6723 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6724 | Kind = CK_BitCast; |
| 6725 | return Compatible; |
| 6726 | } |
| 6727 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6728 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6729 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6730 | // no bits are changed but the result type is different. |
| 6731 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6732 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6733 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6734 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6735 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6736 | } |
| 6737 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6738 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6739 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6740 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6741 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6742 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6743 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6744 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6745 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6746 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6747 | // Conversions to normal pointers. |
| 6748 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6749 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6750 | if (isa<PointerType>(rhsType)) { |
| 6751 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6752 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6753 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6754 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6755 | // int -> T* |
| 6756 | if (rhsType->isIntegerType()) { |
| 6757 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6758 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6759 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6760 | |
| 6761 | // C pointers are not compatible with ObjC object pointers, |
| 6762 | // with two exceptions: |
| 6763 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6764 | // - conversions to void* |
| 6765 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6766 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6767 | return Compatible; |
| 6768 | } |
| 6769 | |
| 6770 | // - conversions from 'Class' to the redefinition type |
| 6771 | if (rhsType->isObjCClassType() && |
| 6772 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6773 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6774 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6775 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6776 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6777 | Kind = CK_BitCast; |
| 6778 | return IncompatiblePointer; |
| 6779 | } |
| 6780 | |
| 6781 | // U^ -> void* |
| 6782 | if (rhsType->getAs<BlockPointerType>()) { |
| 6783 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6784 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6785 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6786 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6787 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6788 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6789 | return Incompatible; |
| 6790 | } |
| 6791 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6792 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6793 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6794 | // U^ -> T^ |
| 6795 | if (rhsType->isBlockPointerType()) { |
| 6796 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6797 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6801 | if (rhsType->isIntegerType()) { |
| 6802 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6803 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6804 | } |
| 6805 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6806 | // id -> T^ |
| 6807 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6808 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6809 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6810 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6811 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6812 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6813 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6814 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6815 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6816 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6817 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6818 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6819 | return Incompatible; |
| 6820 | } |
| 6821 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6822 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6823 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6824 | // A* -> B* |
| 6825 | if (rhsType->isObjCObjectPointerType()) { |
| 6826 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6827 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6831 | if (rhsType->isIntegerType()) { |
| 6832 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6833 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6836 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6837 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6838 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6839 | // - conversions from 'void*' |
| 6840 | if (rhsType->isVoidPointerType()) { |
| 6841 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6842 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | // - conversions to 'Class' from its redefinition type |
| 6846 | if (lhsType->isObjCClassType() && |
| 6847 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6848 | Kind = CK_BitCast; |
| 6849 | return Compatible; |
| 6850 | } |
| 6851 | |
| 6852 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6853 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6854 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6855 | |
| 6856 | // T^ -> A* |
| 6857 | if (rhsType->isBlockPointerType()) { |
| 6858 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6859 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6860 | } |
| 6861 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6862 | return Incompatible; |
| 6863 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6864 | |
| 6865 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6866 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6867 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6868 | if (lhsType == Context.BoolTy) { |
| 6869 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6870 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6871 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6872 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6873 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6874 | if (lhsType->isIntegerType()) { |
| 6875 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6876 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6877 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6878 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6879 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6880 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6881 | |
| 6882 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6883 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6884 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6885 | if (lhsType == Context.BoolTy) { |
| 6886 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6887 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6888 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6889 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6890 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6891 | if (lhsType->isIntegerType()) { |
| 6892 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6893 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6896 | return Incompatible; |
| 6897 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6898 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6899 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6900 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6901 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6902 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6903 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6904 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6905 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6906 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6907 | return Incompatible; |
| 6908 | } |
| 6909 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6910 | /// \brief Constructs a transparent union from an expression that is |
| 6911 | /// used to initialize the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6912 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6913 | QualType UnionType, FieldDecl *Field) { |
| 6914 | // Build an initializer list that designates the appropriate member |
| 6915 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6916 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6917 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6918 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6919 | SourceLocation()); |
| 6920 | Initializer->setType(UnionType); |
| 6921 | Initializer->setInitializedFieldInUnion(Field); |
| 6922 | |
| 6923 | // Build a compound literal constructing a value of the transparent |
| 6924 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6925 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6926 | EResult = S.Owned( |
| 6927 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6928 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6929 | } |
| 6930 | |
| 6931 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6932 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6933 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6934 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6935 | // 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] | 6936 | // transparent_union GCC extension. |
| 6937 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6938 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6939 | return Incompatible; |
| 6940 | |
| 6941 | // The field to initialize within the transparent union. |
| 6942 | RecordDecl *UD = UT->getDecl(); |
| 6943 | FieldDecl *InitField = 0; |
| 6944 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6945 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6946 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6947 | it != itend; ++it) { |
| 6948 | if (it->getType()->isPointerType()) { |
| 6949 | // If the transparent union contains a pointer type, we allow: |
| 6950 | // 1) void pointer |
| 6951 | // 2) null pointer constant |
| 6952 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6953 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6954 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6955 | InitField = *it; |
| 6956 | break; |
| 6957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6958 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6959 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6960 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6961 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6962 | InitField = *it; |
| 6963 | break; |
| 6964 | } |
| 6965 | } |
| 6966 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6967 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6968 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6969 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6970 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6971 | InitField = *it; |
| 6972 | break; |
| 6973 | } |
| 6974 | } |
| 6975 | |
| 6976 | if (!InitField) |
| 6977 | return Incompatible; |
| 6978 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6979 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6980 | return Compatible; |
| 6981 | } |
| 6982 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6983 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6984 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6985 | if (getLangOptions().CPlusPlus) { |
| 6986 | if (!lhsType->isRecordType()) { |
| 6987 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6988 | // expression is implicitly converted (C++ 4) to the |
| 6989 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6990 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6991 | lhsType.getUnqualifiedType(), |
| 6992 | AA_Assigning); |
| 6993 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6994 | return Incompatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6995 | rExpr = move(Res); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6996 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
| 6999 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 7000 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7001 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 7002 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 7003 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 7004 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7005 | if ((lhsType->isPointerType() || |
| 7006 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7007 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7008 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7009 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7010 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 7011 | return Compatible; |
| 7012 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7013 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 7014 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7015 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 7016 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 7017 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 7018 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7019 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7020 | if (!lhsType->isReferenceType()) { |
| 7021 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 7022 | if (rExpr.isInvalid()) |
| 7023 | return Incompatible; |
| 7024 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7025 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7026 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7027 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 7028 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7029 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7030 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 7031 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 7032 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 7033 | // so that we can use references in built-in functions even in C. |
| 7034 | // The getNonReferenceType() call makes sure that the resulting expression |
| 7035 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7036 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 7037 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7038 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7041 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7042 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7043 | << lex.get()->getType() << rex.get()->getType() |
| 7044 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7045 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7048 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7049 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7050 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7051 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7052 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7053 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7054 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7055 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7056 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7057 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7058 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7059 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7060 | // Handle the case of a vector & extvector type of the same size and element |
| 7061 | // 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] | 7062 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7063 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7064 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7065 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7066 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7067 | if (lhsType->isExtVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7068 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7069 | return lhsType; |
| 7070 | } |
| 7071 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7072 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7073 | return rhsType; |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7074 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 7075 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 7076 | // vectors, the total size only needs to be the same. This is a |
| 7077 | // bitcast; no bits are changed but the result type is different. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7078 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7079 | return lhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7080 | } |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7081 | } |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7082 | } |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7083 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7084 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7085 | // Handle the case of equivalent AltiVec and GCC vector types |
| 7086 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 7087 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7088 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7089 | return rhsType; |
| 7090 | } |
| 7091 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7092 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 7093 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 7094 | bool swapped = false; |
| 7095 | if (rhsType->isExtVectorType()) { |
| 7096 | swapped = true; |
| 7097 | std::swap(rex, lex); |
| 7098 | std::swap(rhsType, lhsType); |
| 7099 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7100 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7101 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7102 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7103 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 7104 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7105 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 7106 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7107 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7108 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7109 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7110 | if (swapped) std::swap(rex, lex); |
| 7111 | return lhsType; |
| 7112 | } |
| 7113 | } |
| 7114 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 7115 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7116 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 7117 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7118 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7119 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7120 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7121 | if (swapped) std::swap(rex, lex); |
| 7122 | return lhsType; |
| 7123 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7124 | } |
| 7125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7126 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7127 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7128 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7129 | << lex.get()->getType() << rex.get()->getType() |
| 7130 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7131 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7132 | } |
| 7133 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7134 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7135 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 7136 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7137 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7138 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7139 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7140 | if (lex.isInvalid() || rex.isInvalid()) |
| 7141 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7142 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7143 | if (!lex.get()->getType()->isArithmeticType() || |
| 7144 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7145 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7146 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7147 | // Check for division by zero. |
| 7148 | if (isDiv && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7149 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7150 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 7151 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7152 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7153 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7156 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7157 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7158 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7159 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7160 | rex.get()->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 7161 | return CheckVectorOperands(Loc, lex, rex); |
| 7162 | return InvalidOperands(Loc, lex, rex); |
| 7163 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7164 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7165 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7166 | if (lex.isInvalid() || rex.isInvalid()) |
| 7167 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7168 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7169 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7170 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7171 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7172 | // Check for remainder by zero. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7173 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7174 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 7175 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7176 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7177 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7178 | } |
| 7179 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7180 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7181 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 7182 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7183 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7184 | if (CompLHSTy) *CompLHSTy = compType; |
| 7185 | return compType; |
| 7186 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7187 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7188 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7189 | if (lex.isInvalid() || rex.isInvalid()) |
| 7190 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7192 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7193 | if (lex.get()->getType()->isArithmeticType() && |
| 7194 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7195 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7196 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7197 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7198 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7199 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7200 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7201 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7202 | std::swap(PExp, IExp); |
| 7203 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7204 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7205 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7206 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7207 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7208 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7209 | // Check for arithmetic on pointers to incomplete types. |
| 7210 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7211 | if (getLangOptions().CPlusPlus) { |
| 7212 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7213 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7214 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7215 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7216 | |
| 7217 | // GNU extension: arithmetic on pointer to void |
| 7218 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7219 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7220 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7221 | if (getLangOptions().CPlusPlus) { |
| 7222 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chandler Carruth | ae0bafa | 2011-06-20 07:52:11 +0000 | [diff] [blame] | 7223 | << PExp->getType() << PExp->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7224 | return QualType(); |
| 7225 | } |
| 7226 | |
| 7227 | // GNU extension: arithmetic on pointer to function |
| 7228 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Chandler Carruth | ae0bafa | 2011-06-20 07:52:11 +0000 | [diff] [blame] | 7229 | << PExp->getType() << PExp->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7230 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7231 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7232 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7233 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7234 | PExp->getType()->isObjCObjectPointerType()) && |
| 7235 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7236 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 7237 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7238 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7239 | return QualType(); |
| 7240 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7241 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7242 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7243 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 7244 | << PointeeTy << PExp->getSourceRange(); |
| 7245 | return QualType(); |
| 7246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7247 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7248 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7249 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7250 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7251 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7252 | if (LHSTy->isPromotableIntegerType()) |
| 7253 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7254 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7255 | *CompLHSTy = LHSTy; |
| 7256 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7257 | return PExp->getType(); |
| 7258 | } |
| 7259 | } |
| 7260 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7261 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7264 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7265 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7266 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7267 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7268 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7269 | if (CompLHSTy) *CompLHSTy = compType; |
| 7270 | return compType; |
| 7271 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7272 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7273 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7274 | if (lex.isInvalid() || rex.isInvalid()) |
| 7275 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7276 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7277 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7278 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7279 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7280 | if (lex.get()->getType()->isArithmeticType() && |
| 7281 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7282 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7283 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7285 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7286 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7287 | if (lex.get()->getType()->isAnyPointerType()) { |
| 7288 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7289 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7290 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7291 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7292 | bool ComplainAboutVoid = false; |
| 7293 | Expr *ComplainAboutFunc = 0; |
| 7294 | if (lpointee->isVoidType()) { |
| 7295 | if (getLangOptions().CPlusPlus) { |
| 7296 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7297 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7298 | return QualType(); |
| 7299 | } |
| 7300 | |
| 7301 | // GNU C extension: arithmetic on pointer to void |
| 7302 | ComplainAboutVoid = true; |
| 7303 | } else if (lpointee->isFunctionType()) { |
| 7304 | if (getLangOptions().CPlusPlus) { |
| 7305 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7306 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7307 | return QualType(); |
| 7308 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7309 | |
| 7310 | // GNU C extension: arithmetic on pointer to function |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7311 | ComplainAboutFunc = lex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7312 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7313 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7314 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7315 | << lex.get()->getSourceRange() |
| 7316 | << lex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7317 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7318 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7319 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7320 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7321 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7322 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7323 | return QualType(); |
| 7324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7325 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7326 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7327 | if (rex.get()->getType()->isIntegerType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7328 | if (ComplainAboutVoid) |
| 7329 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7330 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7331 | if (ComplainAboutFunc) |
| 7332 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7333 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7334 | << ComplainAboutFunc->getSourceRange(); |
| 7335 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7336 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 7337 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7338 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7339 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7340 | // Handle pointer-pointer subtractions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7341 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 7342 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7343 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7344 | // RHS must be a completely-type object type. |
| 7345 | // Handle the GNU void* extension. |
| 7346 | if (rpointee->isVoidType()) { |
| 7347 | if (getLangOptions().CPlusPlus) { |
| 7348 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7349 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7350 | return QualType(); |
| 7351 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7352 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7353 | ComplainAboutVoid = true; |
| 7354 | } else if (rpointee->isFunctionType()) { |
| 7355 | if (getLangOptions().CPlusPlus) { |
| 7356 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7357 | << rex.get()->getType() << rex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7358 | return QualType(); |
| 7359 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7360 | |
| 7361 | // GNU extension: arithmetic on pointer to function |
| 7362 | if (!ComplainAboutFunc) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7363 | ComplainAboutFunc = rex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7364 | } else if (!rpointee->isDependentType() && |
| 7365 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7366 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7367 | << rex.get()->getSourceRange() |
| 7368 | << rex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7369 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7370 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7371 | if (getLangOptions().CPlusPlus) { |
| 7372 | // Pointee types must be the same: C++ [expr.add] |
| 7373 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 7374 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7375 | << lex.get()->getType() << rex.get()->getType() |
| 7376 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7377 | return QualType(); |
| 7378 | } |
| 7379 | } else { |
| 7380 | // Pointee types must be compatible C99 6.5.6p3 |
| 7381 | if (!Context.typesAreCompatible( |
| 7382 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 7383 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 7384 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7385 | << lex.get()->getType() << rex.get()->getType() |
| 7386 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7387 | return QualType(); |
| 7388 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7389 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7390 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7391 | if (ComplainAboutVoid) |
| 7392 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7393 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7394 | if (ComplainAboutFunc) |
| 7395 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7396 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7397 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7398 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7399 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7400 | return Context.getPointerDiffType(); |
| 7401 | } |
| 7402 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7403 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7404 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7405 | } |
| 7406 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7407 | static bool isScopedEnumerationType(QualType T) { |
| 7408 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 7409 | return ET->getDecl()->isScoped(); |
| 7410 | return false; |
| 7411 | } |
| 7412 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7413 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7414 | SourceLocation Loc, unsigned Opc, |
| 7415 | QualType LHSTy) { |
| 7416 | llvm::APSInt Right; |
| 7417 | // Check right/shifter operand |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7418 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7419 | return; |
| 7420 | |
| 7421 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7422 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 7423 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7424 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7425 | return; |
| 7426 | } |
| 7427 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7428 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7429 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7430 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 7431 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7432 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7433 | return; |
| 7434 | } |
| 7435 | if (Opc != BO_Shl) |
| 7436 | return; |
| 7437 | |
| 7438 | // When left shifting an ICE which is signed, we can check for overflow which |
| 7439 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 7440 | // integers have defined behavior modulo one more than the maximum value |
| 7441 | // representable in the result type, so never warn for those. |
| 7442 | llvm::APSInt Left; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7443 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7444 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 7445 | return; |
| 7446 | llvm::APInt ResultBits = |
| 7447 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 7448 | if (LeftBits.uge(ResultBits)) |
| 7449 | return; |
| 7450 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 7451 | Result = Result.shl(Right); |
| 7452 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7453 | // Print the bit representation of the signed integer as an unsigned |
| 7454 | // hexadecimal number. |
| 7455 | llvm::SmallString<40> HexResult; |
| 7456 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 7457 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7458 | // If we are only missing a sign bit, this is less likely to result in actual |
| 7459 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 7460 | // expected value. Thus we place this behind a different warning that can be |
| 7461 | // turned off separately if needed. |
| 7462 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7463 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 7464 | << HexResult.str() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7465 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7466 | return; |
| 7467 | } |
| 7468 | |
| 7469 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7470 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7471 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7472 | } |
| 7473 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7474 | // C99 6.5.7 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7475 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7476 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7477 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7478 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7479 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7480 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7481 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7482 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7483 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7484 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7485 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7486 | return InvalidOperands(Loc, lex, rex); |
| 7487 | } |
| 7488 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7489 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7490 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7491 | return CheckVectorOperands(Loc, lex, rex); |
| 7492 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7493 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7494 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7495 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7496 | // For the LHS, do usual unary conversions, but then reset them away |
| 7497 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7498 | ExprResult old_lex = lex; |
| 7499 | lex = UsualUnaryConversions(lex.take()); |
| 7500 | if (lex.isInvalid()) |
| 7501 | return QualType(); |
| 7502 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7503 | if (isCompAssign) lex = old_lex; |
| 7504 | |
| 7505 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7506 | rex = UsualUnaryConversions(rex.take()); |
| 7507 | if (rex.isInvalid()) |
| 7508 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7509 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7510 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7511 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7512 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7513 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7514 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7517 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7518 | if (DeclContext *DC = D->getDeclContext()) { |
| 7519 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7520 | return true; |
| 7521 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7522 | return FD->isFunctionTemplateSpecialization(); |
| 7523 | } |
| 7524 | return false; |
| 7525 | } |
| 7526 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7527 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7528 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7529 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7530 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7531 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7532 | // Handle vector comparisons separately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7533 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7534 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7535 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7536 | QualType lType = lex.get()->getType(); |
| 7537 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7538 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7539 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7540 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7541 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7542 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7543 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7544 | |
| 7545 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7546 | // Two different enums will raise a warning when compared. |
| 7547 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7548 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7549 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7550 | RHSEnumType->getDecl()->getIdentifier() && |
| 7551 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7552 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7553 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7554 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7555 | } |
| 7556 | } |
| 7557 | } |
| 7558 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7559 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7560 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7561 | !lex.get()->getLocStart().isMacroID() && |
| 7562 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7563 | // For non-floating point types, check for self-comparisons of the form |
| 7564 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7565 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7566 | // |
| 7567 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7568 | // expansion. Also don't warn about comparisons which are only self |
| 7569 | // comparisons within a template specialization. The warnings should catch |
| 7570 | // obvious cases in the definition of the template anyways. The idea is to |
| 7571 | // warn when the typed comparison operator will always evaluate to the same |
| 7572 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7573 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7574 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7575 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7576 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7577 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7578 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7579 | << (Opc == BO_EQ |
| 7580 | || Opc == BO_LE |
| 7581 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7582 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7583 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7584 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7585 | // what is it always going to eval to? |
| 7586 | char always_evals_to; |
| 7587 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7588 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7589 | always_evals_to = 0; // false |
| 7590 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7591 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7592 | always_evals_to = 1; // true |
| 7593 | break; |
| 7594 | default: |
| 7595 | // best we can say is 'a constant' |
| 7596 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7597 | break; |
| 7598 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7599 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7600 | << 1 // array |
| 7601 | << always_evals_to); |
| 7602 | } |
| 7603 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7605 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7606 | if (isa<CastExpr>(LHSStripped)) |
| 7607 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7608 | if (isa<CastExpr>(RHSStripped)) |
| 7609 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7610 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7611 | // Warn about comparisons against a string constant (unless the other |
| 7612 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7613 | Expr *literalString = 0; |
| 7614 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7615 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7616 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7617 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7618 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7619 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7620 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7621 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7622 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7623 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7624 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7625 | literalStringStripped = RHSStripped; |
| 7626 | } |
| 7627 | |
| 7628 | if (literalString) { |
| 7629 | std::string resultComparison; |
| 7630 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7631 | case BO_LT: resultComparison = ") < 0"; break; |
| 7632 | case BO_GT: resultComparison = ") > 0"; break; |
| 7633 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7634 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7635 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7636 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7637 | default: assert(false && "Invalid comparison operator"); |
| 7638 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7639 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7640 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7641 | PDiag(diag::warn_stringcompare) |
| 7642 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7643 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7644 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7645 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7646 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7647 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7648 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7649 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7650 | if (lex.isInvalid() || rex.isInvalid()) |
| 7651 | return QualType(); |
| 7652 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7653 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7654 | lex = UsualUnaryConversions(lex.take()); |
| 7655 | if (lex.isInvalid()) |
| 7656 | return QualType(); |
| 7657 | |
| 7658 | rex = UsualUnaryConversions(rex.take()); |
| 7659 | if (rex.isInvalid()) |
| 7660 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7663 | lType = lex.get()->getType(); |
| 7664 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7665 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7666 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7667 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7668 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7669 | if (isRelational) { |
| 7670 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7671 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7672 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7673 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7674 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7675 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7676 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7677 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7678 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7679 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7680 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7681 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7682 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7683 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7684 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7685 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7686 | // All of the following pointer-related warnings are GCC extensions, except |
| 7687 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7688 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7689 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7690 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7691 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7692 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7693 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7694 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7695 | if (LCanPointeeTy == RCanPointeeTy) |
| 7696 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7697 | if (!isRelational && |
| 7698 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7699 | // Valid unless comparison between non-null pointer and function pointer |
| 7700 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7701 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7702 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7703 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7704 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7705 | Diag(Loc, |
| 7706 | isSFINAEContext()? |
| 7707 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7708 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7709 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7710 | |
| 7711 | if (isSFINAEContext()) |
| 7712 | return QualType(); |
| 7713 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7714 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7715 | return ResultTy; |
| 7716 | } |
| 7717 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7718 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7719 | // C++ [expr.rel]p2: |
| 7720 | // [...] Pointer conversions (4.10) and qualification |
| 7721 | // conversions (4.4) are performed on pointer operands (or on |
| 7722 | // a pointer operand and a null pointer constant) to bring |
| 7723 | // them to their composite pointer type. [...] |
| 7724 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7725 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7726 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7727 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7728 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7729 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7730 | if (T.isNull()) { |
| 7731 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7732 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7733 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7734 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7735 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7736 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7737 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7738 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7739 | } |
| 7740 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7741 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7742 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7743 | return ResultTy; |
| 7744 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7745 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7746 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7747 | RCanPointeeTy.getUnqualifiedType())) { |
| 7748 | // Valid unless a relational comparison of function pointers |
| 7749 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7750 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7751 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7752 | } |
| 7753 | } else if (!isRelational && |
| 7754 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7755 | // Valid unless comparison between non-null pointer and function pointer |
| 7756 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7757 | && !LHSIsNull && !RHSIsNull) { |
| 7758 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7759 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7760 | } |
| 7761 | } else { |
| 7762 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7763 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7764 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7765 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7766 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7767 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7768 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7769 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7770 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7771 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7772 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7774 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7775 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7776 | // Comparison of nullptr_t with itself. |
| 7777 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7778 | return ResultTy; |
| 7779 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7780 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7781 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7782 | if (RHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7783 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 7784 | (!isRelational && |
| 7785 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7786 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7787 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7788 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7789 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7790 | return ResultTy; |
| 7791 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7792 | if (LHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7793 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 7794 | (!isRelational && |
| 7795 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7796 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7797 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7798 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7799 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7800 | return ResultTy; |
| 7801 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7802 | |
| 7803 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7804 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7805 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7806 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7807 | // In addition, pointers to members can be compared, or a pointer to |
| 7808 | // member and a null pointer constant. Pointer to member conversions |
| 7809 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7810 | // them to a common type. If one operand is a null pointer constant, |
| 7811 | // the common type is the type of the other operand. Otherwise, the |
| 7812 | // common type is a pointer to member type similar (4.4) to the type |
| 7813 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7814 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7815 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7816 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7817 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7818 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7819 | if (T.isNull()) { |
| 7820 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7821 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7822 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7823 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7824 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7825 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7826 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7827 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7829 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7830 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7831 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7832 | return ResultTy; |
| 7833 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7834 | |
| 7835 | // Handle scoped enumeration types specifically, since they don't promote |
| 7836 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7837 | if (lex.get()->getType()->isEnumeralType() && |
| 7838 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7839 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7841 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7842 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7843 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7844 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7845 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7846 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7847 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7848 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7849 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7850 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7851 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7852 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7853 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7854 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7855 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7856 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7857 | if (!isRelational |
| 7858 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7859 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7860 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7861 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7862 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7863 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7864 | ->getPointeeType()->isVoidType()))) |
| 7865 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7866 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7867 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7868 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7869 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7870 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7871 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7872 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7873 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7874 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7875 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7876 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7877 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7878 | if (LPT || RPT) { |
| 7879 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7880 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7881 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7882 | if (!LPtrToVoid && !RPtrToVoid && |
| 7883 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7884 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7885 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7886 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7887 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7888 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7889 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7890 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7891 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7892 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7893 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7894 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7895 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7896 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7897 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7898 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7899 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7900 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7901 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7902 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7903 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7904 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7905 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7906 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7907 | bool isError = false; |
| 7908 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7909 | (RHSIsNull && rType->isIntegerType())) { |
| 7910 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7911 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7912 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7913 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7914 | else if (getLangOptions().CPlusPlus) { |
| 7915 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7916 | isError = true; |
| 7917 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7918 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7919 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7920 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7921 | Diag(Loc, DiagID) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7922 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7923 | if (isError) |
| 7924 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7925 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7926 | |
| 7927 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7928 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7929 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7930 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7931 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7932 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7933 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7934 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7935 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7936 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7937 | if (!isRelational && RHSIsNull |
| 7938 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7939 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7940 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7941 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7942 | if (!isRelational && LHSIsNull |
| 7943 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7944 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7945 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7946 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7947 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7948 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7951 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7952 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7953 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7954 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7955 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7956 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7957 | bool isRelational) { |
| 7958 | // Check to make sure we're operating on vectors of the same type and width, |
| 7959 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7960 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7961 | if (vType.isNull()) |
| 7962 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7963 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7964 | QualType lType = lex.get()->getType(); |
| 7965 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7966 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7967 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7968 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7969 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7970 | return Context.getLogicalOperationType(); |
| 7971 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7972 | // For non-floating point types, check for self-comparisons of the form |
| 7973 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7974 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7975 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7976 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7977 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7978 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7979 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7980 | PDiag(diag::warn_comparison_always) |
| 7981 | << 0 // self- |
| 7982 | << 2 // "a constant" |
| 7983 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7984 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7985 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7986 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7987 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7988 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7989 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7990 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7991 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7992 | // Return the type for the comparison, which is the same as vector type for |
| 7993 | // integer vectors, or an integer type of identical size and number of |
| 7994 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7995 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7996 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7997 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7998 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7999 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 8000 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 8001 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 8002 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 8003 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 8004 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8005 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 8006 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 8007 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 8008 | } |
| 8009 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8010 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8011 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 8012 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 8013 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 8014 | rex.get()->getType()->hasIntegerRepresentation()) |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 8015 | return CheckVectorOperands(Loc, lex, rex); |
| 8016 | |
| 8017 | return InvalidOperands(Loc, lex, rex); |
| 8018 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 8019 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8020 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 8021 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 8022 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 8023 | return QualType(); |
| 8024 | lex = lexResult.take(); |
| 8025 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8026 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8027 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 8028 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 8029 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8030 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8034 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8035 | |
| 8036 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 8037 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 8038 | // is a constant. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8039 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 8040 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 8041 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8042 | !Loc.isMacroID()) { |
| 8043 | // If the RHS can be constant folded, and if it constant folds to something |
| 8044 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 8045 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8046 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8047 | Expr::EvalResult Result; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8048 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 8049 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 8050 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 8051 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 8052 | << rex.get()->getSourceRange() |
| 8053 | << (Opc == BO_LAnd ? "&&" : "||") |
| 8054 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8055 | } |
| 8056 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8057 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8058 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8059 | lex = UsualUnaryConversions(lex.take()); |
| 8060 | if (lex.isInvalid()) |
| 8061 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8062 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8063 | rex = UsualUnaryConversions(rex.take()); |
| 8064 | if (rex.isInvalid()) |
| 8065 | return QualType(); |
| 8066 | |
| 8067 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8068 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8069 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8070 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 8071 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8072 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8073 | // The following is safe because we only use this method for |
| 8074 | // non-overloadable operands. |
| 8075 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8076 | // C++ [expr.log.and]p1 |
| 8077 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8078 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8079 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 8080 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8081 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8082 | lex = move(lexRes); |
| 8083 | |
| 8084 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 8085 | if (rexRes.isInvalid()) |
| 8086 | return InvalidOperands(Loc, lex, rex); |
| 8087 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8088 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8089 | // C++ [expr.log.and]p2 |
| 8090 | // C++ [expr.log.or]p2 |
| 8091 | // The result is a bool. |
| 8092 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8093 | } |
| 8094 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8095 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 8096 | /// is a read-only property; return true if so. A readonly property expression |
| 8097 | /// depends on various declarations and thus must be treated specially. |
| 8098 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8099 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8100 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8101 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8102 | if (PropExpr->isImplicitProperty()) return false; |
| 8103 | |
| 8104 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8105 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 8106 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 8107 | PropExpr->getBase()->getType(); |
| 8108 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8109 | if (const ObjCObjectPointerType *OPT = |
| 8110 | BaseType->getAsObjCInterfacePointerType()) |
| 8111 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 8112 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 8113 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8114 | } |
| 8115 | return false; |
| 8116 | } |
| 8117 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8118 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 8119 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8120 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 8121 | if (PropExpr->isImplicitProperty()) return false; |
| 8122 | |
| 8123 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8124 | QualType T = PDecl->getType(); |
| 8125 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 8126 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8127 | CanQualType CT = S.Context.getCanonicalType(T); |
| 8128 | return CT.isConstQualified(); |
| 8129 | } |
| 8130 | return false; |
| 8131 | } |
| 8132 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8133 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 8134 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 8135 | return false; |
| 8136 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 8137 | NamedDecl *Member = ME->getMemberDecl(); |
| 8138 | if (isa<FieldDecl>(Member)) { |
| 8139 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8140 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 8141 | return false; |
| 8142 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 8143 | } |
| 8144 | return false; |
| 8145 | } |
| 8146 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8147 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 8148 | /// emit an error and return true. If so, return false. |
| 8149 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8150 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8151 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8152 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8153 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 8154 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8155 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 8156 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8157 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 8158 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8159 | if (IsLV == Expr::MLV_Valid) |
| 8160 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8161 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8162 | unsigned Diag = 0; |
| 8163 | bool NeedType = false; |
| 8164 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8165 | case Expr::MLV_ConstQualified: |
| 8166 | Diag = diag::err_typecheck_assign_const; |
| 8167 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 8168 | // In ARC, use some specialized diagnostics for occasions where we |
| 8169 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8170 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 8171 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 8172 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 8173 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 8174 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 8175 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 8176 | // user actually wrote 'const'. |
| 8177 | if (var->isARCPseudoStrong() && |
| 8178 | (!var->getTypeSourceInfo() || |
| 8179 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 8180 | // There are two pseudo-strong cases: |
| 8181 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8182 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 8183 | if (method && var == method->getSelfDecl()) |
| 8184 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 8185 | |
| 8186 | // - fast enumeration variables |
| 8187 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8188 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 8189 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8190 | SourceRange Assign; |
| 8191 | if (Loc != OrigLoc) |
| 8192 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 8193 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 8194 | // We need to preserve the AST regardless, so migration tool |
| 8195 | // can do its job. |
| 8196 | return false; |
| 8197 | } |
| 8198 | } |
| 8199 | } |
| 8200 | |
| 8201 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8202 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8203 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 8204 | NeedType = true; |
| 8205 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8206 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8207 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 8208 | NeedType = true; |
| 8209 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 8210 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8211 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 8212 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8213 | case Expr::MLV_Valid: |
| 8214 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8215 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8216 | case Expr::MLV_MemberFunction: |
| 8217 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8218 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 8219 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8220 | case Expr::MLV_IncompleteType: |
| 8221 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 8222 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8223 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 8224 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8225 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8226 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 8227 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 8228 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8229 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 8230 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 8231 | case Expr::MLV_ReadonlyProperty: |
| 8232 | Diag = diag::error_readonly_property_assignment; |
| 8233 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 8234 | case Expr::MLV_NoSetterProperty: |
| 8235 | Diag = diag::error_nosetter_property_assignment; |
| 8236 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8237 | case Expr::MLV_InvalidMessageExpression: |
| 8238 | Diag = diag::error_readonly_message_assignment; |
| 8239 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 8240 | case Expr::MLV_SubObjCPropertySetting: |
| 8241 | Diag = diag::error_no_subobject_property_setting; |
| 8242 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8243 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 8244 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8245 | SourceRange Assign; |
| 8246 | if (Loc != OrigLoc) |
| 8247 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8248 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8249 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8250 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8251 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8252 | return true; |
| 8253 | } |
| 8254 | |
| 8255 | |
| 8256 | |
| 8257 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8258 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8259 | SourceLocation Loc, |
| 8260 | QualType CompoundType) { |
| 8261 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 8262 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8263 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8264 | |
| 8265 | QualType LHSType = LHS->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8266 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8267 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8268 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8269 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8270 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8271 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 8272 | ExprResult LHSResult = Owned(LHS); |
| 8273 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 8274 | if (LHSResult.isInvalid()) |
| 8275 | return QualType(); |
| 8276 | LHS = LHSResult.take(); |
| 8277 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8278 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8279 | if (RHS.isInvalid()) |
| 8280 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8281 | // Special case of NSObject attributes on c-style pointer types. |
| 8282 | if (ConvTy == IncompatiblePointer && |
| 8283 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8284 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8285 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8286 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8287 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8288 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8289 | if (ConvTy == Compatible && |
| 8290 | getLangOptions().ObjCNonFragileABI && |
| 8291 | LHSType->isObjCObjectType()) |
| 8292 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 8293 | << LHSType; |
| 8294 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8295 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 8296 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 8297 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8298 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8299 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 8300 | RHSCheck = ICE->getSubExpr(); |
| 8301 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8302 | if ((UO->getOpcode() == UO_Plus || |
| 8303 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8304 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8305 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8306 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 8307 | // And there is a space or other character before the subexpr of the |
| 8308 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 8309 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 8310 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8311 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8312 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8313 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8314 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8315 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8316 | |
| 8317 | if (ConvTy == Compatible) { |
| 8318 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 8319 | checkRetainCycles(LHS, RHS.get()); |
| 8320 | else |
| 8321 | checkUnsafeAssigns(Loc, LHSType, RHS.get()); |
| 8322 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8323 | } else { |
| 8324 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 8325 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8326 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8327 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8328 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8329 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8330 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8331 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 8332 | CheckForNullPointerDereference(*this, LHS); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8333 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8334 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8335 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8336 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 8337 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8338 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8339 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 8340 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8341 | // 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] | 8342 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 8343 | return (getLangOptions().CPlusPlus |
| 8344 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8345 | } |
| 8346 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8347 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8348 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8349 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8350 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 8351 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8352 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 8353 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8354 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 8355 | return QualType(); |
| 8356 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8357 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 8358 | // operands, but not unary promotions. |
| 8359 | // 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] | 8360 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8361 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 8362 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8363 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 8364 | if (LHS.isInvalid()) |
| 8365 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8366 | |
| 8367 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8368 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 8369 | if (RHS.isInvalid()) |
| 8370 | return QualType(); |
| 8371 | if (!RHS.get()->getType()->isVoidType()) |
| 8372 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8373 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8374 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8375 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8376 | } |
| 8377 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 8378 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 8379 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8380 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 8381 | ExprValueKind &VK, |
| 8382 | SourceLocation OpLoc, |
| 8383 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8384 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8385 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8386 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8387 | QualType ResType = Op->getType(); |
| 8388 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8389 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8390 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8391 | // Decrement of bool is not allowed. |
| 8392 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8393 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8394 | return QualType(); |
| 8395 | } |
| 8396 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8397 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8398 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8399 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 8400 | } else if (ResType->isAnyPointerType()) { |
| 8401 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8402 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8403 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8404 | if (PointeeTy->isVoidType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8405 | if (S.getLangOptions().CPlusPlus) { |
| 8406 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8407 | << Op->getSourceRange(); |
| 8408 | return QualType(); |
| 8409 | } |
| 8410 | |
| 8411 | // Pointer to void is a GNU extension in C. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8412 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8413 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8414 | if (S.getLangOptions().CPlusPlus) { |
| 8415 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8416 | << Op->getType() << Op->getSourceRange(); |
| 8417 | return QualType(); |
| 8418 | } |
| 8419 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8420 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8421 | << ResType << Op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8422 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 8423 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8424 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 8425 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 8426 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8427 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8428 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 8429 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8430 | << PointeeTy << Op->getSourceRange(); |
| 8431 | return QualType(); |
| 8432 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 8433 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8434 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8435 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8436 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8437 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8438 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8439 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8440 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 8441 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 8442 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 8443 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8444 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8445 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 8446 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8447 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8448 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8449 | // 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] | 8450 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8451 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8452 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8453 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 8454 | // (in C or with postfix), the increment is the unqualified type of the |
| 8455 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8456 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 8457 | VK = VK_LValue; |
| 8458 | return ResType; |
| 8459 | } else { |
| 8460 | VK = VK_RValue; |
| 8461 | return ResType.getUnqualifiedType(); |
| 8462 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8465 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8466 | assert(E->getValueKind() == VK_LValue && |
| 8467 | E->getObjectKind() == OK_ObjCProperty); |
| 8468 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 8469 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8470 | QualType T = E->getType(); |
| 8471 | QualType ReceiverType; |
| 8472 | if (PRE->isObjectReceiver()) |
| 8473 | ReceiverType = PRE->getBase()->getType(); |
| 8474 | else if (PRE->isSuperReceiver()) |
| 8475 | ReceiverType = PRE->getSuperReceiverType(); |
| 8476 | else |
| 8477 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 8478 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8479 | ExprValueKind VK = VK_RValue; |
| 8480 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8481 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8482 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8483 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 8484 | PRE->isClassReceiver(), |
| 8485 | PRE->isSuperReceiver()); |
| 8486 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8487 | } |
| 8488 | else { |
| 8489 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 8490 | << PRE->getBase()->getType(); |
| 8491 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8492 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8493 | |
| 8494 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8495 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 8496 | |
| 8497 | ExprResult Result = MaybeBindToTemporary(E); |
| 8498 | if (!Result.isInvalid()) |
| 8499 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8500 | |
| 8501 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8504 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 8505 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 8506 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8507 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8508 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8509 | bool Consumed = false; |
| 8510 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8511 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8512 | // If using property-dot syntax notation for assignment, and there is a |
| 8513 | // setter, RHS expression is being passed to the setter argument. So, |
| 8514 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8515 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8516 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8517 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8518 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 8519 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8520 | |
| 8521 | // Otherwise, if the getter returns an l-value, just call that. |
| 8522 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8523 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8524 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8525 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8526 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8527 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8528 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8529 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8530 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8531 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 8532 | const ObjCMethodDecl *setter |
| 8533 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 8534 | if (setter) { |
| 8535 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 8536 | LHSTy = (*P)->getType(); |
| 8537 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 8538 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8539 | } |
| 8540 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8541 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 8542 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8543 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8544 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8545 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8546 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8547 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8548 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 8549 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 8550 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8551 | } |
| 8552 | } |
| 8553 | |
| 8554 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8555 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8556 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8557 | /// where the declaration is needed for type checking. We only need to |
| 8558 | /// handle cases when the expression references a function designator |
| 8559 | /// or is an lvalue. Here are some examples: |
| 8560 | /// - &(x) => x |
| 8561 | /// - &*****f => f for f a function designator. |
| 8562 | /// - &s.xx => s |
| 8563 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8564 | /// - *(x + 1) -> x, if x is an array |
| 8565 | /// - &"123"[2] -> 0 |
| 8566 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8567 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8568 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8569 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8570 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8571 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8572 | // If this is an arrow operator, the address is an offset from |
| 8573 | // the base's value, so the object the base refers to is |
| 8574 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8575 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8576 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8577 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8578 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8579 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8580 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8581 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8582 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8583 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8584 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8585 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8586 | } |
| 8587 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8588 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8589 | case Stmt::UnaryOperatorClass: { |
| 8590 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8591 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8592 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8593 | case UO_Real: |
| 8594 | case UO_Imag: |
| 8595 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8596 | return getPrimaryDecl(UO->getSubExpr()); |
| 8597 | default: |
| 8598 | return 0; |
| 8599 | } |
| 8600 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8601 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8602 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8603 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8604 | // If the result of an implicit cast is an l-value, we care about |
| 8605 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8606 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8607 | default: |
| 8608 | return 0; |
| 8609 | } |
| 8610 | } |
| 8611 | |
| 8612 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8613 | /// 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] | 8614 | /// 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] | 8615 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8616 | /// 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] | 8617 | /// 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] | 8618 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8619 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8620 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8621 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8622 | return S.Context.DependentTy; |
| 8623 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8624 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8625 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8626 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8627 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 8628 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8629 | << OrigOp->getSourceRange(); |
| 8630 | return QualType(); |
| 8631 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8632 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8633 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8634 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8635 | // Make sure to ignore parentheses in subsequent checks |
| 8636 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8637 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8638 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8639 | // Implement C99-only parts of addressof rules. |
| 8640 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8641 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8642 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8643 | // (assuming the deref expression is valid). |
| 8644 | return uOp->getSubExpr()->getType(); |
| 8645 | } |
| 8646 | // Technically, there should be a check for array subscript |
| 8647 | // expressions here, but the result of one is always an lvalue anyway. |
| 8648 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8649 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8650 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8651 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8652 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8653 | bool sfinae = S.isSFINAEContext(); |
| 8654 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8655 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8656 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8657 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8658 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8659 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8660 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8661 | } else if (lval == Expr::LV_MemberFunction) { |
| 8662 | // If it's an instance method, make a member pointer. |
| 8663 | // The expression must have exactly the form &A::foo. |
| 8664 | |
| 8665 | // If the underlying expression isn't a decl ref, give up. |
| 8666 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8667 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8668 | << OrigOp->getSourceRange(); |
| 8669 | return QualType(); |
| 8670 | } |
| 8671 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8672 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8673 | |
| 8674 | // The id-expression was parenthesized. |
| 8675 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8676 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8677 | << OrigOp->getSourceRange(); |
| 8678 | |
| 8679 | // The method was named without a qualifier. |
| 8680 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8681 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8682 | << op->getSourceRange(); |
| 8683 | } |
| 8684 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8685 | return S.Context.getMemberPointerType(op->getType(), |
| 8686 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8687 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8688 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8689 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8690 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8691 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8692 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8693 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8694 | return QualType(); |
| 8695 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8696 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8697 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8698 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8699 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8700 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8701 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8702 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8703 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8704 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8705 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8706 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8707 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8708 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8709 | << "property expression" << op->getSourceRange(); |
| 8710 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8711 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8712 | // 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] | 8713 | // with the register storage-class specifier. |
| 8714 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8715 | // in C++ it is not error to take address of a register |
| 8716 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8717 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8718 | !S.getLangOptions().CPlusPlus) { |
| 8719 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8720 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8721 | return QualType(); |
| 8722 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8723 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8724 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8725 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8726 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8727 | // Could be a pointer to member, though, if there is an explicit |
| 8728 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8729 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8730 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8731 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8732 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8733 | S.Diag(OpLoc, |
| 8734 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8735 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8736 | return QualType(); |
| 8737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8738 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8739 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8740 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8741 | return S.Context.getMemberPointerType(op->getType(), |
| 8742 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8743 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8744 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8745 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8746 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8747 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8748 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8749 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8750 | // Taking the address of a void variable is technically illegal, but we |
| 8751 | // allow it in cases which are otherwise valid. |
| 8752 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8753 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8754 | } |
| 8755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8756 | // 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] | 8757 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8758 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8759 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8762 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8763 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8764 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8765 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8766 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8767 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8768 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8769 | if (ConvResult.isInvalid()) |
| 8770 | return QualType(); |
| 8771 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8772 | QualType OpTy = Op->getType(); |
| 8773 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8774 | |
| 8775 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8776 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8777 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8778 | Op->getSourceRange()); |
| 8779 | } |
| 8780 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8781 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8782 | // is an incomplete type or void. It would be possible to warn about |
| 8783 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8784 | // warning is unlikely to catch any mistakes. |
| 8785 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8786 | Result = PT->getPointeeType(); |
| 8787 | else if (const ObjCObjectPointerType *OPT = |
| 8788 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8789 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8790 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8791 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8792 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8793 | if (PR.take() != Op) |
| 8794 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8795 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8796 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8797 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8798 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8799 | << OpTy << Op->getSourceRange(); |
| 8800 | return QualType(); |
| 8801 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8802 | |
| 8803 | // Dereferences are usually l-values... |
| 8804 | VK = VK_LValue; |
| 8805 | |
| 8806 | // ...except that certain expressions are never l-values in C. |
| 8807 | if (!S.getLangOptions().CPlusPlus && |
| 8808 | IsCForbiddenLValueType(S.Context, Result)) |
| 8809 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8810 | |
| 8811 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8812 | } |
| 8813 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8814 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8815 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8816 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8817 | switch (Kind) { |
| 8818 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8819 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8820 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8821 | case tok::star: Opc = BO_Mul; break; |
| 8822 | case tok::slash: Opc = BO_Div; break; |
| 8823 | case tok::percent: Opc = BO_Rem; break; |
| 8824 | case tok::plus: Opc = BO_Add; break; |
| 8825 | case tok::minus: Opc = BO_Sub; break; |
| 8826 | case tok::lessless: Opc = BO_Shl; break; |
| 8827 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8828 | case tok::lessequal: Opc = BO_LE; break; |
| 8829 | case tok::less: Opc = BO_LT; break; |
| 8830 | case tok::greaterequal: Opc = BO_GE; break; |
| 8831 | case tok::greater: Opc = BO_GT; break; |
| 8832 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8833 | case tok::equalequal: Opc = BO_EQ; break; |
| 8834 | case tok::amp: Opc = BO_And; break; |
| 8835 | case tok::caret: Opc = BO_Xor; break; |
| 8836 | case tok::pipe: Opc = BO_Or; break; |
| 8837 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8838 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8839 | case tok::equal: Opc = BO_Assign; break; |
| 8840 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8841 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8842 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8843 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8844 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8845 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8846 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8847 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8848 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8849 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8850 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8851 | } |
| 8852 | return Opc; |
| 8853 | } |
| 8854 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8855 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8856 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8857 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8858 | switch (Kind) { |
| 8859 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8860 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8861 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8862 | case tok::amp: Opc = UO_AddrOf; break; |
| 8863 | case tok::star: Opc = UO_Deref; break; |
| 8864 | case tok::plus: Opc = UO_Plus; break; |
| 8865 | case tok::minus: Opc = UO_Minus; break; |
| 8866 | case tok::tilde: Opc = UO_Not; break; |
| 8867 | case tok::exclaim: Opc = UO_LNot; break; |
| 8868 | case tok::kw___real: Opc = UO_Real; break; |
| 8869 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8870 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8871 | } |
| 8872 | return Opc; |
| 8873 | } |
| 8874 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8875 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8876 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8877 | /// suppressed in the event of macro expansions. |
| 8878 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8879 | SourceLocation OpLoc) { |
| 8880 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8881 | return; |
| 8882 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8883 | return; |
| 8884 | lhs = lhs->IgnoreParenImpCasts(); |
| 8885 | rhs = rhs->IgnoreParenImpCasts(); |
| 8886 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8887 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8888 | if (!LeftDeclRef || !RightDeclRef || |
| 8889 | LeftDeclRef->getLocation().isMacroID() || |
| 8890 | RightDeclRef->getLocation().isMacroID()) |
| 8891 | return; |
| 8892 | const ValueDecl *LeftDecl = |
| 8893 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8894 | const ValueDecl *RightDecl = |
| 8895 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8896 | if (LeftDecl != RightDecl) |
| 8897 | return; |
| 8898 | if (LeftDecl->getType().isVolatileQualified()) |
| 8899 | return; |
| 8900 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8901 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8902 | return; |
| 8903 | |
| 8904 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8905 | << LeftDeclRef->getType() |
| 8906 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8907 | } |
| 8908 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8909 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8910 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8911 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8912 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8913 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8914 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8915 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8916 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8917 | // The following two variables are used for compound assignment operators |
| 8918 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8919 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8920 | ExprValueKind VK = VK_RValue; |
| 8921 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8922 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8923 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8924 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8925 | // But since an assignment can trigger target based overload, exclude it in |
| 8926 | // our blind search. i.e: |
| 8927 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8928 | // f<int> == 0; // resolve f<int> blindly |
| 8929 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8930 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8931 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8932 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8933 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8934 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8935 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8936 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8937 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8938 | } |
| 8939 | |
Eli Friedman | ed3b256 | 2011-06-17 20:52:22 +0000 | [diff] [blame] | 8940 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 8941 | // but we use a bit of a hack here for speed; this is a relatively |
| 8942 | // hot path, and isNullPointerConstant is slow. |
| 8943 | bool LeftNull = isa<GNUNullExpr>(lhs.get()->IgnoreParenImpCasts()); |
| 8944 | bool RightNull = isa<GNUNullExpr>(rhs.get()->IgnoreParenImpCasts()); |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 8945 | |
| 8946 | // Detect when a NULL constant is used improperly in an expression. These |
| 8947 | // are mainly cases where the null pointer is used as an integer instead |
| 8948 | // of a pointer. |
| 8949 | if (LeftNull || RightNull) { |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 8950 | // Avoid analyzing cases where the result will either be invalid (and |
| 8951 | // diagnosed as such) or entirely valid and not something to warn about. |
| 8952 | QualType LeftType = lhs.get()->getType(); |
| 8953 | QualType RightType = rhs.get()->getType(); |
| 8954 | if (!LeftType->isBlockPointerType() && !LeftType->isMemberPointerType() && |
| 8955 | !LeftType->isFunctionType() && |
| 8956 | !RightType->isBlockPointerType() && |
| 8957 | !RightType->isMemberPointerType() && |
| 8958 | !RightType->isFunctionType()) { |
| 8959 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 8960 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 8961 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 8962 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 8963 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 8964 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) { |
| 8965 | // These are the operations that would not make sense with a null pointer |
| 8966 | // no matter what the other expression is. |
Chandler Carruth | 2af68e4 | 2011-06-19 09:05:14 +0000 | [diff] [blame] | 8967 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 8968 | << (LeftNull ? lhs.get()->getSourceRange() : SourceRange()) |
| 8969 | << (RightNull ? rhs.get()->getSourceRange() : SourceRange()); |
| 8970 | } else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 8971 | Opc == BO_EQ || Opc == BO_NE) { |
| 8972 | // These are the operations that would not make sense with a null pointer |
| 8973 | // if the other expression the other expression is not a pointer. |
| 8974 | if (LeftNull != RightNull && |
| 8975 | !LeftType->isAnyPointerType() && |
| 8976 | !LeftType->canDecayToPointerType() && |
| 8977 | !RightType->isAnyPointerType() && |
| 8978 | !RightType->canDecayToPointerType()) { |
| 8979 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8980 | << (LeftNull ? lhs.get()->getSourceRange() |
| 8981 | : rhs.get()->getSourceRange()); |
| 8982 | } |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 8983 | } |
| 8984 | } |
| 8985 | } |
| 8986 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8987 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8988 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8989 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8990 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8991 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8992 | VK = lhs.get()->getValueKind(); |
| 8993 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8994 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8995 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8996 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8997 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8998 | case BO_PtrMemD: |
| 8999 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9000 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9001 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 9002 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9003 | case BO_Mul: |
| 9004 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 9005 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9006 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9007 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9008 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9009 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 9010 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9011 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9012 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 9013 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9014 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9015 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 9016 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9017 | case BO_Shl: |
| 9018 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 9019 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9020 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9021 | case BO_LE: |
| 9022 | case BO_LT: |
| 9023 | case BO_GE: |
| 9024 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 9025 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9026 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9027 | case BO_EQ: |
| 9028 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 9029 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9030 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9031 | case BO_And: |
| 9032 | case BO_Xor: |
| 9033 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9034 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 9035 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9036 | case BO_LAnd: |
| 9037 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 9038 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9039 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9040 | case BO_MulAssign: |
| 9041 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 9042 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9043 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9044 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9045 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9046 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9047 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9048 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9049 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 9050 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9051 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9052 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9053 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9054 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9055 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9056 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9057 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9058 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9059 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9060 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9061 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9062 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9063 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9064 | case BO_ShlAssign: |
| 9065 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 9066 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9067 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9068 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9069 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9070 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9071 | case BO_AndAssign: |
| 9072 | case BO_XorAssign: |
| 9073 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9074 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 9075 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9076 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9077 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9078 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9079 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9080 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9081 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 9082 | VK = rhs.get()->getValueKind(); |
| 9083 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9084 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9085 | break; |
| 9086 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9087 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9088 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9089 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9090 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 9091 | ResultTy, VK, OK, OpLoc)); |
| 9092 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9093 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9094 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9095 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9096 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 9097 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9098 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9099 | } |
| 9100 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9101 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 9102 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 9103 | /// comparison operators have higher precedence. The most typical example of |
| 9104 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9105 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9106 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9107 | typedef BinaryOperator BinOp; |
| 9108 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 9109 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 9110 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9111 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9112 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9113 | rhsopc = BO->getOpcode(); |
| 9114 | |
| 9115 | // Subs are not binary operators. |
| 9116 | if (lhsopc == -1 && rhsopc == -1) |
| 9117 | return; |
| 9118 | |
| 9119 | // Bitwise operations are sometimes used as eager logical ops. |
| 9120 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9121 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 9122 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9123 | return; |
| 9124 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9125 | if (BinOp::isComparisonOp(lhsopc)) { |
| 9126 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 9127 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 9128 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 9129 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9130 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 9131 | << BinOp::getOpcodeStr(lhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9132 | lhs->getSourceRange()); |
| 9133 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9134 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 9135 | << BinOp::getOpcodeStr(Opc), |
| 9136 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9137 | } else if (BinOp::isComparisonOp(rhsopc)) { |
| 9138 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 9139 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 9140 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 9141 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9142 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 9143 | << BinOp::getOpcodeStr(rhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9144 | rhs->getSourceRange()); |
| 9145 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9146 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 9147 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | b27c7a1 | 2011-06-22 18:41:08 +0000 | [diff] [blame] | 9148 | SourceRange(lhs->getLocStart(), |
| 9149 | cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9150 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9151 | } |
| 9152 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 9153 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 9154 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 9155 | /// in parentheses. |
| 9156 | static void |
| 9157 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 9158 | BinaryOperator *Bop) { |
| 9159 | assert(Bop->getOpcode() == BO_And); |
| 9160 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 9161 | << Bop->getSourceRange() << OpLoc; |
| 9162 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 9163 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 9164 | Bop->getSourceRange()); |
| 9165 | } |
| 9166 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9167 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 9168 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 9169 | /// in parentheses. |
| 9170 | static void |
| 9171 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9172 | BinaryOperator *Bop) { |
| 9173 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9174 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 9175 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9176 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9177 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9178 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9179 | } |
| 9180 | |
| 9181 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9182 | /// 'true'. |
| 9183 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 9184 | bool Res; |
| 9185 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 9186 | } |
| 9187 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9188 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9189 | /// 'false'. |
| 9190 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 9191 | bool Res; |
| 9192 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 9193 | } |
| 9194 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9195 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 9196 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9197 | Expr *OrLHS, Expr *OrRHS) { |
| 9198 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9199 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9200 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 9201 | if (EvaluatesAsFalse(S, OrRHS)) |
| 9202 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9203 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 9204 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 9205 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 9206 | } else if (Bop->getOpcode() == BO_LOr) { |
| 9207 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 9208 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 9209 | // "a || b && 1", but warn now. |
| 9210 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 9211 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 9212 | } |
| 9213 | } |
| 9214 | } |
| 9215 | } |
| 9216 | |
| 9217 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 9218 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9219 | Expr *OrLHS, Expr *OrRHS) { |
| 9220 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9221 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9222 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 9223 | if (EvaluatesAsFalse(S, OrLHS)) |
| 9224 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9225 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 9226 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 9227 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9228 | } |
| 9229 | } |
| 9230 | } |
| 9231 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 9232 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 9233 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 9234 | Expr *OrArg) { |
| 9235 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 9236 | if (Bop->getOpcode() == BO_And) |
| 9237 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 9238 | } |
| 9239 | } |
| 9240 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9241 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9242 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9243 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9244 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9245 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9246 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 9247 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 9248 | |
| 9249 | // Diagnose "arg1 & arg2 | arg3" |
| 9250 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| 9251 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, lhs); |
| 9252 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, rhs); |
| 9253 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9254 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9255 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 9256 | // 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] | 9257 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9258 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 9259 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9260 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9261 | } |
| 9262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9263 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9264 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9265 | tok::TokenKind Kind, |
| 9266 | Expr *lhs, Expr *rhs) { |
| 9267 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 9268 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 9269 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9270 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9271 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 9272 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 9273 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9274 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 9275 | } |
| 9276 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9277 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9278 | BinaryOperatorKind Opc, |
| 9279 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9280 | if (getLangOptions().CPlusPlus) { |
| 9281 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9282 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9283 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 9284 | UseBuiltinOperator = false; |
| 9285 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 9286 | UseBuiltinOperator = true; |
| 9287 | } else { |
| 9288 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 9289 | !rhs->getType()->isOverloadableType(); |
| 9290 | } |
| 9291 | |
| 9292 | if (!UseBuiltinOperator) { |
| 9293 | // Find all of the overloaded operators visible from this |
| 9294 | // point. We perform both an operator-name lookup from the local |
| 9295 | // scope and an argument-dependent lookup based on the types of |
| 9296 | // the arguments. |
| 9297 | UnresolvedSet<16> Functions; |
| 9298 | OverloadedOperatorKind OverOp |
| 9299 | = BinaryOperator::getOverloadedOperator(Opc); |
| 9300 | if (S && OverOp != OO_None) |
| 9301 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 9302 | Functions); |
| 9303 | |
| 9304 | // Build the (potentially-overloaded, potentially-dependent) |
| 9305 | // binary operation. |
| 9306 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 9307 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9308 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9309 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9310 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9311 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9312 | } |
| 9313 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9314 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 9315 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9316 | Expr *InputExpr) { |
| 9317 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9318 | ExprValueKind VK = VK_RValue; |
| 9319 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9320 | QualType resultType; |
| 9321 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9322 | case UO_PreInc: |
| 9323 | case UO_PreDec: |
| 9324 | case UO_PostInc: |
| 9325 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9326 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9327 | Opc == UO_PreInc || |
| 9328 | Opc == UO_PostInc, |
| 9329 | Opc == UO_PreInc || |
| 9330 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9331 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9332 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9333 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9334 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9335 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9336 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9337 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9338 | Input = move(resolved); |
| 9339 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9340 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9341 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9342 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9343 | case UO_Plus: |
| 9344 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9345 | Input = UsualUnaryConversions(Input.take()); |
| 9346 | if (Input.isInvalid()) return ExprError(); |
| 9347 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9348 | if (resultType->isDependentType()) |
| 9349 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 9350 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 9351 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9352 | break; |
| 9353 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 9354 | resultType->isEnumeralType()) |
| 9355 | break; |
| 9356 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9357 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9358 | resultType->isPointerType()) |
| 9359 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9360 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9361 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9362 | if (Input.isInvalid()) return ExprError(); |
| 9363 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9364 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9365 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9366 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9367 | << resultType << Input.get()->getSourceRange()); |
| 9368 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9369 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9370 | Input = UsualUnaryConversions(Input.take()); |
| 9371 | if (Input.isInvalid()) return ExprError(); |
| 9372 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9373 | if (resultType->isDependentType()) |
| 9374 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 9375 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 9376 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 9377 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 9378 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9379 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9380 | else if (resultType->hasIntegerRepresentation()) |
| 9381 | break; |
| 9382 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9383 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9384 | if (Input.isInvalid()) return ExprError(); |
| 9385 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9386 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9387 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9388 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9389 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9390 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9391 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9392 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9393 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9394 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9395 | if (Input.isInvalid()) return ExprError(); |
| 9396 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9397 | if (resultType->isDependentType()) |
| 9398 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9399 | if (resultType->isScalarType()) { |
| 9400 | // C99 6.5.3.3p1: ok, fallthrough; |
| 9401 | if (Context.getLangOptions().CPlusPlus) { |
| 9402 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 9403 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9404 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 9405 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9406 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9407 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9408 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9409 | if (Input.isInvalid()) return ExprError(); |
| 9410 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9411 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9412 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9413 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9414 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 9415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9416 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9417 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 9418 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9419 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9420 | case UO_Real: |
| 9421 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9422 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9423 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9424 | if (Input.isInvalid()) return ExprError(); |
| 9425 | if (Input.get()->getValueKind() != VK_RValue && |
| 9426 | Input.get()->getObjectKind() == OK_Ordinary) |
| 9427 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 9428 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9429 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9430 | resultType = Input.get()->getType(); |
| 9431 | VK = Input.get()->getValueKind(); |
| 9432 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9433 | break; |
| 9434 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9435 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9436 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9437 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9438 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9439 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9442 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9443 | UnaryOperatorKind Opc, |
| 9444 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 9445 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 9446 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9447 | // Find all of the overloaded operators visible from this |
| 9448 | // point. We perform both an operator-name lookup from the local |
| 9449 | // scope and an argument-dependent lookup based on the types of |
| 9450 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9451 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9452 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9453 | if (S && OverOp != OO_None) |
| 9454 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9455 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9456 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9457 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9458 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9459 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9460 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9461 | } |
| 9462 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9463 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9464 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9465 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9466 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9467 | } |
| 9468 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9469 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9470 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9471 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9472 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9473 | // 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] | 9474 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9475 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9476 | } |
| 9477 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9478 | /// Given the last statement in a statement-expression, check whether |
| 9479 | /// the result is a producing expression (like a call to an |
| 9480 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 9481 | /// release out of the full-expression. Otherwise, return null. |
| 9482 | /// Cannot fail. |
| 9483 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 9484 | // Should always be wrapped with one of these. |
| 9485 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 9486 | if (!cleanups) return 0; |
| 9487 | |
| 9488 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 9489 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 9490 | return 0; |
| 9491 | |
| 9492 | // Splice out the cast. This shouldn't modify any interesting |
| 9493 | // features of the statement. |
| 9494 | Expr *producer = cast->getSubExpr(); |
| 9495 | assert(producer->getType() == cast->getType()); |
| 9496 | assert(producer->getValueKind() == cast->getValueKind()); |
| 9497 | cleanups->setSubExpr(producer); |
| 9498 | return cleanups; |
| 9499 | } |
| 9500 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9501 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9502 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9503 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9504 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9505 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9506 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9507 | bool isFileScope |
| 9508 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9509 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9510 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9511 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9512 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9513 | // example, it is not possible to goto into a stmt expression apparently. |
| 9514 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9515 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9516 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9517 | // as the type of the stmtexpr. |
| 9518 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9519 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9520 | if (!Compound->body_empty()) { |
| 9521 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9522 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9523 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9524 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9525 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9526 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9527 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9528 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9529 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9530 | // Do function/array conversion on the last expression, but not |
| 9531 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9532 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9533 | if (LastExpr.isInvalid()) |
| 9534 | return ExprError(); |
| 9535 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9536 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9537 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9538 | // In ARC, if the final expression ends in a consume, splice |
| 9539 | // the consume out and bind it later. In the alternate case |
| 9540 | // (when dealing with a retainable type), the result |
| 9541 | // initialization will create a produce. In both cases the |
| 9542 | // result will be +1, and we'll need to balance that out with |
| 9543 | // a bind. |
| 9544 | if (Expr *rebuiltLastStmt |
| 9545 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 9546 | LastExpr = rebuiltLastStmt; |
| 9547 | } else { |
| 9548 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9549 | InitializedEntity::InitializeResult(LPLoc, |
| 9550 | Ty, |
| 9551 | false), |
| 9552 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9553 | LastExpr); |
| 9554 | } |
| 9555 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9556 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9557 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9558 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9559 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9560 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9561 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9562 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9563 | StmtExprMayBindToTemp = true; |
| 9564 | } |
| 9565 | } |
| 9566 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9567 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9568 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9569 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9570 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9571 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9572 | if (StmtExprMayBindToTemp) |
| 9573 | return MaybeBindToTemporary(ResStmtExpr); |
| 9574 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9575 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9576 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9577 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9578 | TypeSourceInfo *TInfo, |
| 9579 | OffsetOfComponent *CompPtr, |
| 9580 | unsigned NumComponents, |
| 9581 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9582 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9583 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9584 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9585 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9586 | // We must have at least one component that refers to the type, and the first |
| 9587 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9588 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9589 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9590 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9591 | << ArgTy << TypeRange); |
| 9592 | |
| 9593 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9594 | // with an incomplete type would be ill-formed. |
| 9595 | if (!Dependent |
| 9596 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 9597 | PDiag(diag::err_offsetof_incomplete_type) |
| 9598 | << TypeRange)) |
| 9599 | return ExprError(); |
| 9600 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9601 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9602 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9603 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9604 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9605 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9606 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9607 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9608 | |
| 9609 | bool DidWarnAboutNonPOD = false; |
| 9610 | QualType CurrentType = ArgTy; |
| 9611 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9612 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9613 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9614 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9615 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9616 | if (OC.isBrackets) { |
| 9617 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9618 | if (!CurrentType->isDependentType()) { |
| 9619 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9620 | if(!AT) |
| 9621 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9622 | << CurrentType); |
| 9623 | CurrentType = AT->getElementType(); |
| 9624 | } else |
| 9625 | CurrentType = Context.DependentTy; |
| 9626 | |
| 9627 | // The expression must be an integral expression. |
| 9628 | // FIXME: An integral constant expression? |
| 9629 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9630 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9631 | !Idx->getType()->isIntegerType()) |
| 9632 | return ExprError(Diag(Idx->getLocStart(), |
| 9633 | diag::err_typecheck_subscript_not_integer) |
| 9634 | << Idx->getSourceRange()); |
| 9635 | |
| 9636 | // Record this array index. |
| 9637 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9638 | Exprs.push_back(Idx); |
| 9639 | continue; |
| 9640 | } |
| 9641 | |
| 9642 | // Offset of a field. |
| 9643 | if (CurrentType->isDependentType()) { |
| 9644 | // We have the offset of a field, but we can't look into the dependent |
| 9645 | // type. Just record the identifier of the field. |
| 9646 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9647 | CurrentType = Context.DependentTy; |
| 9648 | continue; |
| 9649 | } |
| 9650 | |
| 9651 | // We need to have a complete type to look into. |
| 9652 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9653 | diag::err_offsetof_incomplete_type)) |
| 9654 | return ExprError(); |
| 9655 | |
| 9656 | // Look for the designated field. |
| 9657 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9658 | if (!RC) |
| 9659 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9660 | << CurrentType); |
| 9661 | RecordDecl *RD = RC->getDecl(); |
| 9662 | |
| 9663 | // C++ [lib.support.types]p5: |
| 9664 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9665 | // International Standard. type shall be a POD structure or a POD union |
| 9666 | // (clause 9). |
| 9667 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9668 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9669 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9670 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9671 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9672 | << CurrentType)) |
| 9673 | DidWarnAboutNonPOD = true; |
| 9674 | } |
| 9675 | |
| 9676 | // Look for the field. |
| 9677 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9678 | LookupQualifiedName(R, RD); |
| 9679 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9680 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9681 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9682 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9683 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9684 | } |
| 9685 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9686 | if (!MemberDecl) |
| 9687 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9688 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9689 | OC.LocEnd)); |
| 9690 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9691 | // C99 7.17p3: |
| 9692 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9693 | // |
| 9694 | // We diagnose this as an error. |
| 9695 | if (MemberDecl->getBitWidth()) { |
| 9696 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9697 | << MemberDecl->getDeclName() |
| 9698 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9699 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9700 | return ExprError(); |
| 9701 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9702 | |
| 9703 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9704 | if (IndirectMemberDecl) |
| 9705 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9706 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9707 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9708 | // the base class indirections. |
| 9709 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9710 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9711 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9712 | CXXBasePath &Path = Paths.front(); |
| 9713 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9714 | B != BEnd; ++B) |
| 9715 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9716 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9717 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9718 | if (IndirectMemberDecl) { |
| 9719 | for (IndirectFieldDecl::chain_iterator FI = |
| 9720 | IndirectMemberDecl->chain_begin(), |
| 9721 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9722 | assert(isa<FieldDecl>(*FI)); |
| 9723 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9724 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9725 | } |
| 9726 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9727 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9728 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9729 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9730 | } |
| 9731 | |
| 9732 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9733 | TInfo, Comps.data(), Comps.size(), |
| 9734 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9735 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9736 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9737 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9738 | SourceLocation BuiltinLoc, |
| 9739 | SourceLocation TypeLoc, |
| 9740 | ParsedType argty, |
| 9741 | OffsetOfComponent *CompPtr, |
| 9742 | unsigned NumComponents, |
| 9743 | SourceLocation RPLoc) { |
| 9744 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9745 | TypeSourceInfo *ArgTInfo; |
| 9746 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9747 | if (ArgTy.isNull()) |
| 9748 | return ExprError(); |
| 9749 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9750 | if (!ArgTInfo) |
| 9751 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9752 | |
| 9753 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9754 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9755 | } |
| 9756 | |
| 9757 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9758 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9759 | Expr *CondExpr, |
| 9760 | Expr *LHSExpr, Expr *RHSExpr, |
| 9761 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9762 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9763 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9764 | ExprValueKind VK = VK_RValue; |
| 9765 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9766 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9767 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9768 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9769 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9770 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9771 | } else { |
| 9772 | // The conditional expression is required to be a constant expression. |
| 9773 | llvm::APSInt condEval(32); |
| 9774 | SourceLocation ExpLoc; |
| 9775 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9776 | return ExprError(Diag(ExpLoc, |
| 9777 | diag::err_typecheck_choose_expr_requires_constant) |
| 9778 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9779 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9780 | // 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] | 9781 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9782 | |
| 9783 | resType = ActiveExpr->getType(); |
| 9784 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9785 | VK = ActiveExpr->getValueKind(); |
| 9786 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9787 | } |
| 9788 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9789 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9790 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9791 | resType->isDependentType(), |
| 9792 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9793 | } |
| 9794 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9795 | //===----------------------------------------------------------------------===// |
| 9796 | // Clang Extensions. |
| 9797 | //===----------------------------------------------------------------------===// |
| 9798 | |
| 9799 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9800 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9801 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9802 | PushBlockScope(BlockScope, Block); |
| 9803 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9804 | if (BlockScope) |
| 9805 | PushDeclContext(BlockScope, Block); |
| 9806 | else |
| 9807 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9808 | } |
| 9809 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9810 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9811 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9812 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9813 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9814 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9815 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9816 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9817 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9818 | // GetTypeForDeclarator always produces a function type for a block |
| 9819 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9820 | // unless the function was written with a typedef. |
| 9821 | assert(T->isFunctionType() && |
| 9822 | "GetTypeForDeclarator made a non-function block signature"); |
| 9823 | |
| 9824 | // Look for an explicit signature in that function type. |
| 9825 | FunctionProtoTypeLoc ExplicitSignature; |
| 9826 | |
| 9827 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9828 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9829 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9830 | |
| 9831 | // Check whether that explicit signature was synthesized by |
| 9832 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9833 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9834 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9835 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9836 | // This would be much cheaper if we stored TypeLocs instead of |
| 9837 | // TypeSourceInfos. |
| 9838 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9839 | unsigned Size = Result.getFullDataSize(); |
| 9840 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9841 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9842 | |
| 9843 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9844 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9845 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9846 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9847 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9848 | CurBlock->FunctionType = T; |
| 9849 | |
| 9850 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9851 | QualType RetTy = Fn->getResultType(); |
| 9852 | bool isVariadic = |
| 9853 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9854 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9855 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9856 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9857 | // Don't allow returning a objc interface by value. |
| 9858 | if (RetTy->isObjCObjectType()) { |
| 9859 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9860 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9861 | return; |
| 9862 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9863 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9864 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9865 | // return type. TODO: what should we do with declarators like: |
| 9866 | // ^ * { ... } |
| 9867 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9868 | if (RetTy != Context.DependentTy) |
| 9869 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9870 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9871 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9872 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9873 | if (ExplicitSignature) { |
| 9874 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9875 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9876 | if (Param->getIdentifier() == 0 && |
| 9877 | !Param->isImplicit() && |
| 9878 | !Param->isInvalidDecl() && |
| 9879 | !getLangOptions().CPlusPlus) |
| 9880 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9881 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9882 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9883 | |
| 9884 | // Fake up parameter variables if we have a typedef, like |
| 9885 | // ^ fntype { ... } |
| 9886 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9887 | for (FunctionProtoType::arg_type_iterator |
| 9888 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9889 | ParmVarDecl *Param = |
| 9890 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9891 | ParamInfo.getSourceRange().getBegin(), |
| 9892 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9893 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9894 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9895 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9896 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9897 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9898 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9899 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9900 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9901 | CurBlock->TheDecl->param_end(), |
| 9902 | /*CheckParameterNames=*/false); |
| 9903 | } |
| 9904 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9905 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9906 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9907 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9908 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9909 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9910 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9911 | // FIXME: remove the attribute. |
| 9912 | } |
| 9913 | |
| 9914 | // Put the parameter variables in scope. We can bail out immediately |
| 9915 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9916 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9917 | return; |
| 9918 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9919 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9920 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9921 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9922 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9923 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9924 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9925 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9926 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9927 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9928 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9929 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9930 | } |
| 9931 | |
| 9932 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9933 | /// is invoked to pop the information about the block from the action impl. |
| 9934 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9935 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9936 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9937 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9938 | } |
| 9939 | |
| 9940 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9941 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9942 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9943 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9944 | // If blocks are disabled, emit an error. |
| 9945 | if (!LangOpts.Blocks) |
| 9946 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9947 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9948 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9949 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9950 | PopDeclContext(); |
| 9951 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9952 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9953 | if (!BSI->ReturnType.isNull()) |
| 9954 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9955 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9956 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9957 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9958 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9959 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9960 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9961 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9962 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9963 | // If the user wrote a function type in some form, try to use that. |
| 9964 | if (!BSI->FunctionType.isNull()) { |
| 9965 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9966 | |
| 9967 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9968 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9969 | |
| 9970 | // Turn protoless block types into nullary block types. |
| 9971 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9972 | FunctionProtoType::ExtProtoInfo EPI; |
| 9973 | EPI.ExtInfo = Ext; |
| 9974 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9975 | |
| 9976 | // Otherwise, if we don't need to change anything about the function type, |
| 9977 | // preserve its sugar structure. |
| 9978 | } else if (FTy->getResultType() == RetTy && |
| 9979 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9980 | BlockTy = BSI->FunctionType; |
| 9981 | |
| 9982 | // Otherwise, make the minimal modifications to the function type. |
| 9983 | } else { |
| 9984 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9985 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9986 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9987 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9988 | BlockTy = Context.getFunctionType(RetTy, |
| 9989 | FPT->arg_type_begin(), |
| 9990 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9991 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9992 | } |
| 9993 | |
| 9994 | // If we don't have a function type, just build one from nothing. |
| 9995 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9996 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9997 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9998 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9999 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10000 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 10001 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 10002 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 10003 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10004 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 10005 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10006 | if (getCurFunction()->NeedsScopeChecking() && |
| 10007 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10008 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10009 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 10010 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10011 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 10012 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 10013 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 10014 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 10015 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 10016 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 10017 | } |
| 10018 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10019 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 10020 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 10021 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 10022 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 10023 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10024 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 10025 | } |
| 10026 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10027 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 10028 | Expr *E, TypeSourceInfo *TInfo, |
| 10029 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 10030 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10031 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 10032 | // Get the va_list type |
| 10033 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10034 | if (VaListType->isArrayType()) { |
| 10035 | // Deal with implicit array decay; for example, on x86-64, |
| 10036 | // va_list is an array, but it's supposed to decay to |
| 10037 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 10038 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10039 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10040 | ExprResult Result = UsualUnaryConversions(E); |
| 10041 | if (Result.isInvalid()) |
| 10042 | return ExprError(); |
| 10043 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10044 | } else { |
| 10045 | // Otherwise, the va_list argument must be an l-value because |
| 10046 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10047 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 10048 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10049 | return ExprError(); |
| 10050 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 10051 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 10052 | if (!E->isTypeDependent() && |
| 10053 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 10054 | return ExprError(Diag(E->getLocStart(), |
| 10055 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 10056 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 10057 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10058 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10059 | if (!TInfo->getType()->isDependentType()) { |
| 10060 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 10061 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 10062 | << TInfo->getTypeLoc().getSourceRange())) |
| 10063 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 10064 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10065 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 10066 | TInfo->getType(), |
| 10067 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 10068 | << TInfo->getTypeLoc().getSourceRange())) |
| 10069 | return ExprError(); |
| 10070 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10071 | if (!TInfo->getType().isPODType(Context)) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10072 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 10073 | diag::warn_second_parameter_to_va_arg_not_pod) |
| 10074 | << TInfo->getType() |
| 10075 | << TInfo->getTypeLoc().getSourceRange(); |
| 10076 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10077 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 10078 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 10079 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 10080 | } |
| 10081 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10082 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10083 | // The type of __null will be int or long, depending on the size of |
| 10084 | // pointers on the target. |
| 10085 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10086 | unsigned pw = Context.Target.getPointerWidth(0); |
| 10087 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10088 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10089 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10090 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10091 | else if (pw == Context.Target.getLongLongWidth()) |
| 10092 | Ty = Context.LongLongTy; |
| 10093 | else { |
| 10094 | assert(!"I don't know size of pointer!"); |
| 10095 | Ty = Context.IntTy; |
| 10096 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10097 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 10098 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10099 | } |
| 10100 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10101 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10102 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10103 | if (!SemaRef.getLangOptions().ObjC1) |
| 10104 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10105 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10106 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 10107 | if (!PT) |
| 10108 | return; |
| 10109 | |
| 10110 | // Check if the destination is of type 'id'. |
| 10111 | if (!PT->isObjCIdType()) { |
| 10112 | // Check if the destination is the 'NSString' interface. |
| 10113 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 10114 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 10115 | return; |
| 10116 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10117 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10118 | // Strip off any parens and casts. |
| 10119 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 10120 | if (!SL || SL->isWide()) |
| 10121 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10122 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10123 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10124 | } |
| 10125 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10126 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 10127 | SourceLocation Loc, |
| 10128 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10129 | Expr *SrcExpr, AssignmentAction Action, |
| 10130 | bool *Complained) { |
| 10131 | if (Complained) |
| 10132 | *Complained = false; |
| 10133 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10134 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10135 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10136 | bool isInvalid = false; |
| 10137 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10138 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10139 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10140 | switch (ConvTy) { |
| 10141 | default: assert(0 && "Unknown conversion type"); |
| 10142 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 10143 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10144 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 10145 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 10146 | case IntToPointer: |
| 10147 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 10148 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10149 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10150 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10151 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10152 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 10153 | SrcType->isObjCObjectPointerType(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10154 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 10155 | case IncompatiblePointerSign: |
| 10156 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 10157 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10158 | case FunctionVoidPointer: |
| 10159 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 10160 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10161 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 10162 | // Perform array-to-pointer decay if necessary. |
| 10163 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 10164 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10165 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 10166 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 10167 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 10168 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 10169 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10170 | |
| 10171 | |
| 10172 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
| 10173 | DiagKind = diag::err_typecheck_incompatible_lifetime; |
| 10174 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10175 | } |
| 10176 | |
| 10177 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 10178 | // fallthrough |
| 10179 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10180 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10181 | // If the qualifiers lost were because we were applying the |
| 10182 | // (deprecated) C++ conversion from a string literal to a char* |
| 10183 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 10184 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 10185 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10186 | // bit of refactoring (so that the second argument is an |
| 10187 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 10188 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10189 | // C++ semantics. |
| 10190 | if (getLangOptions().CPlusPlus && |
| 10191 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 10192 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10193 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 10194 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 10195 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 10196 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 10197 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 10198 | case IntToBlockPointer: |
| 10199 | DiagKind = diag::err_int_to_block_pointer; |
| 10200 | break; |
| 10201 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 10202 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 10203 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 10204 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10205 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 10206 | // it can give a more specific diagnostic. |
| 10207 | DiagKind = diag::warn_incompatible_qualified_id; |
| 10208 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 10209 | case IncompatibleVectors: |
| 10210 | DiagKind = diag::warn_incompatible_vectors; |
| 10211 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10212 | case Incompatible: |
| 10213 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 10214 | isInvalid = true; |
| 10215 | break; |
| 10216 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10217 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10218 | QualType FirstType, SecondType; |
| 10219 | switch (Action) { |
| 10220 | case AA_Assigning: |
| 10221 | case AA_Initializing: |
| 10222 | // The destination type comes first. |
| 10223 | FirstType = DstType; |
| 10224 | SecondType = SrcType; |
| 10225 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10226 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10227 | case AA_Returning: |
| 10228 | case AA_Passing: |
| 10229 | case AA_Converting: |
| 10230 | case AA_Sending: |
| 10231 | case AA_Casting: |
| 10232 | // The source type comes first. |
| 10233 | FirstType = SrcType; |
| 10234 | SecondType = DstType; |
| 10235 | break; |
| 10236 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10237 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10238 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10239 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10240 | if (CheckInferredResultType) |
| 10241 | EmitRelatedResultTypeNote(SrcExpr); |
| 10242 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10243 | if (Complained) |
| 10244 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10245 | return isInvalid; |
| 10246 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10247 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 10248 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10249 | llvm::APSInt ICEResult; |
| 10250 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 10251 | if (Result) |
| 10252 | *Result = ICEResult; |
| 10253 | return false; |
| 10254 | } |
| 10255 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10256 | Expr::EvalResult EvalResult; |
| 10257 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10258 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10259 | EvalResult.HasSideEffects) { |
| 10260 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 10261 | |
| 10262 | if (EvalResult.Diag) { |
| 10263 | // We only show the note if it's not the usual "invalid subexpression" |
| 10264 | // or if it's actually in a subexpression. |
| 10265 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 10266 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 10267 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 10268 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10269 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10270 | return true; |
| 10271 | } |
| 10272 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10273 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 10274 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10275 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10276 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 10277 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 10278 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10279 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10280 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10281 | if (Result) |
| 10282 | *Result = EvalResult.Val.getInt(); |
| 10283 | return false; |
| 10284 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10285 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10286 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10287 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10288 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10289 | ExpressionEvaluationContextRecord(NewContext, |
| 10290 | ExprTemporaries.size(), |
| 10291 | ExprNeedsCleanups)); |
| 10292 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10293 | } |
| 10294 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10295 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10296 | Sema::PopExpressionEvaluationContext() { |
| 10297 | // Pop the current expression evaluation context off the stack. |
| 10298 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 10299 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10300 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10301 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 10302 | if (Rec.PotentiallyReferenced) { |
| 10303 | // Mark any remaining declarations in the current position of the stack |
| 10304 | // as "referenced". If they were not meant to be referenced, semantic |
| 10305 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10306 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10307 | I = Rec.PotentiallyReferenced->begin(), |
| 10308 | IEnd = Rec.PotentiallyReferenced->end(); |
| 10309 | I != IEnd; ++I) |
| 10310 | MarkDeclarationReferenced(I->first, I->second); |
| 10311 | } |
| 10312 | |
| 10313 | if (Rec.PotentiallyDiagnosed) { |
| 10314 | // Emit any pending diagnostics. |
| 10315 | for (PotentiallyEmittedDiagnostics::iterator |
| 10316 | I = Rec.PotentiallyDiagnosed->begin(), |
| 10317 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 10318 | I != IEnd; ++I) |
| 10319 | Diag(I->first, I->second); |
| 10320 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10321 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10322 | |
| 10323 | // When are coming out of an unevaluated context, clear out any |
| 10324 | // temporaries that we may have created as part of the evaluation of |
| 10325 | // the expression in that context: they aren't relevant because they |
| 10326 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10327 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10328 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 10329 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10330 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 10331 | |
| 10332 | // Otherwise, merge the contexts together. |
| 10333 | } else { |
| 10334 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 10335 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10336 | |
| 10337 | // Destroy the popped expression evaluation record. |
| 10338 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10339 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10340 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10341 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 10342 | ExprTemporaries.erase( |
| 10343 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 10344 | ExprTemporaries.end()); |
| 10345 | ExprNeedsCleanups = false; |
| 10346 | } |
| 10347 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10348 | /// \brief Note that the given declaration was referenced in the source code. |
| 10349 | /// |
| 10350 | /// This routine should be invoke whenever a given declaration is referenced |
| 10351 | /// in the source code, and where that reference occurred. If this declaration |
| 10352 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 10353 | /// C99 6.9p3), then the declaration will be marked as used. |
| 10354 | /// |
| 10355 | /// \param Loc the location where the declaration was referenced. |
| 10356 | /// |
| 10357 | /// \param D the declaration that has been referenced by the source code. |
| 10358 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 10359 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10360 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 10361 | D->setReferenced(); |
| 10362 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10363 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10364 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10365 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 10366 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 10367 | // template or not. The reason for this is that unevaluated expressions |
| 10368 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 10369 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10370 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10371 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 10372 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10373 | return; |
| 10374 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10375 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10376 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 10377 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10378 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10379 | // Do not mark anything as "used" within a dependent context; wait for |
| 10380 | // an instantiation. |
| 10381 | if (CurContext->isDependentContext()) |
| 10382 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10383 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10384 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10385 | case Unevaluated: |
| 10386 | // We are in an expression that is not potentially evaluated; do nothing. |
| 10387 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10388 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10389 | case PotentiallyEvaluated: |
| 10390 | // We are in a potentially-evaluated expression, so this declaration is |
| 10391 | // "used"; handle this below. |
| 10392 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10393 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10394 | case PotentiallyPotentiallyEvaluated: |
| 10395 | // We are in an expression that may be potentially evaluated; queue this |
| 10396 | // declaration reference until we know whether the expression is |
| 10397 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10398 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10399 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10400 | |
| 10401 | case PotentiallyEvaluatedIfUsed: |
| 10402 | // Referenced declarations will only be used if the construct in the |
| 10403 | // containing expression is used. |
| 10404 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10406 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10407 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 10408 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 10409 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 10410 | if (Constructor->isTrivial()) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 10411 | return; |
| 10412 | if (!Constructor->isUsed(false)) |
| 10413 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Sean Hunt | 509f048 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 10414 | } else if (Constructor->isDefaulted() && |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10415 | Constructor->isCopyConstructor()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10416 | if (!Constructor->isUsed(false)) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10417 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10418 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10419 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10420 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10421 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10422 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10423 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10424 | if (Destructor->isVirtual()) |
| 10425 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10426 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10427 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10428 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10429 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 10430 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10431 | } else if (MethodDecl->isVirtual()) |
| 10432 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10433 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 10434 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10435 | // Recursive functions should be marked when used from another function. |
| 10436 | if (CurContext == Function) return; |
| 10437 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10438 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 10439 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 10440 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10441 | bool AlreadyInstantiated = false; |
| 10442 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10443 | = Function->getTemplateSpecializationInfo()) { |
| 10444 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10445 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10446 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10447 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10448 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10449 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10450 | = Function->getMemberSpecializationInfo()) { |
| 10451 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 10452 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10453 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10454 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10455 | AlreadyInstantiated = true; |
| 10456 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10457 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10458 | if (!AlreadyInstantiated) { |
| 10459 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 10460 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 10461 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 10462 | Loc)); |
| 10463 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10464 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10465 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10466 | } else { |
| 10467 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10468 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 10469 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 10470 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10471 | MarkDeclarationReferenced(Loc, *i); |
| 10472 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10473 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10474 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10475 | // Keep track of used but undefined functions. |
| 10476 | if (!Function->isPure() && !Function->hasBody() && |
| 10477 | Function->getLinkage() != ExternalLinkage) { |
| 10478 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 10479 | if (old.isInvalid()) old = Loc; |
| 10480 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 10481 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10482 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10483 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10485 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10486 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10487 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10488 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10489 | Var->getInstantiatedFromStaticDataMember()) { |
| 10490 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10491 | assert(MSInfo && "Missing member specialization information?"); |
| 10492 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 10493 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 10494 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 10495 | // This is a modification of an existing AST node. Notify listeners. |
| 10496 | if (ASTMutationListener *L = getASTMutationListener()) |
| 10497 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10498 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10499 | } |
| 10500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10501 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10502 | // Keep track of used but undefined variables. We make a hole in |
| 10503 | // the warning for static const data members with in-line |
| 10504 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10505 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10506 | && Var->getLinkage() != ExternalLinkage |
| 10507 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10508 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 10509 | if (old.isInvalid()) old = Loc; |
| 10510 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10511 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10512 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10513 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10514 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10515 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10516 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10517 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10518 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10519 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10520 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10521 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10522 | Sema &S; |
| 10523 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10524 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10525 | public: |
| 10526 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10527 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10528 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10529 | |
| 10530 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10531 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10532 | }; |
| 10533 | } |
| 10534 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10535 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10536 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10537 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 10538 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 10539 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10540 | |
| 10541 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10542 | } |
| 10543 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10544 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10545 | if (ClassTemplateSpecializationDecl *Spec |
| 10546 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10547 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10548 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10549 | } |
| 10550 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10551 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10552 | } |
| 10553 | |
| 10554 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10555 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10556 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10557 | } |
| 10558 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10559 | namespace { |
| 10560 | /// \brief Helper class that marks all of the declarations referenced by |
| 10561 | /// potentially-evaluated subexpressions as "referenced". |
| 10562 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10563 | Sema &S; |
| 10564 | |
| 10565 | public: |
| 10566 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10567 | |
| 10568 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 10569 | |
| 10570 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 10571 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10572 | } |
| 10573 | |
| 10574 | void VisitMemberExpr(MemberExpr *E) { |
| 10575 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10576 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10577 | } |
| 10578 | |
| 10579 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 10580 | if (E->getConstructor()) |
| 10581 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 10582 | if (E->getOperatorNew()) |
| 10583 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 10584 | if (E->getOperatorDelete()) |
| 10585 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10586 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10587 | } |
| 10588 | |
| 10589 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10590 | if (E->getOperatorDelete()) |
| 10591 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10592 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10593 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10594 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 10595 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 10596 | S.LookupDestructor(Record)); |
| 10597 | } |
| 10598 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10599 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10600 | } |
| 10601 | |
| 10602 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 10603 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10604 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10605 | } |
| 10606 | |
| 10607 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 10608 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10609 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10610 | |
| 10611 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10612 | Visit(E->getExpr()); |
| 10613 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10614 | }; |
| 10615 | } |
| 10616 | |
| 10617 | /// \brief Mark any declarations that appear within this expression or any |
| 10618 | /// potentially-evaluated subexpressions as "referenced". |
| 10619 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 10620 | EvaluatedExprMarker(*this).Visit(E); |
| 10621 | } |
| 10622 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10623 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10624 | /// of the program being compiled. |
| 10625 | /// |
| 10626 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10627 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10628 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10629 | /// expressions, code used only during overload resolution, etc., are not |
| 10630 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10631 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10632 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10633 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10634 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10635 | /// This routine should be used for all diagnostics that describe the run-time |
| 10636 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10637 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10638 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 10639 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10640 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10641 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10642 | case Unevaluated: |
| 10643 | // The argument will never be evaluated, so don't complain. |
| 10644 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10645 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10646 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10647 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10648 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 10649 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 10650 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 10651 | } |
| 10652 | else |
| 10653 | Diag(Loc, PD); |
| 10654 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10655 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10656 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10657 | case PotentiallyPotentiallyEvaluated: |
| 10658 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10659 | break; |
| 10660 | } |
| 10661 | |
| 10662 | return false; |
| 10663 | } |
| 10664 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10665 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10666 | CallExpr *CE, FunctionDecl *FD) { |
| 10667 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10668 | return false; |
| 10669 | |
| 10670 | PartialDiagnostic Note = |
| 10671 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10672 | << FD->getDeclName() : PDiag(); |
| 10673 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10674 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10675 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10676 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10677 | PDiag(diag::err_call_function_incomplete_return) |
| 10678 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10679 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10680 | << CE->getSourceRange(), |
| 10681 | std::make_pair(NoteLoc, Note))) |
| 10682 | return true; |
| 10683 | |
| 10684 | return false; |
| 10685 | } |
| 10686 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10687 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10688 | // will prevent this condition from triggering, which is what we want. |
| 10689 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10690 | SourceLocation Loc; |
| 10691 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10692 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10693 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10694 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10695 | if (isa<BinaryOperator>(E)) { |
| 10696 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10697 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10698 | return; |
| 10699 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10700 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10701 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10702 | // Greylist some idioms by putting them into a warning subcategory. |
| 10703 | if (ObjCMessageExpr *ME |
| 10704 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10705 | Selector Sel = ME->getSelector(); |
| 10706 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10707 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10708 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10709 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10710 | |
| 10711 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10712 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10713 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10714 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10715 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10716 | Loc = Op->getOperatorLoc(); |
| 10717 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10718 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10719 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10720 | return; |
| 10721 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10722 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10723 | Loc = Op->getOperatorLoc(); |
| 10724 | } else { |
| 10725 | // Not an assignment. |
| 10726 | return; |
| 10727 | } |
| 10728 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10729 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10730 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10731 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10732 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 10733 | Diag(Loc, diag::note_condition_assign_silence) |
| 10734 | << FixItHint::CreateInsertion(Open, "(") |
| 10735 | << FixItHint::CreateInsertion(Close, ")"); |
| 10736 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10737 | if (IsOrAssign) |
| 10738 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10739 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10740 | else |
| 10741 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10742 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10743 | } |
| 10744 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10745 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10746 | /// that the user intended an assignment used as condition. |
| 10747 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10748 | // Don't warn if the parens came from a macro. |
| 10749 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10750 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10751 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10752 | // Don't warn for dependent expressions. |
| 10753 | if (parenE->isTypeDependent()) |
| 10754 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10755 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10756 | Expr *E = parenE->IgnoreParens(); |
| 10757 | |
| 10758 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10759 | if (opE->getOpcode() == BO_EQ && |
| 10760 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10761 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10762 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10763 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10764 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10765 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10766 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10767 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10768 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10769 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10770 | } |
| 10771 | } |
| 10772 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10773 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10774 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10775 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10776 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10777 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10778 | ExprResult result = CheckPlaceholderExpr(E); |
| 10779 | if (result.isInvalid()) return ExprError(); |
| 10780 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10781 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10782 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10783 | if (getLangOptions().CPlusPlus) |
| 10784 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10785 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10786 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10787 | if (ERes.isInvalid()) |
| 10788 | return ExprError(); |
| 10789 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10790 | |
| 10791 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10792 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10793 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10794 | << T << E->getSourceRange(); |
| 10795 | return ExprError(); |
| 10796 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10797 | } |
| 10798 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10799 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10800 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10801 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10802 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10803 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10804 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10805 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10806 | |
| 10807 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10808 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10809 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10810 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10811 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10812 | /// to have an appropriate type. |
| 10813 | struct RebuildUnknownAnyFunction |
| 10814 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10815 | |
| 10816 | Sema &S; |
| 10817 | |
| 10818 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10819 | |
| 10820 | ExprResult VisitStmt(Stmt *S) { |
| 10821 | llvm_unreachable("unexpected statement!"); |
| 10822 | return ExprError(); |
| 10823 | } |
| 10824 | |
| 10825 | ExprResult VisitExpr(Expr *expr) { |
| 10826 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10827 | << expr->getSourceRange(); |
| 10828 | return ExprError(); |
| 10829 | } |
| 10830 | |
| 10831 | /// Rebuild an expression which simply semantically wraps another |
| 10832 | /// expression which it shares the type and value kind of. |
| 10833 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10834 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10835 | if (subResult.isInvalid()) return ExprError(); |
| 10836 | |
| 10837 | Expr *subExpr = subResult.take(); |
| 10838 | expr->setSubExpr(subExpr); |
| 10839 | expr->setType(subExpr->getType()); |
| 10840 | expr->setValueKind(subExpr->getValueKind()); |
| 10841 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10842 | return expr; |
| 10843 | } |
| 10844 | |
| 10845 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10846 | return rebuildSugarExpr(paren); |
| 10847 | } |
| 10848 | |
| 10849 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10850 | return rebuildSugarExpr(op); |
| 10851 | } |
| 10852 | |
| 10853 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10854 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10855 | if (subResult.isInvalid()) return ExprError(); |
| 10856 | |
| 10857 | Expr *subExpr = subResult.take(); |
| 10858 | op->setSubExpr(subExpr); |
| 10859 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10860 | assert(op->getValueKind() == VK_RValue); |
| 10861 | assert(op->getObjectKind() == OK_Ordinary); |
| 10862 | return op; |
| 10863 | } |
| 10864 | |
| 10865 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10866 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10867 | |
| 10868 | expr->setType(decl->getType()); |
| 10869 | |
| 10870 | assert(expr->getValueKind() == VK_RValue); |
| 10871 | if (S.getLangOptions().CPlusPlus && |
| 10872 | !(isa<CXXMethodDecl>(decl) && |
| 10873 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10874 | expr->setValueKind(VK_LValue); |
| 10875 | |
| 10876 | return expr; |
| 10877 | } |
| 10878 | |
| 10879 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10880 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10881 | } |
| 10882 | |
| 10883 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10884 | return resolveDecl(ref, ref->getDecl()); |
| 10885 | } |
| 10886 | }; |
| 10887 | } |
| 10888 | |
| 10889 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10890 | /// to have a function type. |
| 10891 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10892 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10893 | if (result.isInvalid()) return ExprError(); |
| 10894 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10895 | } |
| 10896 | |
| 10897 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10898 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10899 | /// into one which resolves the type directly on the referring |
| 10900 | /// expression. Strict preservation of the original source |
| 10901 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10902 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10903 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10904 | |
| 10905 | Sema &S; |
| 10906 | |
| 10907 | /// The current destination type. |
| 10908 | QualType DestType; |
| 10909 | |
| 10910 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10911 | : S(S), DestType(castType) {} |
| 10912 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10913 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10914 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10915 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10916 | } |
| 10917 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10918 | ExprResult VisitExpr(Expr *expr) { |
| 10919 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10920 | << expr->getSourceRange(); |
| 10921 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10922 | } |
| 10923 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10924 | ExprResult VisitCallExpr(CallExpr *call); |
| 10925 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10926 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10927 | /// Rebuild an expression which simply semantically wraps another |
| 10928 | /// expression which it shares the type and value kind of. |
| 10929 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10930 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10931 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10932 | Expr *subExpr = subResult.take(); |
| 10933 | expr->setSubExpr(subExpr); |
| 10934 | expr->setType(subExpr->getType()); |
| 10935 | expr->setValueKind(subExpr->getValueKind()); |
| 10936 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10937 | return expr; |
| 10938 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10939 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10940 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10941 | return rebuildSugarExpr(paren); |
| 10942 | } |
| 10943 | |
| 10944 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10945 | return rebuildSugarExpr(op); |
| 10946 | } |
| 10947 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10948 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10949 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10950 | if (!ptr) { |
| 10951 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10952 | << op->getSourceRange(); |
| 10953 | return ExprError(); |
| 10954 | } |
| 10955 | assert(op->getValueKind() == VK_RValue); |
| 10956 | assert(op->getObjectKind() == OK_Ordinary); |
| 10957 | op->setType(DestType); |
| 10958 | |
| 10959 | // Build the sub-expression as if it were an object of the pointee type. |
| 10960 | DestType = ptr->getPointeeType(); |
| 10961 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10962 | if (subResult.isInvalid()) return ExprError(); |
| 10963 | op->setSubExpr(subResult.take()); |
| 10964 | return op; |
| 10965 | } |
| 10966 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10967 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10968 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10969 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10970 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10971 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10972 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10973 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10974 | |
| 10975 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10976 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10977 | } |
| 10978 | }; |
| 10979 | } |
| 10980 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10981 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10982 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10983 | Expr *callee = call->getCallee(); |
| 10984 | |
| 10985 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10986 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10987 | FK_FunctionPointer, |
| 10988 | FK_BlockPointer |
| 10989 | }; |
| 10990 | |
| 10991 | FnKind kind; |
| 10992 | QualType type = callee->getType(); |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10993 | if (type == S.Context.BoundMemberTy) { |
| 10994 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10995 | kind = FK_MemberFunction; |
| 10996 | type = Expr::findBoundMemberType(callee); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10997 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10998 | type = ptr->getPointeeType(); |
| 10999 | kind = FK_FunctionPointer; |
| 11000 | } else { |
| 11001 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 11002 | kind = FK_BlockPointer; |
| 11003 | } |
| 11004 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 11005 | |
| 11006 | // Verify that this is a legal result type of a function. |
| 11007 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11008 | unsigned diagID = diag::err_func_returning_array_function; |
| 11009 | if (kind == FK_BlockPointer) |
| 11010 | diagID = diag::err_block_returning_array_function; |
| 11011 | |
| 11012 | S.Diag(call->getExprLoc(), diagID) |
| 11013 | << DestType->isFunctionType() << DestType; |
| 11014 | return ExprError(); |
| 11015 | } |
| 11016 | |
| 11017 | // Otherwise, go ahead and set DestType as the call's result. |
| 11018 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 11019 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 11020 | assert(call->getObjectKind() == OK_Ordinary); |
| 11021 | |
| 11022 | // Rebuild the function type, replacing the result type with DestType. |
| 11023 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 11024 | DestType = S.Context.getFunctionType(DestType, |
| 11025 | proto->arg_type_begin(), |
| 11026 | proto->getNumArgs(), |
| 11027 | proto->getExtProtoInfo()); |
| 11028 | else |
| 11029 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 11030 | fnType->getExtInfo()); |
| 11031 | |
| 11032 | // Rebuild the appropriate pointer-to-function type. |
| 11033 | switch (kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11034 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11035 | // Nothing to do. |
| 11036 | break; |
| 11037 | |
| 11038 | case FK_FunctionPointer: |
| 11039 | DestType = S.Context.getPointerType(DestType); |
| 11040 | break; |
| 11041 | |
| 11042 | case FK_BlockPointer: |
| 11043 | DestType = S.Context.getBlockPointerType(DestType); |
| 11044 | break; |
| 11045 | } |
| 11046 | |
| 11047 | // Finally, we can recurse. |
| 11048 | ExprResult calleeResult = Visit(callee); |
| 11049 | if (!calleeResult.isUsable()) return ExprError(); |
| 11050 | call->setCallee(calleeResult.take()); |
| 11051 | |
| 11052 | // Bind a temporary if necessary. |
| 11053 | return S.MaybeBindToTemporary(call); |
| 11054 | } |
| 11055 | |
| 11056 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11057 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 11058 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11059 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11060 | // Verify that this is a legal result type of a call. |
| 11061 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11062 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 11063 | << DestType->isFunctionType() << DestType; |
| 11064 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11065 | } |
| 11066 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11067 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 11068 | method->setResultType(DestType); |
| 11069 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11070 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11071 | msg->setType(DestType.getNonReferenceType()); |
| 11072 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11073 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11074 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11075 | } |
| 11076 | |
| 11077 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11078 | // The only case we should ever see here is a function-to-pointer decay. |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11079 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11080 | assert(ice->getValueKind() == VK_RValue); |
| 11081 | assert(ice->getObjectKind() == OK_Ordinary); |
| 11082 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11083 | ice->setType(DestType); |
| 11084 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11085 | // Rebuild the sub-expression as the pointee (function) type. |
| 11086 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 11087 | |
| 11088 | ExprResult result = Visit(ice->getSubExpr()); |
| 11089 | if (!result.isUsable()) return ExprError(); |
| 11090 | |
| 11091 | ice->setSubExpr(result.take()); |
| 11092 | return S.Owned(ice); |
| 11093 | } |
| 11094 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11095 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11096 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11097 | QualType type = DestType; |
| 11098 | |
| 11099 | // We know how to make this work for certain kinds of decls: |
| 11100 | |
| 11101 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11102 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11103 | // This is true because FunctionDecls must always have function |
| 11104 | // type, so we can't be resolving the entire thing at once. |
| 11105 | assert(type->isFunctionType()); |
| 11106 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11107 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 11108 | if (method->isInstance()) { |
| 11109 | valueKind = VK_RValue; |
| 11110 | type = S.Context.BoundMemberTy; |
| 11111 | } |
| 11112 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11113 | // Function references aren't l-values in C. |
| 11114 | if (!S.getLangOptions().CPlusPlus) |
| 11115 | valueKind = VK_RValue; |
| 11116 | |
| 11117 | // - variables |
| 11118 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11119 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 11120 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11121 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11122 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 11123 | << decl << expr->getSourceRange(); |
| 11124 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11125 | } |
| 11126 | |
| 11127 | // - nothing else |
| 11128 | } else { |
| 11129 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 11130 | << decl << expr->getSourceRange(); |
| 11131 | return ExprError(); |
| 11132 | } |
| 11133 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11134 | decl->setType(DestType); |
| 11135 | expr->setType(type); |
| 11136 | expr->setValueKind(valueKind); |
| 11137 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11138 | } |
| 11139 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11140 | /// Check a cast of an unknown-any type. We intentionally only |
| 11141 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11142 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 11143 | Expr *castExpr, CastKind &castKind, |
| 11144 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11145 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11146 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 11147 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11148 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11149 | castExpr = result.take(); |
| 11150 | VK = castExpr->getValueKind(); |
| 11151 | castKind = CK_NoOp; |
| 11152 | |
| 11153 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11154 | } |
| 11155 | |
| 11156 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 11157 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11158 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11159 | while (true) { |
| 11160 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11161 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11162 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11163 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11164 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11165 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11166 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11167 | } |
| 11168 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11169 | SourceLocation loc; |
| 11170 | NamedDecl *d; |
| 11171 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 11172 | loc = ref->getLocation(); |
| 11173 | d = ref->getDecl(); |
| 11174 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 11175 | loc = mem->getMemberLoc(); |
| 11176 | d = mem->getMemberDecl(); |
| 11177 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 11178 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11179 | loc = msg->getSelectorLoc(); |
| 11180 | d = msg->getMethodDecl(); |
| 11181 | assert(d && "unknown method returning __unknown_any?"); |
| 11182 | } else { |
| 11183 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11184 | << e->getSourceRange(); |
| 11185 | return ExprError(); |
| 11186 | } |
| 11187 | |
| 11188 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11189 | |
| 11190 | // Never recoverable. |
| 11191 | return ExprError(); |
| 11192 | } |
| 11193 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11194 | /// Check for operands with placeholder types and complain if found. |
| 11195 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 11196 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11197 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 11198 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11199 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11200 | // Overloaded expressions. |
| 11201 | if (type == Context.OverloadTy) |
| 11202 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 11203 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11204 | QualType(), |
| 11205 | diag::err_ovl_unresolvable); |
| 11206 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11207 | // Bound member functions. |
| 11208 | if (type == Context.BoundMemberTy) { |
| 11209 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 11210 | << E->getSourceRange(); |
| 11211 | return ExprError(); |
| 11212 | } |
| 11213 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11214 | // Expressions of unknown type. |
| 11215 | if (type == Context.UnknownAnyTy) |
| 11216 | return diagnoseUnknownAnyExpr(*this, E); |
| 11217 | |
| 11218 | assert(!type->isPlaceholderType()); |
| 11219 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11220 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11221 | |
| 11222 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 11223 | if (expr->isTypeDependent()) |
| 11224 | return true; |
| 11225 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 11226 | return expr->getType()->isIntegralOrEnumerationType(); |
| 11227 | return false; |
| 11228 | } |