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 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1285 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1286 | /// possibly a list of template arguments. |
| 1287 | /// |
| 1288 | /// If this produces template arguments, it is permitted to call |
| 1289 | /// DecomposeTemplateName. |
| 1290 | /// |
| 1291 | /// This actually loses a lot of source location information for |
| 1292 | /// non-standard name kinds; we should consider preserving that in |
| 1293 | /// some way. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1294 | void Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1295 | TemplateArgumentListInfo &Buffer, |
| 1296 | DeclarationNameInfo &NameInfo, |
| 1297 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1298 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1299 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1300 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1301 | |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1302 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1303 | Id.TemplateId->getTemplateArgs(), |
| 1304 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1305 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1306 | TemplateArgsPtr.release(); |
| 1307 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1308 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1309 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1310 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1311 | TemplateArgs = &Buffer; |
| 1312 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1313 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1314 | TemplateArgs = 0; |
| 1315 | } |
| 1316 | } |
| 1317 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1318 | /// Diagnose an empty lookup. |
| 1319 | /// |
| 1320 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1321 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1322 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1323 | DeclarationName Name = R.getLookupName(); |
| 1324 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1325 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1326 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1327 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1328 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1329 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1330 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1331 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1332 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1333 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1334 | // If the original lookup was an unqualified lookup, fake an |
| 1335 | // unqualified lookup. This is useful when (for example) the |
| 1336 | // original lookup would not have found something because it was a |
| 1337 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1338 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1339 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1340 | if (isa<CXXRecordDecl>(DC)) { |
| 1341 | LookupQualifiedName(R, DC); |
| 1342 | |
| 1343 | if (!R.empty()) { |
| 1344 | // Don't give errors about ambiguities in this lookup. |
| 1345 | R.suppressDiagnostics(); |
| 1346 | |
| 1347 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1348 | bool isInstance = CurMethod && |
| 1349 | CurMethod->isInstance() && |
| 1350 | DC == CurMethod->getParent(); |
| 1351 | |
| 1352 | // Give a code modification hint to insert 'this->'. |
| 1353 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1354 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1355 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1356 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1357 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1358 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1359 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1360 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1361 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1362 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1363 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1364 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1365 | R.getNameLoc(), DepThisType, false); |
| 1366 | TemplateArgumentListInfo TList; |
| 1367 | if (ULE->hasExplicitTemplateArgs()) |
| 1368 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1369 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1370 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1371 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1372 | CXXDependentScopeMemberExpr *DepExpr = |
| 1373 | CXXDependentScopeMemberExpr::Create( |
| 1374 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1375 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1376 | R.getLookupNameInfo(), &TList); |
| 1377 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1378 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1379 | // FIXME: we should be able to handle this case too. It is correct |
| 1380 | // to add this-> here. This is a workaround for PR7947. |
| 1381 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1382 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1383 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1384 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1385 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Do we really want to note all of these? |
| 1388 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1389 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1390 | |
| 1391 | // Tell the callee to try to recover. |
| 1392 | return false; |
| 1393 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1394 | |
| 1395 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1399 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1400 | TypoCorrection Corrected; |
| 1401 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
| 1402 | S, &SS, NULL, false, CTC))) { |
| 1403 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1404 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1405 | R.setLookupName(Corrected.getCorrection()); |
| 1406 | |
| 1407 | if (!Corrected.isKeyword()) { |
| 1408 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
| 1409 | R.addDecl(ND); |
| 1410 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1411 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1412 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1413 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1414 | else |
| 1415 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1416 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1417 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1418 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1419 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1420 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1421 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Tell the callee to try to recover. |
| 1424 | return false; |
| 1425 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1426 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1427 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1428 | // FIXME: If we ended up with a typo for a type name or |
| 1429 | // Objective-C class name, we're in trouble because the parser |
| 1430 | // is in the wrong place to recover. Suggest the typo |
| 1431 | // correction, but don't make it a fix-it since we're not going |
| 1432 | // to recover well anyway. |
| 1433 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1434 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1435 | else |
| 1436 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1437 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1438 | << SS.getRange(); |
| 1439 | |
| 1440 | // Don't try to recover; it won't work. |
| 1441 | return true; |
| 1442 | } |
| 1443 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1444 | // 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] | 1445 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1446 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1447 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1448 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1449 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1450 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1451 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1454 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1455 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1456 | |
| 1457 | // Emit a special diagnostic for failed member lookups. |
| 1458 | // FIXME: computing the declaration context might fail here (?) |
| 1459 | if (!SS.isEmpty()) { |
| 1460 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1461 | << Name << computeDeclContext(SS, false) |
| 1462 | << SS.getRange(); |
| 1463 | return true; |
| 1464 | } |
| 1465 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1466 | // Give up, we can't recover. |
| 1467 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1471 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1472 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1473 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1474 | if (!IDecl) |
| 1475 | return 0; |
| 1476 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1477 | if (!ClassImpDecl) |
| 1478 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1479 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1480 | if (!property) |
| 1481 | return 0; |
| 1482 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1483 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1484 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1485 | return 0; |
| 1486 | return property; |
| 1487 | } |
| 1488 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1489 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1490 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1491 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1492 | if (!IDecl) |
| 1493 | return false; |
| 1494 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1495 | if (!ClassImpDecl) |
| 1496 | return false; |
| 1497 | if (ObjCPropertyImplDecl *PIDecl |
| 1498 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1499 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1500 | PIDecl->getPropertyIvarDecl()) |
| 1501 | return false; |
| 1502 | |
| 1503 | return true; |
| 1504 | } |
| 1505 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1506 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1507 | IdentifierInfo *II, |
| 1508 | SourceLocation NameLoc) { |
| 1509 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1510 | bool LookForIvars; |
| 1511 | if (Lookup.empty()) |
| 1512 | LookForIvars = true; |
| 1513 | else if (CurMeth->isClassMethod()) |
| 1514 | LookForIvars = false; |
| 1515 | else |
| 1516 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1517 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1518 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1519 | if (!LookForIvars) |
| 1520 | return 0; |
| 1521 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1522 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1523 | if (!IDecl) |
| 1524 | return 0; |
| 1525 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1526 | if (!ClassImpDecl) |
| 1527 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1528 | bool DynamicImplSeen = false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1529 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1530 | if (!property) |
| 1531 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1532 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1533 | DynamicImplSeen = |
| 1534 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1535 | // property implementation has a designated ivar. No need to assume a new |
| 1536 | // one. |
| 1537 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1538 | return 0; |
| 1539 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1540 | if (!DynamicImplSeen) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1541 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1542 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1543 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1544 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1545 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1546 | (Expr *)0, true); |
| 1547 | ClassImpDecl->addDecl(Ivar); |
| 1548 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1549 | property->setPropertyIvarDecl(Ivar); |
| 1550 | return Ivar; |
| 1551 | } |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1555 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1556 | CXXScopeSpec &SS, |
| 1557 | UnqualifiedId &Id, |
| 1558 | bool HasTrailingLParen, |
| 1559 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1560 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1561 | "cannot be direct & operand and have a trailing lparen"); |
| 1562 | |
| 1563 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1564 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1565 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1566 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1569 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1570 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1571 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1572 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1573 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1574 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1575 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1576 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1577 | // C++ [temp.dep.expr]p3: |
| 1578 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1579 | // -- an identifier that was declared with a dependent type, |
| 1580 | // (note: handled after lookup) |
| 1581 | // -- a template-id that is dependent, |
| 1582 | // (note: handled in BuildTemplateIdExpr) |
| 1583 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1584 | // -- a nested-name-specifier that contains a class-name that |
| 1585 | // names a dependent type. |
| 1586 | // Determine whether this is a member of an unknown specialization; |
| 1587 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1588 | bool DependentID = false; |
| 1589 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1590 | Name.getCXXNameType()->isDependentType()) { |
| 1591 | DependentID = true; |
| 1592 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1593 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1594 | if (RequireCompleteDeclContext(SS, DC)) |
| 1595 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | DependentID = true; |
| 1598 | } |
| 1599 | } |
| 1600 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1601 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1602 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1603 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1604 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1605 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1606 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1607 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1608 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1609 | // Lookup the template name again to correctly establish the context in |
| 1610 | // which it was found. This is really unfortunate as we already did the |
| 1611 | // lookup to determine that it was a template name in the first place. If |
| 1612 | // this becomes a performance hit, we can work harder to preserve those |
| 1613 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1614 | bool MemberOfUnknownSpecialization; |
| 1615 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1616 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1617 | |
| 1618 | if (MemberOfUnknownSpecialization || |
| 1619 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1620 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1621 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1622 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1623 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1624 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1626 | // If the result might be in a dependent base class, this is a dependent |
| 1627 | // id-expression. |
| 1628 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1629 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1630 | TemplateArgs); |
| 1631 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1632 | // If this reference is in an Objective-C method, then we need to do |
| 1633 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1634 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1635 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1636 | if (E.isInvalid()) |
| 1637 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1639 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1640 | return Owned(Ex); |
| 1641 | |
| 1642 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1643 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1644 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1645 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1646 | if (const ObjCPropertyDecl *Property = |
| 1647 | canSynthesizeProvisionalIvar(II)) { |
| 1648 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1649 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1650 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1651 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1652 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1653 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1654 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1655 | // for further use, this must be set to false if in class method. |
| 1656 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1657 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1658 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1659 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1660 | if (R.isAmbiguous()) |
| 1661 | return ExprError(); |
| 1662 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1663 | // Determine whether this name might be a candidate for |
| 1664 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1665 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1666 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1667 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1669 | // in C90, extension in C99, forbidden in C++). |
| 1670 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1671 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1672 | if (D) R.addDecl(D); |
| 1673 | } |
| 1674 | |
| 1675 | // If this name wasn't predeclared and if this is not a function |
| 1676 | // call, diagnose the problem. |
| 1677 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1678 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1679 | return ExprError(); |
| 1680 | |
| 1681 | assert(!R.empty() && |
| 1682 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1683 | |
| 1684 | // If we found an Objective-C instance variable, let |
| 1685 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1686 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1687 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1688 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1689 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1690 | assert(E.isInvalid() || E.get()); |
| 1691 | return move(E); |
| 1692 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1696 | // This is guaranteed from this point on. |
| 1697 | assert(!R.empty() || ADL); |
| 1698 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1699 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1700 | // C++ [class.mfct.non-static]p3: |
| 1701 | // When an id-expression that is not part of a class member access |
| 1702 | // syntax and not used to form a pointer to member is used in the |
| 1703 | // body of a non-static member function of class X, if name lookup |
| 1704 | // resolves the name in the id-expression to a non-static non-type |
| 1705 | // member of some class C, the id-expression is transformed into a |
| 1706 | // class member access expression using (*this) as the |
| 1707 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1708 | // |
| 1709 | // But we don't actually need to do this for '&' operands if R |
| 1710 | // resolved to a function or overloaded function set, because the |
| 1711 | // expression is ill-formed if it actually works out to be a |
| 1712 | // non-static member function: |
| 1713 | // |
| 1714 | // C++ [expr.ref]p4: |
| 1715 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1716 | // [t]he expression can be used only as the left-hand operand of a |
| 1717 | // member function call. |
| 1718 | // |
| 1719 | // There are other safeguards against such uses, but it's important |
| 1720 | // to get this right here so that we don't end up making a |
| 1721 | // spuriously dependent expression if we're inside a dependent |
| 1722 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1723 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1724 | bool MightBeImplicitMember; |
| 1725 | if (!isAddressOfOperand) |
| 1726 | MightBeImplicitMember = true; |
| 1727 | else if (!SS.isEmpty()) |
| 1728 | MightBeImplicitMember = false; |
| 1729 | else if (R.isOverloadedResult()) |
| 1730 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1731 | else if (R.isUnresolvableResult()) |
| 1732 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1733 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1734 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1735 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1738 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1741 | if (TemplateArgs) |
| 1742 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1743 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1744 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1745 | } |
| 1746 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1747 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1748 | /// declaration name, generally during template instantiation. |
| 1749 | /// There's a large number of things which don't need to be done along |
| 1750 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1751 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1752 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1753 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1754 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1755 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1756 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1757 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1758 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1759 | return ExprError(); |
| 1760 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1761 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1762 | LookupQualifiedName(R, DC); |
| 1763 | |
| 1764 | if (R.isAmbiguous()) |
| 1765 | return ExprError(); |
| 1766 | |
| 1767 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1768 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1769 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1770 | return ExprError(); |
| 1771 | } |
| 1772 | |
| 1773 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1774 | } |
| 1775 | |
| 1776 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1777 | /// detected that we're currently inside an ObjC method. Perform some |
| 1778 | /// additional lookup. |
| 1779 | /// |
| 1780 | /// Ideally, most of this would be done by lookup, but there's |
| 1781 | /// actually quite a lot of extra work involved. |
| 1782 | /// |
| 1783 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1784 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1785 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1786 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1787 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1788 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1789 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1790 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1791 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1792 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1793 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1794 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1795 | |
| 1796 | // If we're in a class method, we don't normally want to look for |
| 1797 | // ivars. But if we don't find anything else, and there's an |
| 1798 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1799 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1800 | |
| 1801 | bool LookForIvars; |
| 1802 | if (Lookup.empty()) |
| 1803 | LookForIvars = true; |
| 1804 | else if (IsClassMethod) |
| 1805 | LookForIvars = false; |
| 1806 | else |
| 1807 | LookForIvars = (Lookup.isSingleResult() && |
| 1808 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1809 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1810 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1811 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1812 | ObjCInterfaceDecl *ClassDeclared; |
| 1813 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1814 | // Diagnose using an ivar in a class method. |
| 1815 | if (IsClassMethod) |
| 1816 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1817 | << IV->getDeclName()); |
| 1818 | |
| 1819 | // If we're referencing an invalid decl, just return this as a silent |
| 1820 | // error node. The error diagnostic was already emitted on the decl. |
| 1821 | if (IV->isInvalidDecl()) |
| 1822 | return ExprError(); |
| 1823 | |
| 1824 | // Check if referencing a field with __attribute__((deprecated)). |
| 1825 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1826 | return ExprError(); |
| 1827 | |
| 1828 | // Diagnose the use of an ivar outside of the declaring class. |
| 1829 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1830 | ClassDeclared != IFace) |
| 1831 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1832 | |
| 1833 | // FIXME: This should use a new expr for a direct reference, don't |
| 1834 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1835 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1836 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1837 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1838 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1839 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1840 | SelfName, false, false); |
| 1841 | if (SelfExpr.isInvalid()) |
| 1842 | return ExprError(); |
| 1843 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1844 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1845 | if (SelfExpr.isInvalid()) |
| 1846 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1847 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1848 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 1849 | Expr *base = SelfExpr.take(); |
| 1850 | base = base->IgnoreParenImpCasts(); |
| 1851 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 1852 | const NamedDecl *ND = DE->getDecl(); |
| 1853 | if (!isa<ImplicitParamDecl>(ND)) { |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 1854 | // relax the rule such that it is allowed to have a shadow 'self' |
| 1855 | // where stand-alone ivar can be found in this 'self' object. |
| 1856 | // This is to match gcc's behavior. |
| 1857 | ObjCInterfaceDecl *selfIFace = 0; |
| 1858 | if (const ObjCObjectPointerType *OPT = |
| 1859 | base->getType()->getAsObjCInterfacePointerType()) |
| 1860 | selfIFace = OPT->getInterfaceDecl(); |
| 1861 | if (!selfIFace || |
| 1862 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 1863 | Diag(Loc, diag::error_implicit_ivar_access) |
| 1864 | << IV->getDeclName(); |
| 1865 | Diag(ND->getLocation(), diag::note_declared_at); |
| 1866 | return ExprError(); |
| 1867 | } |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 1868 | } |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 1869 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1870 | return Owned(new (Context) |
| 1871 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1872 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1873 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1874 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1875 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1876 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1877 | ObjCInterfaceDecl *ClassDeclared; |
| 1878 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1879 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1880 | IFace == ClassDeclared) |
| 1881 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1882 | } |
| 1883 | } |
| 1884 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1885 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1886 | // FIXME. Consolidate this with similar code in LookupName. |
| 1887 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1888 | if (!(getLangOptions().CPlusPlus && |
| 1889 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1890 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1891 | S, Lookup.isForRedeclaration(), |
| 1892 | Lookup.getNameLoc()); |
| 1893 | if (D) Lookup.addDecl(D); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1897 | // Sentinel value saying that we didn't do anything special. |
| 1898 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1899 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1900 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1901 | /// \brief Cast a base object to a member's actual type. |
| 1902 | /// |
| 1903 | /// Logically this happens in three phases: |
| 1904 | /// |
| 1905 | /// * First we cast from the base type to the naming class. |
| 1906 | /// The naming class is the class into which we were looking |
| 1907 | /// when we found the member; it's the qualifier type if a |
| 1908 | /// qualifier was provided, and otherwise it's the base type. |
| 1909 | /// |
| 1910 | /// * Next we cast from the naming class to the declaring class. |
| 1911 | /// If the member we found was brought into a class's scope by |
| 1912 | /// a using declaration, this is that class; otherwise it's |
| 1913 | /// the class declaring the member. |
| 1914 | /// |
| 1915 | /// * Finally we cast from the declaring class to the "true" |
| 1916 | /// declaring class of the member. This conversion does not |
| 1917 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1918 | ExprResult |
| 1919 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1920 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1921 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1922 | NamedDecl *Member) { |
| 1923 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1924 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1925 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1927 | QualType DestRecordType; |
| 1928 | QualType DestType; |
| 1929 | QualType FromRecordType; |
| 1930 | QualType FromType = From->getType(); |
| 1931 | bool PointerConversions = false; |
| 1932 | if (isa<FieldDecl>(Member)) { |
| 1933 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1934 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1935 | if (FromType->getAs<PointerType>()) { |
| 1936 | DestType = Context.getPointerType(DestRecordType); |
| 1937 | FromRecordType = FromType->getPointeeType(); |
| 1938 | PointerConversions = true; |
| 1939 | } else { |
| 1940 | DestType = DestRecordType; |
| 1941 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1942 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1943 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1944 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1945 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1947 | DestType = Method->getThisType(Context); |
| 1948 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1950 | if (FromType->getAs<PointerType>()) { |
| 1951 | FromRecordType = FromType->getPointeeType(); |
| 1952 | PointerConversions = true; |
| 1953 | } else { |
| 1954 | FromRecordType = FromType; |
| 1955 | DestType = DestRecordType; |
| 1956 | } |
| 1957 | } else { |
| 1958 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1959 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1960 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1962 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1963 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1965 | // If the unqualified types are the same, no conversion is necessary. |
| 1966 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1967 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1968 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1969 | SourceRange FromRange = From->getSourceRange(); |
| 1970 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1971 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1972 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1974 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1975 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1976 | // class name. |
| 1977 | // |
| 1978 | // If the member was a qualified name and the qualified referred to a |
| 1979 | // specific base subobject type, we'll cast to that intermediate type |
| 1980 | // first and then to the object in which the member is declared. That allows |
| 1981 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1982 | // |
| 1983 | // class Base { public: int x; }; |
| 1984 | // class Derived1 : public Base { }; |
| 1985 | // class Derived2 : public Base { }; |
| 1986 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1987 | // |
| 1988 | // void VeryDerived::f() { |
| 1989 | // x = 17; // error: ambiguous base subobjects |
| 1990 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1991 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1992 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1993 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1994 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1995 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1996 | |
| 1997 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1998 | |
| 1999 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2000 | // type of the object type, in which case we just ignore it. |
| 2001 | // Otherwise build the appropriate casts. |
| 2002 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2003 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2004 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2005 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2006 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2008 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2009 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2010 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2011 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2012 | |
| 2013 | FromType = QType; |
| 2014 | FromRecordType = QRecordType; |
| 2015 | |
| 2016 | // If the qualifier type was the same as the destination type, |
| 2017 | // we're done. |
| 2018 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2019 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2020 | } |
| 2021 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2022 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2023 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2024 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2025 | // If we actually found the member through a using declaration, cast |
| 2026 | // down to the using declaration's type. |
| 2027 | // |
| 2028 | // Pointer equality is fine here because only one declaration of a |
| 2029 | // class ever has member declarations. |
| 2030 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2031 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2032 | QualType URecordType = Context.getTypeDeclType( |
| 2033 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2034 | |
| 2035 | // We only need to do this if the naming-class to declaring-class |
| 2036 | // conversion is non-trivial. |
| 2037 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2038 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2039 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2040 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2041 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2042 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2043 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2044 | QualType UType = URecordType; |
| 2045 | if (PointerConversions) |
| 2046 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2047 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2048 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2049 | FromType = UType; |
| 2050 | FromRecordType = URecordType; |
| 2051 | } |
| 2052 | |
| 2053 | // We don't do access control for the conversion from the |
| 2054 | // declaring class to the true declaring class. |
| 2055 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2056 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2057 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2058 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2059 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2060 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2061 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2062 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2063 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2064 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2065 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2066 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2067 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2068 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2069 | const LookupResult &R, |
| 2070 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2071 | // Only when used directly as the postfix-expression of a call. |
| 2072 | if (!HasTrailingLParen) |
| 2073 | return false; |
| 2074 | |
| 2075 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2076 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2077 | return false; |
| 2078 | |
| 2079 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2080 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2081 | return false; |
| 2082 | |
| 2083 | // Turn off ADL when we find certain kinds of declarations during |
| 2084 | // normal lookup: |
| 2085 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2086 | NamedDecl *D = *I; |
| 2087 | |
| 2088 | // C++0x [basic.lookup.argdep]p3: |
| 2089 | // -- a declaration of a class member |
| 2090 | // Since using decls preserve this property, we check this on the |
| 2091 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2092 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2093 | return false; |
| 2094 | |
| 2095 | // C++0x [basic.lookup.argdep]p3: |
| 2096 | // -- a block-scope function declaration that is not a |
| 2097 | // using-declaration |
| 2098 | // NOTE: we also trigger this for function templates (in fact, we |
| 2099 | // don't check the decl type at all, since all other decl types |
| 2100 | // turn off ADL anyway). |
| 2101 | if (isa<UsingShadowDecl>(D)) |
| 2102 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2103 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2104 | return false; |
| 2105 | |
| 2106 | // C++0x [basic.lookup.argdep]p3: |
| 2107 | // -- a declaration that is neither a function or a function |
| 2108 | // template |
| 2109 | // And also for builtin functions. |
| 2110 | if (isa<FunctionDecl>(D)) { |
| 2111 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2112 | |
| 2113 | // But also builtin functions. |
| 2114 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2115 | return false; |
| 2116 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | return true; |
| 2121 | } |
| 2122 | |
| 2123 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2124 | /// Diagnoses obvious problems with the use of the given declaration |
| 2125 | /// as an expression. This is only actually called for lookups that |
| 2126 | /// were not overloaded, and it doesn't promise that the declaration |
| 2127 | /// will in fact be used. |
| 2128 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2129 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2130 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
| 2134 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2135 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2136 | return true; |
| 2137 | } |
| 2138 | |
| 2139 | if (isa<NamespaceDecl>(D)) { |
| 2140 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | return false; |
| 2145 | } |
| 2146 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2147 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2148 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2149 | LookupResult &R, |
| 2150 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2151 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2152 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2153 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2154 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2155 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2156 | |
| 2157 | // We only need to check the declaration if there's exactly one |
| 2158 | // result, because in the overloaded case the results can only be |
| 2159 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2160 | if (R.isSingleResult() && |
| 2161 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2162 | return ExprError(); |
| 2163 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2164 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2165 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2166 | // we've picked a target. |
| 2167 | R.suppressDiagnostics(); |
| 2168 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2169 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2170 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2171 | SS.getWithLocInContext(Context), |
| 2172 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2173 | NeedsADL, R.isOverloadedResult(), |
| 2174 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2175 | |
| 2176 | return Owned(ULE); |
| 2177 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2178 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2179 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2180 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2181 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2182 | const DeclarationNameInfo &NameInfo, |
| 2183 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2184 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2185 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2186 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2187 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2188 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2189 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2190 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2192 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2193 | // Specifically diagnose references to class templates that are missing |
| 2194 | // a template argument list. |
| 2195 | Diag(Loc, diag::err_template_decl_ref) |
| 2196 | << Template << SS.getRange(); |
| 2197 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2198 | return ExprError(); |
| 2199 | } |
| 2200 | |
| 2201 | // Make sure that we're referring to a value. |
| 2202 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2203 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2204 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2205 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2206 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2207 | return ExprError(); |
| 2208 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2209 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2210 | // Check whether this declaration can be used. Note that we suppress |
| 2211 | // this check when we're going to perform argument-dependent lookup |
| 2212 | // on this function name, because this might not be the function |
| 2213 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2214 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2215 | return ExprError(); |
| 2216 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2217 | // Only create DeclRefExpr's for valid Decl's. |
| 2218 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2219 | return ExprError(); |
| 2220 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2221 | // Handle members of anonymous structs and unions. If we got here, |
| 2222 | // and the reference is to a class member indirect field, then this |
| 2223 | // must be the subject of a pointer-to-member expression. |
| 2224 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2225 | if (!indirectField->isCXXClassMember()) |
| 2226 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2227 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2229 | // If the identifier reference is inside a block, and it refers to a value |
| 2230 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2231 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2232 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2233 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2234 | // We do not do this for things like enum constants, global variables, etc, |
| 2235 | // as they do not get snapshotted. |
| 2236 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2237 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2238 | case CR_Error: |
| 2239 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2240 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2241 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2242 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2243 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2244 | |
| 2245 | case CR_CaptureByRef: |
| 2246 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2247 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2248 | |
| 2249 | case CR_NoCapture: { |
| 2250 | // If this reference is not in a block or if the referenced |
| 2251 | // variable is within the block, create a normal DeclRefExpr. |
| 2252 | |
| 2253 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2254 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2255 | |
| 2256 | switch (D->getKind()) { |
| 2257 | // Ignore all the non-ValueDecl kinds. |
| 2258 | #define ABSTRACT_DECL(kind) |
| 2259 | #define VALUE(type, base) |
| 2260 | #define DECL(type, base) \ |
| 2261 | case Decl::type: |
| 2262 | #include "clang/AST/DeclNodes.inc" |
| 2263 | llvm_unreachable("invalid value decl kind"); |
| 2264 | return ExprError(); |
| 2265 | |
| 2266 | // These shouldn't make it here. |
| 2267 | case Decl::ObjCAtDefsField: |
| 2268 | case Decl::ObjCIvar: |
| 2269 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2270 | return ExprError(); |
| 2271 | |
| 2272 | // Enum constants are always r-values and never references. |
| 2273 | // Unresolved using declarations are dependent. |
| 2274 | case Decl::EnumConstant: |
| 2275 | case Decl::UnresolvedUsingValue: |
| 2276 | valueKind = VK_RValue; |
| 2277 | break; |
| 2278 | |
| 2279 | // Fields and indirect fields that got here must be for |
| 2280 | // pointer-to-member expressions; we just call them l-values for |
| 2281 | // internal consistency, because this subexpression doesn't really |
| 2282 | // exist in the high-level semantics. |
| 2283 | case Decl::Field: |
| 2284 | case Decl::IndirectField: |
| 2285 | assert(getLangOptions().CPlusPlus && |
| 2286 | "building reference to field in C?"); |
| 2287 | |
| 2288 | // These can't have reference type in well-formed programs, but |
| 2289 | // for internal consistency we do this anyway. |
| 2290 | type = type.getNonReferenceType(); |
| 2291 | valueKind = VK_LValue; |
| 2292 | break; |
| 2293 | |
| 2294 | // Non-type template parameters are either l-values or r-values |
| 2295 | // depending on the type. |
| 2296 | case Decl::NonTypeTemplateParm: { |
| 2297 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2298 | type = reftype->getPointeeType(); |
| 2299 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | // For non-references, we need to strip qualifiers just in case |
| 2304 | // the template parameter was declared as 'const int' or whatever. |
| 2305 | valueKind = VK_RValue; |
| 2306 | type = type.getUnqualifiedType(); |
| 2307 | break; |
| 2308 | } |
| 2309 | |
| 2310 | case Decl::Var: |
| 2311 | // In C, "extern void blah;" is valid and is an r-value. |
| 2312 | if (!getLangOptions().CPlusPlus && |
| 2313 | !type.hasQualifiers() && |
| 2314 | type->isVoidType()) { |
| 2315 | valueKind = VK_RValue; |
| 2316 | break; |
| 2317 | } |
| 2318 | // fallthrough |
| 2319 | |
| 2320 | case Decl::ImplicitParam: |
| 2321 | case Decl::ParmVar: |
| 2322 | // These are always l-values. |
| 2323 | valueKind = VK_LValue; |
| 2324 | type = type.getNonReferenceType(); |
| 2325 | break; |
| 2326 | |
| 2327 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2328 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2329 | |
| 2330 | // If we're referring to a function with an __unknown_anytype |
| 2331 | // result type, make the entire expression __unknown_anytype. |
| 2332 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2333 | type = Context.UnknownAnyTy; |
| 2334 | valueKind = VK_RValue; |
| 2335 | break; |
| 2336 | } |
| 2337 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2338 | // Functions are l-values in C++. |
| 2339 | if (getLangOptions().CPlusPlus) { |
| 2340 | valueKind = VK_LValue; |
| 2341 | break; |
| 2342 | } |
| 2343 | |
| 2344 | // C99 DR 316 says that, if a function type comes from a |
| 2345 | // function definition (without a prototype), that type is only |
| 2346 | // used for checking compatibility. Therefore, when referencing |
| 2347 | // the function, we pretend that we don't have the full function |
| 2348 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2349 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2350 | isa<FunctionProtoType>(fty)) |
| 2351 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2352 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2353 | |
| 2354 | // Functions are r-values in C. |
| 2355 | valueKind = VK_RValue; |
| 2356 | break; |
| 2357 | } |
| 2358 | |
| 2359 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2360 | // If we're referring to a method with an __unknown_anytype |
| 2361 | // result type, make the entire expression __unknown_anytype. |
| 2362 | // This should only be possible with a type written directly. |
| 2363 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2364 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2365 | type = Context.UnknownAnyTy; |
| 2366 | valueKind = VK_RValue; |
| 2367 | break; |
| 2368 | } |
| 2369 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2370 | // C++ methods are l-values if static, r-values if non-static. |
| 2371 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2372 | valueKind = VK_LValue; |
| 2373 | break; |
| 2374 | } |
| 2375 | // fallthrough |
| 2376 | |
| 2377 | case Decl::CXXConversion: |
| 2378 | case Decl::CXXDestructor: |
| 2379 | case Decl::CXXConstructor: |
| 2380 | valueKind = VK_RValue; |
| 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2385 | } |
| 2386 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2387 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2388 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2389 | llvm_unreachable("unknown capture result"); |
| 2390 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2393 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2394 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2396 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2397 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2398 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2399 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2400 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2401 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2403 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2404 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2406 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2407 | if (!currentDecl && getCurBlock()) |
| 2408 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2409 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2410 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2411 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2412 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2413 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2414 | QualType ResTy; |
| 2415 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2416 | ResTy = Context.DependentTy; |
| 2417 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2418 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2419 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2420 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2421 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2422 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2423 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2424 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2427 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2428 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2429 | bool Invalid = false; |
| 2430 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2431 | if (Invalid) |
| 2432 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2433 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2434 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2435 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2436 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2437 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2439 | QualType Ty; |
| 2440 | if (!getLangOptions().CPlusPlus) |
| 2441 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2442 | else if (Literal.isWide()) |
| 2443 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2444 | else if (Literal.isMultiChar()) |
| 2445 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2446 | else |
| 2447 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2448 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2449 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2450 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2451 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2454 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2455 | // 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] | 2456 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2457 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2458 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2459 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2460 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2461 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2462 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2463 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2464 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2465 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2466 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2467 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2469 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2470 | bool Invalid = false; |
| 2471 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2472 | if (Invalid) |
| 2473 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2474 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2476 | Tok.getLocation(), PP); |
| 2477 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2478 | return ExprError(); |
| 2479 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2480 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2481 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2482 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2483 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2484 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2485 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2486 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2487 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2488 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2489 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2490 | |
| 2491 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2492 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2493 | using llvm::APFloat; |
| 2494 | APFloat Val(Format); |
| 2495 | |
| 2496 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2497 | |
| 2498 | // Overflow is always an error, but underflow is only an error if |
| 2499 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2500 | if ((result & APFloat::opOverflow) || |
| 2501 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2502 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2503 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2504 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2505 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2506 | APFloat::getLargest(Format).toString(buffer); |
| 2507 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2508 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2509 | APFloat::getSmallest(Format).toString(buffer); |
| 2510 | } |
| 2511 | |
| 2512 | Diag(Tok.getLocation(), diagnostic) |
| 2513 | << Ty |
| 2514 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2515 | } |
| 2516 | |
| 2517 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2518 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2519 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2520 | if (Ty == Context.DoubleTy) { |
| 2521 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2522 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2523 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2524 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2525 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2526 | } |
| 2527 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2528 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2529 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2530 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2531 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2532 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2533 | // long long is a C99 feature. |
| 2534 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2535 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2536 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2537 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2538 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2539 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2541 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2542 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2543 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2544 | Ty = Context.UnsignedLongLongTy; |
| 2545 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2546 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2547 | } else { |
| 2548 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2549 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2550 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2551 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2552 | // be an unsigned int. |
| 2553 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2554 | |
| 2555 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2556 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2557 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2558 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2559 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2560 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2561 | // Does it fit in a unsigned int? |
| 2562 | if (ResultVal.isIntN(IntSize)) { |
| 2563 | // Does it fit in a signed int? |
| 2564 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2565 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2566 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2567 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2568 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2569 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2570 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2571 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2572 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2573 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2574 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2576 | // Does it fit in a unsigned long? |
| 2577 | if (ResultVal.isIntN(LongSize)) { |
| 2578 | // Does it fit in a signed long? |
| 2579 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2580 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2581 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2582 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2583 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2584 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2587 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2588 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2589 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2590 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | // Does it fit in a unsigned long long? |
| 2592 | if (ResultVal.isIntN(LongLongSize)) { |
| 2593 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2594 | // To be compatible with MSVC, hex integer literals ending with the |
| 2595 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2596 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2597 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2598 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2599 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2600 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2601 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2602 | } |
| 2603 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2605 | // If we still couldn't decide a type, we probably have something that |
| 2606 | // 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] | 2607 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2608 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2609 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2610 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2611 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2612 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2613 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2614 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2615 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2616 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2617 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2619 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2620 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2622 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2623 | |
| 2624 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2627 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2628 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2629 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2630 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2633 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2634 | SourceLocation Loc, |
| 2635 | SourceRange ArgRange) { |
| 2636 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2637 | // scalar or vector data type argument..." |
| 2638 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2639 | // type (C99 6.2.5p18) or void. |
| 2640 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2641 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2642 | << T << ArgRange; |
| 2643 | return true; |
| 2644 | } |
| 2645 | |
| 2646 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2647 | "Scalar types should always be complete"); |
| 2648 | return false; |
| 2649 | } |
| 2650 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2651 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2652 | SourceLocation Loc, |
| 2653 | SourceRange ArgRange, |
| 2654 | UnaryExprOrTypeTrait TraitKind) { |
| 2655 | // C99 6.5.3.4p1: |
| 2656 | if (T->isFunctionType()) { |
| 2657 | // alignof(function) is allowed as an extension. |
| 2658 | if (TraitKind == UETT_SizeOf) |
| 2659 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2660 | return false; |
| 2661 | } |
| 2662 | |
| 2663 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2664 | if (T->isVoidType()) { |
| 2665 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
| 2669 | return true; |
| 2670 | } |
| 2671 | |
| 2672 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2673 | SourceLocation Loc, |
| 2674 | SourceRange ArgRange, |
| 2675 | UnaryExprOrTypeTrait TraitKind) { |
| 2676 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2677 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2678 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2679 | << T << (TraitKind == UETT_SizeOf) |
| 2680 | << ArgRange; |
| 2681 | return true; |
| 2682 | } |
| 2683 | |
| 2684 | return false; |
| 2685 | } |
| 2686 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2687 | /// \brief Check the constrains on expression operands to unary type expression |
| 2688 | /// and type traits. |
| 2689 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2690 | /// Completes any types necessary and validates the constraints on the operand |
| 2691 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2692 | /// the expression as it completes the type for that expression through template |
| 2693 | /// instantiation, etc. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2694 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 2695 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2696 | QualType ExprTy = Op->getType(); |
| 2697 | |
| 2698 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2699 | // the result is the size of the referenced type." |
| 2700 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2701 | // result shall be the alignment of the referenced type." |
| 2702 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2703 | ExprTy = Ref->getPointeeType(); |
| 2704 | |
| 2705 | if (ExprKind == UETT_VecStep) |
| 2706 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2707 | Op->getSourceRange()); |
| 2708 | |
| 2709 | // Whitelist some types as extensions |
| 2710 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2711 | Op->getSourceRange(), ExprKind)) |
| 2712 | return false; |
| 2713 | |
| 2714 | if (RequireCompleteExprType(Op, |
| 2715 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2716 | << ExprKind << Op->getSourceRange(), |
| 2717 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2718 | return true; |
| 2719 | |
| 2720 | // Completeing the expression's type may have changed it. |
| 2721 | ExprTy = Op->getType(); |
| 2722 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2723 | ExprTy = Ref->getPointeeType(); |
| 2724 | |
| 2725 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 2726 | Op->getSourceRange(), ExprKind)) |
| 2727 | return true; |
| 2728 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2729 | if (ExprKind == UETT_SizeOf) { |
| 2730 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 2731 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2732 | QualType OType = PVD->getOriginalType(); |
| 2733 | QualType Type = PVD->getType(); |
| 2734 | if (Type->isPointerType() && OType->isArrayType()) { |
| 2735 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 2736 | << Type << OType; |
| 2737 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | } |
| 2742 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2743 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | /// \brief Check the constraints on operands to unary expression and type |
| 2747 | /// traits. |
| 2748 | /// |
| 2749 | /// This will complete any types necessary, and validate the various constraints |
| 2750 | /// on those operands. |
| 2751 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2752 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2753 | /// C99 6.3.2.1p[2-4] all state: |
| 2754 | /// Except when it is the operand of the sizeof operator ... |
| 2755 | /// |
| 2756 | /// C++ [expr.sizeof]p4 |
| 2757 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2758 | /// standard conversions are not applied to the operand of sizeof. |
| 2759 | /// |
| 2760 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2761 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2762 | SourceLocation OpLoc, |
| 2763 | SourceRange ExprRange, |
| 2764 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2765 | if (exprType->isDependentType()) |
| 2766 | return false; |
| 2767 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2768 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2769 | // the result is the size of the referenced type." |
| 2770 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2771 | // result shall be the alignment of the referenced type." |
| 2772 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2773 | exprType = Ref->getPointeeType(); |
| 2774 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2775 | if (ExprKind == UETT_VecStep) |
| 2776 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2777 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2778 | // Whitelist some types as extensions |
| 2779 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 2780 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2781 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2783 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2784 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2785 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2786 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2788 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 2789 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2790 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2792 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2795 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2796 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2797 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2798 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2799 | if (isa<DeclRefExpr>(E)) |
| 2800 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Cannot know anything else if the expression is dependent. |
| 2803 | if (E->isTypeDependent()) |
| 2804 | return false; |
| 2805 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2806 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2807 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2808 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2809 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2810 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2811 | |
| 2812 | // Alignment of a field access is always okay, so long as it isn't a |
| 2813 | // bit-field. |
| 2814 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2815 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2816 | return false; |
| 2817 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2818 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2821 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2822 | E = E->IgnoreParens(); |
| 2823 | |
| 2824 | // Cannot know anything else if the expression is dependent. |
| 2825 | if (E->isTypeDependent()) |
| 2826 | return false; |
| 2827 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2828 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2831 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2832 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2833 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2834 | SourceLocation OpLoc, |
| 2835 | UnaryExprOrTypeTrait ExprKind, |
| 2836 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2837 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2838 | return ExprError(); |
| 2839 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2840 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2841 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2842 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2843 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2844 | return ExprError(); |
| 2845 | |
| 2846 | // 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] | 2847 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2848 | Context.getSizeType(), |
| 2849 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | /// \brief Build a sizeof or alignof expression given an expression |
| 2853 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2854 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2855 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2856 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2857 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2858 | if (PE.isInvalid()) |
| 2859 | return ExprError(); |
| 2860 | |
| 2861 | E = PE.get(); |
| 2862 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2863 | // Verify that the operand is valid. |
| 2864 | bool isInvalid = false; |
| 2865 | if (E->isTypeDependent()) { |
| 2866 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2867 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2868 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2869 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2870 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2871 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2872 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2873 | isInvalid = true; |
| 2874 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2875 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | if (isInvalid) |
| 2879 | return ExprError(); |
| 2880 | |
| 2881 | // 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] | 2882 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2883 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2884 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2887 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2888 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2889 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2890 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2891 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2892 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2893 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2894 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2895 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2896 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2897 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2898 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2899 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2900 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2902 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2903 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2904 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2905 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2908 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2909 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2910 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2911 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2913 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2914 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2915 | V = S.DefaultLvalueConversion(V.take()); |
| 2916 | if (V.isInvalid()) |
| 2917 | return QualType(); |
| 2918 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2919 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2920 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2921 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2922 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2924 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2925 | if (V.get()->getType()->isArithmeticType()) |
| 2926 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2928 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2929 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2930 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2931 | if (PR.get() != V.get()) { |
| 2932 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2933 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2936 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2937 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2938 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2939 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2943 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2944 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2945 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2946 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2947 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2948 | switch (Kind) { |
| 2949 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2950 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2951 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2952 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2953 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2954 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2957 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2958 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2959 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2960 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2961 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2962 | if (Result.isInvalid()) return ExprError(); |
| 2963 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2964 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2965 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2967 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2968 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2969 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2970 | Context.DependentTy, |
| 2971 | VK_LValue, OK_Ordinary, |
| 2972 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2976 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2977 | LHSExp->getType()->isEnumeralType() || |
| 2978 | RHSExp->getType()->isRecordType() || |
| 2979 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2980 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2983 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2987 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2988 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 2989 | Expr *Idx, SourceLocation RLoc) { |
| 2990 | Expr *LHSExp = Base; |
| 2991 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2992 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2993 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2994 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 2995 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2996 | if (Result.isInvalid()) |
| 2997 | return ExprError(); |
| 2998 | LHSExp = Result.take(); |
| 2999 | } |
| 3000 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3001 | if (Result.isInvalid()) |
| 3002 | return ExprError(); |
| 3003 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3004 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3005 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3006 | ExprValueKind VK = VK_LValue; |
| 3007 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3008 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3009 | // 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] | 3010 | // 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] | 3011 | // 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] | 3012 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3013 | Expr *BaseExpr, *IndexExpr; |
| 3014 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3015 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3016 | BaseExpr = LHSExp; |
| 3017 | IndexExpr = RHSExp; |
| 3018 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3019 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3020 | BaseExpr = LHSExp; |
| 3021 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3022 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3023 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3024 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3025 | BaseExpr = RHSExp; |
| 3026 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3027 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3029 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3030 | BaseExpr = LHSExp; |
| 3031 | IndexExpr = RHSExp; |
| 3032 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3034 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3035 | // Handle the uncommon case of "123[Ptr]". |
| 3036 | BaseExpr = RHSExp; |
| 3037 | IndexExpr = LHSExp; |
| 3038 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3039 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3040 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3041 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3042 | VK = LHSExp->getValueKind(); |
| 3043 | if (VK != VK_RValue) |
| 3044 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3045 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3046 | // FIXME: need to deal with const... |
| 3047 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3048 | } else if (LHSTy->isArrayType()) { |
| 3049 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3050 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3051 | // wasn't promoted because of the C90 rule that doesn't |
| 3052 | // allow promoting non-lvalue arrays. Warn, then |
| 3053 | // force the promotion here. |
| 3054 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3055 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3056 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3057 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3058 | LHSTy = LHSExp->getType(); |
| 3059 | |
| 3060 | BaseExpr = LHSExp; |
| 3061 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3062 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3063 | } else if (RHSTy->isArrayType()) { |
| 3064 | // Same as previous, except for 123[f().a] case |
| 3065 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3066 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3067 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3068 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3069 | RHSTy = RHSExp->getType(); |
| 3070 | |
| 3071 | BaseExpr = RHSExp; |
| 3072 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3073 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3074 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3075 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3076 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3077 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3078 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3079 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3080 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3081 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3082 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3083 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3084 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3085 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3086 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3087 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3088 | // 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] | 3089 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3090 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3091 | // incomplete types are not object types. |
| 3092 | if (ResultType->isFunctionType()) { |
| 3093 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3094 | << ResultType << BaseExpr->getSourceRange(); |
| 3095 | return ExprError(); |
| 3096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3098 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3099 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3100 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3101 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3102 | |
| 3103 | // C forbids expressions of unqualified void type from being l-values. |
| 3104 | // See IsCForbiddenLValueType. |
| 3105 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3106 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3107 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3108 | PDiag(diag::err_subscript_incomplete_type) |
| 3109 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3110 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3112 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3113 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3114 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3115 | << ResultType << BaseExpr->getSourceRange(); |
| 3116 | return ExprError(); |
| 3117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3119 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3120 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3121 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3122 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3123 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3126 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3127 | FunctionDecl *FD, |
| 3128 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3129 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3130 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3131 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3132 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3134 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3135 | return ExprError(); |
| 3136 | } |
| 3137 | |
| 3138 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3139 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3140 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3141 | // Instantiate the expression. |
| 3142 | MultiLevelTemplateArgumentList ArgList |
| 3143 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3144 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3145 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3146 | = ArgList.getInnermost(); |
| 3147 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3148 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3149 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3150 | ExprResult Result; |
| 3151 | { |
| 3152 | // C++ [dcl.fct.default]p5: |
| 3153 | // The names in the [default argument] expression are bound, and |
| 3154 | // the semantic constraints are checked, at the point where the |
| 3155 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3156 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3157 | Result = SubstExpr(UninstExpr, ArgList); |
| 3158 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3159 | if (Result.isInvalid()) |
| 3160 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3162 | // Check the expression as an initializer for the parameter. |
| 3163 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3164 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3165 | InitializationKind Kind |
| 3166 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3167 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3168 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3169 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3170 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3171 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3172 | MultiExprArg(*this, &ResultE, 1)); |
| 3173 | if (Result.isInvalid()) |
| 3174 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3175 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3176 | // Build the default argument expression. |
| 3177 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3178 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3181 | // If the default expression creates temporaries, we need to |
| 3182 | // push them to the current stack of expression temporaries so they'll |
| 3183 | // be properly destroyed. |
| 3184 | // FIXME: We should really be rebuilding the default argument with new |
| 3185 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3186 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3187 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3188 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3189 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3190 | ExprTemporaries.push_back(Temporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3191 | ExprNeedsCleanups = true; |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3192 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3193 | |
| 3194 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3195 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3196 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3197 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3198 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3201 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3202 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3203 | /// function prototype Proto. Call is the call expression itself, and |
| 3204 | /// Fn is the function expression. For a C++ member function, this |
| 3205 | /// routine does not attempt to convert the object argument. Returns |
| 3206 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3207 | bool |
| 3208 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3209 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3210 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3211 | Expr **Args, unsigned NumArgs, |
| 3212 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3213 | // Bail out early if calling a builtin with custom typechecking. |
| 3214 | // We don't need to do this in the |
| 3215 | if (FDecl) |
| 3216 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3217 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3218 | return false; |
| 3219 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3220 | // 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] | 3221 | // assignment, to the types of the corresponding parameter, ... |
| 3222 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3223 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3224 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3225 | // If too few arguments are available (and we don't have default |
| 3226 | // arguments for the remaining parameters), don't make the call. |
| 3227 | if (NumArgs < NumArgsInProto) { |
| 3228 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3229 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3230 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3231 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3232 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | // If too many are passed and not variadic, error on the extras and drop |
| 3236 | // them. |
| 3237 | if (NumArgs > NumArgsInProto) { |
| 3238 | if (!Proto->isVariadic()) { |
| 3239 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3240 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3241 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3242 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3243 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3244 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Emit the location of the prototype. |
| 3247 | if (FDecl && !FDecl->getBuiltinID()) |
| 3248 | Diag(FDecl->getLocStart(), |
| 3249 | diag::note_typecheck_call_too_many_args) |
| 3250 | << FDecl; |
| 3251 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3252 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3253 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3254 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3255 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3256 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3257 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3258 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3259 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3260 | if (Fn->getType()->isBlockPointerType()) |
| 3261 | CallType = VariadicBlock; // Block |
| 3262 | else if (isa<MemberExpr>(Fn)) |
| 3263 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3264 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3265 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3266 | if (Invalid) |
| 3267 | return true; |
| 3268 | unsigned TotalNumArgs = AllArgs.size(); |
| 3269 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3270 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3271 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3272 | return false; |
| 3273 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3274 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3275 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3276 | FunctionDecl *FDecl, |
| 3277 | const FunctionProtoType *Proto, |
| 3278 | unsigned FirstProtoArg, |
| 3279 | Expr **Args, unsigned NumArgs, |
| 3280 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3281 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3282 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3283 | unsigned NumArgsToCheck = NumArgs; |
| 3284 | bool Invalid = false; |
| 3285 | if (NumArgs != NumArgsInProto) |
| 3286 | // Use default arguments for missing arguments |
| 3287 | NumArgsToCheck = NumArgsInProto; |
| 3288 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3289 | // 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] | 3290 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3291 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3292 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3293 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3294 | if (ArgIx < NumArgs) { |
| 3295 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3296 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3297 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3298 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3299 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3300 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3301 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3303 | // Pass the argument |
| 3304 | ParmVarDecl *Param = 0; |
| 3305 | if (FDecl && i < FDecl->getNumParams()) |
| 3306 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3308 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3309 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3310 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3311 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3312 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3313 | SourceLocation(), |
| 3314 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3315 | if (ArgE.isInvalid()) |
| 3316 | return true; |
| 3317 | |
| 3318 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3319 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3320 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3321 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3322 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3323 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3324 | if (ArgExpr.isInvalid()) |
| 3325 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3326 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3327 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3328 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3329 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3330 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3332 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3333 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3334 | |
| 3335 | // Assume that extern "C" functions with variadic arguments that |
| 3336 | // return __unknown_anytype aren't *really* variadic. |
| 3337 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3338 | FDecl && FDecl->isExternC()) { |
| 3339 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3340 | ExprResult arg; |
| 3341 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3342 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3343 | else |
| 3344 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3345 | Invalid |= arg.isInvalid(); |
| 3346 | AllArgs.push_back(arg.take()); |
| 3347 | } |
| 3348 | |
| 3349 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3350 | } else { |
| 3351 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3352 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3353 | Invalid |= Arg.isInvalid(); |
| 3354 | AllArgs.push_back(Arg.take()); |
| 3355 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3358 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3361 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3362 | /// to have a function type. |
| 3363 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3364 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3365 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3366 | /// This provides the location of the left/right parens and a list of comma |
| 3367 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3368 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3369 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3370 | MultiExprArg args, SourceLocation RParenLoc, |
| 3371 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3372 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3373 | |
| 3374 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3375 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3376 | if (Result.isInvalid()) return ExprError(); |
| 3377 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3379 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3381 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3382 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3383 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3384 | if (NumArgs > 0) { |
| 3385 | // Pseudo-destructor calls should not have any arguments. |
| 3386 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3387 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3388 | SourceRange(Args[0]->getLocStart(), |
| 3389 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3391 | NumArgs = 0; |
| 3392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3394 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3395 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3398 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3399 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3400 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3401 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3402 | bool Dependent = false; |
| 3403 | if (Fn->isTypeDependent()) |
| 3404 | Dependent = true; |
| 3405 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3406 | Dependent = true; |
| 3407 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3408 | if (Dependent) { |
| 3409 | if (ExecConfig) { |
| 3410 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3411 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3412 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3413 | } else { |
| 3414 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3415 | Context.DependentTy, VK_RValue, |
| 3416 | RParenLoc)); |
| 3417 | } |
| 3418 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3419 | |
| 3420 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3421 | if (Fn->getType()->isRecordType()) |
| 3422 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3423 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3424 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3425 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3426 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3427 | if (result.isInvalid()) return ExprError(); |
| 3428 | Fn = result.take(); |
| 3429 | } |
| 3430 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3431 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3432 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3433 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3434 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3435 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3436 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3437 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3438 | if (Fn->getType() == Context.OverloadTy) { |
| 3439 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3440 | |
| 3441 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3442 | if (!find.IsAddressOfOperand) { |
| 3443 | OverloadExpr *ovl = find.Expression; |
| 3444 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3445 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3446 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3447 | RParenLoc, ExecConfig); |
| 3448 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3449 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3450 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3451 | } |
| 3452 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3455 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3456 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3457 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3458 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3459 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3460 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3461 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3462 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3463 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3464 | if (isa<DeclRefExpr>(NakedFn)) |
| 3465 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3466 | else if (isa<MemberExpr>(NakedFn)) |
| 3467 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3468 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3469 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3470 | ExecConfig); |
| 3471 | } |
| 3472 | |
| 3473 | ExprResult |
| 3474 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 3475 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 3476 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3477 | if (!ConfigDecl) |
| 3478 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3479 | << "cudaConfigureCall"); |
| 3480 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3481 | |
| 3482 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3483 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3484 | |
| 3485 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3488 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3489 | /// |
| 3490 | /// __builtin_astype( value, dst type ) |
| 3491 | /// |
| 3492 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 3493 | SourceLocation BuiltinLoc, |
| 3494 | SourceLocation RParenLoc) { |
| 3495 | ExprValueKind VK = VK_RValue; |
| 3496 | ExprObjectKind OK = OK_Ordinary; |
| 3497 | QualType DstTy = GetTypeFromParser(destty); |
| 3498 | QualType SrcTy = expr->getType(); |
| 3499 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3500 | return ExprError(Diag(BuiltinLoc, |
| 3501 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3502 | << DstTy |
| 3503 | << SrcTy |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3504 | << expr->getSourceRange()); |
| 3505 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, RParenLoc)); |
| 3506 | } |
| 3507 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3508 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3509 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3510 | /// unary-convert to an expression of function-pointer or |
| 3511 | /// block-pointer type. |
| 3512 | /// |
| 3513 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3514 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3515 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3516 | SourceLocation LParenLoc, |
| 3517 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3518 | SourceLocation RParenLoc, |
| 3519 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3520 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3521 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3522 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3523 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3524 | if (Result.isInvalid()) |
| 3525 | return ExprError(); |
| 3526 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3527 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3528 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3529 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3530 | CallExpr *TheCall; |
| 3531 | if (Config) { |
| 3532 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3533 | cast<CallExpr>(Config), |
| 3534 | Args, NumArgs, |
| 3535 | Context.BoolTy, |
| 3536 | VK_RValue, |
| 3537 | RParenLoc); |
| 3538 | } else { |
| 3539 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3540 | Args, NumArgs, |
| 3541 | Context.BoolTy, |
| 3542 | VK_RValue, |
| 3543 | RParenLoc); |
| 3544 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3545 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3546 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3547 | |
| 3548 | // Bail out early if calling a builtin with custom typechecking. |
| 3549 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3550 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3551 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3552 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3553 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3554 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3555 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3556 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3557 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3558 | if (FuncT == 0) |
| 3559 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3560 | << Fn->getType() << Fn->getSourceRange()); |
| 3561 | } else if (const BlockPointerType *BPT = |
| 3562 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3563 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3564 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3565 | // Handle calls to expressions of unknown-any type. |
| 3566 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3567 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3568 | if (rewrite.isInvalid()) return ExprError(); |
| 3569 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3570 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3571 | goto retry; |
| 3572 | } |
| 3573 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3574 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3575 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3576 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3577 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3578 | if (getLangOptions().CUDA) { |
| 3579 | if (Config) { |
| 3580 | // CUDA: Kernel calls must be to global functions |
| 3581 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3582 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3583 | << FDecl->getName() << Fn->getSourceRange()); |
| 3584 | |
| 3585 | // CUDA: Kernel function must have 'void' return type |
| 3586 | if (!FuncT->getResultType()->isVoidType()) |
| 3587 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3588 | << Fn->getType() << Fn->getSourceRange()); |
| 3589 | } |
| 3590 | } |
| 3591 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3592 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3593 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3594 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3595 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3596 | return ExprError(); |
| 3597 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3598 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3599 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3600 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3601 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3602 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3603 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3604 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3605 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3606 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3607 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3608 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3609 | if (FDecl) { |
| 3610 | // Check if we have too few/too many template arguments, based |
| 3611 | // on our knowledge of the function definition. |
| 3612 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3613 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3614 | const FunctionProtoType *Proto |
| 3615 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3616 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3617 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3618 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3619 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3620 | |
| 3621 | // If the function we're calling isn't a function prototype, but we have |
| 3622 | // a function prototype from a prior declaratiom, use that prototype. |
| 3623 | if (!FDecl->hasPrototype()) |
| 3624 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3627 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3628 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3629 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3630 | |
| 3631 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3632 | InitializedEntity Entity |
| 3633 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | Proto->getArgType(i), |
| 3635 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3636 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3637 | SourceLocation(), |
| 3638 | Owned(Arg)); |
| 3639 | if (ArgE.isInvalid()) |
| 3640 | return true; |
| 3641 | |
| 3642 | Arg = ArgE.takeAs<Expr>(); |
| 3643 | |
| 3644 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3645 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3646 | |
| 3647 | if (ArgE.isInvalid()) |
| 3648 | return true; |
| 3649 | |
| 3650 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3653 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3654 | Arg->getType(), |
| 3655 | PDiag(diag::err_call_incomplete_argument) |
| 3656 | << Arg->getSourceRange())) |
| 3657 | return ExprError(); |
| 3658 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3659 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3660 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3661 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3662 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3663 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3664 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3665 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3666 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3667 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3668 | // Check for sentinels |
| 3669 | if (NDecl) |
| 3670 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3672 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3673 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3674 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3675 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3676 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3677 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3678 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3679 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3680 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3681 | return ExprError(); |
| 3682 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3683 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3684 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3687 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3688 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3689 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3690 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3691 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3692 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3693 | |
| 3694 | TypeSourceInfo *TInfo; |
| 3695 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3696 | if (!TInfo) |
| 3697 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3698 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3699 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3702 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3703 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3704 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3705 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3706 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3707 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3708 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3709 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3710 | << SourceRange(LParenLoc, |
| 3711 | literalExpr->getSourceRange().getEnd()))) |
| 3712 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3713 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3714 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3715 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3716 | } else if (!literalType->isDependentType() && |
| 3717 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3718 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3720 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3721 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3723 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3724 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3725 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3726 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3727 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3728 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3729 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3730 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3731 | &literalType); |
| 3732 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3733 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3734 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3735 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3736 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3737 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3738 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3739 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3740 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3741 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3742 | // In C, compound literals are l-values for some reason. |
| 3743 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3744 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3745 | return MaybeBindToTemporary( |
| 3746 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| 3747 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3750 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3751 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3752 | SourceLocation RBraceLoc) { |
| 3753 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3754 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3755 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3756 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3757 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3758 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3759 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3760 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3761 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3762 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3765 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3766 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3767 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3768 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3769 | // Also, callers should have filtered out the invalid cases with |
| 3770 | // pointers. Everything else should be possible. |
| 3771 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3772 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3773 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3774 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3775 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3776 | switch (SrcTy->getScalarTypeKind()) { |
| 3777 | case Type::STK_MemberPointer: |
| 3778 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3779 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3780 | case Type::STK_Pointer: |
| 3781 | switch (DestTy->getScalarTypeKind()) { |
| 3782 | case Type::STK_Pointer: |
| 3783 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3784 | CK_AnyPointerToObjCPointerCast : |
| 3785 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3786 | case Type::STK_Bool: |
| 3787 | return CK_PointerToBoolean; |
| 3788 | case Type::STK_Integral: |
| 3789 | return CK_PointerToIntegral; |
| 3790 | case Type::STK_Floating: |
| 3791 | case Type::STK_FloatingComplex: |
| 3792 | case Type::STK_IntegralComplex: |
| 3793 | case Type::STK_MemberPointer: |
| 3794 | llvm_unreachable("illegal cast from pointer"); |
| 3795 | } |
| 3796 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3797 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3798 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3799 | case Type::STK_Integral: |
| 3800 | switch (DestTy->getScalarTypeKind()) { |
| 3801 | case Type::STK_Pointer: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3802 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3803 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3804 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3805 | case Type::STK_Bool: |
| 3806 | return CK_IntegralToBoolean; |
| 3807 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3808 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3809 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3810 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3811 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3812 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 3813 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3814 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3815 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3816 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 3817 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3818 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3819 | case Type::STK_MemberPointer: |
| 3820 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3821 | } |
| 3822 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3823 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3824 | case Type::STK_Floating: |
| 3825 | switch (DestTy->getScalarTypeKind()) { |
| 3826 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3827 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3828 | case Type::STK_Bool: |
| 3829 | return CK_FloatingToBoolean; |
| 3830 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3831 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3832 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3833 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 3834 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3835 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3836 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3837 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 3838 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3839 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3840 | case Type::STK_Pointer: |
| 3841 | llvm_unreachable("valid float->pointer cast?"); |
| 3842 | case Type::STK_MemberPointer: |
| 3843 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3844 | } |
| 3845 | break; |
| 3846 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3847 | case Type::STK_FloatingComplex: |
| 3848 | switch (DestTy->getScalarTypeKind()) { |
| 3849 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3850 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3851 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3852 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3853 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3854 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3855 | if (S.Context.hasSameType(ET, DestTy)) |
| 3856 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3857 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3858 | return CK_FloatingCast; |
| 3859 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3860 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3861 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3862 | case Type::STK_Integral: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3863 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 3864 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3865 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3866 | case Type::STK_Pointer: |
| 3867 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3868 | case Type::STK_MemberPointer: |
| 3869 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3870 | } |
| 3871 | break; |
| 3872 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3873 | case Type::STK_IntegralComplex: |
| 3874 | switch (DestTy->getScalarTypeKind()) { |
| 3875 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3876 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3877 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3878 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3879 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3880 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3881 | if (S.Context.hasSameType(ET, DestTy)) |
| 3882 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3883 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3884 | return CK_IntegralCast; |
| 3885 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3886 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3887 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3888 | case Type::STK_Floating: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3889 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 3890 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3891 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3892 | case Type::STK_Pointer: |
| 3893 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3894 | case Type::STK_MemberPointer: |
| 3895 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3896 | } |
| 3897 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3898 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3899 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3900 | llvm_unreachable("Unhandled scalar cast"); |
| 3901 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3904 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3905 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 3906 | QualType castType, Expr *castExpr, |
| 3907 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3908 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3909 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 3910 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 3911 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3912 | if (getLangOptions().CPlusPlus) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3913 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3914 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3915 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3916 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3917 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3918 | assert(!castExpr->getType()->isPlaceholderType()); |
| 3919 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3920 | // We only support r-value casts in C. |
| 3921 | VK = VK_RValue; |
| 3922 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3923 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3924 | // type needs to be scalar. |
| 3925 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3926 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3927 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 3928 | if (castExprRes.isInvalid()) |
| 3929 | return ExprError(); |
| 3930 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3931 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3932 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3933 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3934 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3935 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3936 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3937 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 3938 | if (castExprRes.isInvalid()) |
| 3939 | return ExprError(); |
| 3940 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3941 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 3942 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 3943 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3944 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 3945 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3946 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3947 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3948 | (castType->isStructureType() || castType->isUnionType())) { |
| 3949 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3950 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3951 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3952 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3953 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3954 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3955 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3956 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3957 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3958 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3959 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3960 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3961 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3962 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3963 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 3964 | castExpr->getType()) && |
| 3965 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3966 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3967 | << castExpr->getSourceRange(); |
| 3968 | break; |
| 3969 | } |
| 3970 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3971 | if (Field == FieldEnd) { |
| 3972 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3973 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3974 | return ExprError(); |
| 3975 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3976 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3977 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3978 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3979 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3980 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3981 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3982 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3983 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3984 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3985 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3986 | // The type we're casting to is known to be a scalar or vector. |
| 3987 | |
| 3988 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3989 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3990 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3991 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3992 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3993 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3994 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3995 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3996 | |
| 3997 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3998 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3999 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4000 | if (castType->isVectorType()) { |
| 4001 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 4002 | VectorType::AltiVecVector && |
| 4003 | (castExpr->getType()->isIntegerType() || |
| 4004 | castExpr->getType()->isFloatingType())) { |
| 4005 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4006 | return Owned(castExpr); |
| 4007 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 4008 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4009 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4010 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4011 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4012 | if (castExpr->getType()->isVectorType()) { |
| 4013 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 4014 | return ExprError(); |
| 4015 | else |
| 4016 | return Owned(castExpr); |
| 4017 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4018 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4019 | // The source and target types are both scalars, i.e. |
| 4020 | // - arithmetic types (fundamental, enum, and complex) |
| 4021 | // - all kinds of pointers |
| 4022 | // Note that member pointers were filtered out with C++, above. |
| 4023 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4024 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 4025 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 4026 | return ExprError(); |
| 4027 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4028 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4029 | // If either type is a pointer, the other type has to be either an |
| 4030 | // integer or a pointer. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4031 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4032 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4033 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4034 | castExprType->isArithmeticType()) { |
| 4035 | Diag(castExpr->getLocStart(), |
| 4036 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4037 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4038 | return ExprError(); |
| 4039 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4040 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4041 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 4042 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4043 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4044 | return ExprError(); |
| 4045 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4046 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4047 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4048 | if (getLangOptions().ObjCAutoRefCount) { |
| 4049 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 4050 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 4051 | castType, castExpr, CCK_CStyleCast); |
| 4052 | |
| 4053 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 4054 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 4055 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4056 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4057 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4058 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4059 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 4060 | Diag(castExpr->getLocStart(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4061 | diag::err_typecheck_incompatible_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4062 | << castExprType << castType << AA_Casting |
| 4063 | << castExpr->getSourceRange(); |
| 4064 | |
| 4065 | return ExprError(); |
| 4066 | } |
| 4067 | } |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame^] | 4068 | } |
| 4069 | else { |
| 4070 | QualType canCastType = |
| 4071 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| 4072 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 4073 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 4074 | castExprType->isObjCObjectPointerType()) { |
| 4075 | if (const ObjCObjectPointerType *ObjT = |
| 4076 | castExprType->getAs<ObjCObjectPointerType>()) |
| 4077 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) { |
| 4078 | Diag(castExpr->getLocStart(), |
| 4079 | diag::err_arc_cast_of_weak_unavailable) |
| 4080 | << castExprType << castType |
| 4081 | << castExpr->getSourceRange(); |
| 4082 | return ExprError(); |
| 4083 | } |
| 4084 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } |
| 4087 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4088 | castExprRes = Owned(castExpr); |
| 4089 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 4090 | if (castExprRes.isInvalid()) |
| 4091 | return ExprError(); |
| 4092 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4093 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4094 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4095 | CheckCastAlign(castExpr, castType, TyR); |
| 4096 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4097 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4100 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4101 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4102 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4103 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4104 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4105 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4106 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4107 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4108 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4109 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4110 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4111 | } else |
| 4112 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4113 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4114 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4115 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4116 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4117 | return false; |
| 4118 | } |
| 4119 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4120 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4121 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4122 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4123 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4124 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4125 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4126 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4127 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4128 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4129 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4130 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4131 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4132 | return ExprError(); |
| 4133 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4134 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4135 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4138 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4139 | // conversion will take place first from scalar to elt type, and then |
| 4140 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4141 | if (SrcTy->isPointerType()) |
| 4142 | return Diag(R.getBegin(), |
| 4143 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4144 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4145 | |
| 4146 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4147 | ExprResult CastExprRes = Owned(CastExpr); |
| 4148 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4149 | if (CastExprRes.isInvalid()) |
| 4150 | return ExprError(); |
| 4151 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4152 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4153 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4154 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4157 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4158 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4159 | Declarator &D, ParsedType &Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4160 | SourceLocation RParenLoc, Expr *castExpr) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4161 | assert(!D.isInvalidType() && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4162 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4163 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4164 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, castExpr->getType()); |
| 4165 | if (D.isInvalidType()) |
| 4166 | return ExprError(); |
| 4167 | |
| 4168 | if (getLangOptions().CPlusPlus) { |
| 4169 | // Check that there are no default arguments (C++ only). |
| 4170 | CheckExtraCXXDefaultArguments(D); |
| 4171 | } |
| 4172 | |
| 4173 | QualType castType = castTInfo->getType(); |
| 4174 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4176 | bool isVectorLiteral = false; |
| 4177 | |
| 4178 | // Check for an altivec or OpenCL literal, |
| 4179 | // i.e. all the elements are integer constants. |
| 4180 | ParenExpr *PE = dyn_cast<ParenExpr>(castExpr); |
| 4181 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(castExpr); |
| 4182 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4183 | if (PLE && PLE->getNumExprs() == 0) { |
| 4184 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4185 | return ExprError(); |
| 4186 | } |
| 4187 | if (PE || PLE->getNumExprs() == 1) { |
| 4188 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4189 | if (!E->getType()->isVectorType()) |
| 4190 | isVectorLiteral = true; |
| 4191 | } |
| 4192 | else |
| 4193 | isVectorLiteral = true; |
| 4194 | } |
| 4195 | |
| 4196 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4197 | // then handle it as such. |
| 4198 | if (isVectorLiteral) |
| 4199 | return BuildVectorLiteral(LParenLoc, RParenLoc, castExpr, castTInfo); |
| 4200 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4201 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4202 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4203 | // sequence of BinOp comma operators. |
| 4204 | if (isa<ParenListExpr>(castExpr)) { |
| 4205 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, castExpr); |
| 4206 | if (Result.isInvalid()) return ExprError(); |
| 4207 | castExpr = Result.take(); |
| 4208 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4209 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4210 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4213 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4214 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4215 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4216 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4217 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4218 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4219 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4220 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 4221 | castExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4222 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4223 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4224 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4225 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4226 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4227 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4228 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4229 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4232 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4233 | SourceLocation RParenLoc, Expr *E, |
| 4234 | TypeSourceInfo *TInfo) { |
| 4235 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4236 | "Expected paren or paren list expression"); |
| 4237 | |
| 4238 | Expr **exprs; |
| 4239 | unsigned numExprs; |
| 4240 | Expr *subExpr; |
| 4241 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4242 | exprs = PE->getExprs(); |
| 4243 | numExprs = PE->getNumExprs(); |
| 4244 | } else { |
| 4245 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4246 | exprs = &subExpr; |
| 4247 | numExprs = 1; |
| 4248 | } |
| 4249 | |
| 4250 | QualType Ty = TInfo->getType(); |
| 4251 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4252 | |
| 4253 | llvm::SmallVector<Expr *, 8> initExprs; |
| 4254 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4255 | // initializers must be one or must match the size of the vector. |
| 4256 | // If a single value is specified in the initializer then it will be |
| 4257 | // replicated to all the components of the vector |
| 4258 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 4259 | VectorType::AltiVecVector) { |
| 4260 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4261 | // The number of initializers must be one or must match the size of the |
| 4262 | // vector. If a single value is specified in the initializer then it will |
| 4263 | // be replicated to all the components of the vector |
| 4264 | if (numExprs == 1) { |
| 4265 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4266 | ExprResult Literal = Owned(exprs[0]); |
| 4267 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4268 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4269 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4270 | } |
| 4271 | else if (numExprs < numElems) { |
| 4272 | Diag(E->getExprLoc(), |
| 4273 | diag::err_incorrect_number_of_vector_initializers); |
| 4274 | return ExprError(); |
| 4275 | } |
| 4276 | else |
| 4277 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4278 | initExprs.push_back(exprs[i]); |
| 4279 | } |
| 4280 | else |
| 4281 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4282 | initExprs.push_back(exprs[i]); |
| 4283 | |
| 4284 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4285 | // braces instead of the original commas. |
| 4286 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4287 | &initExprs[0], |
| 4288 | initExprs.size(), RParenLoc); |
| 4289 | initE->setType(Ty); |
| 4290 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4291 | } |
| 4292 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4293 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4294 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4295 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4296 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4297 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4298 | if (!E) |
| 4299 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4301 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4303 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4304 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4305 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4307 | if (Result.isInvalid()) return ExprError(); |
| 4308 | |
| 4309 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4312 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4313 | SourceLocation R, |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4314 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4315 | unsigned nexprs = Val.size(); |
| 4316 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4317 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4318 | Expr *expr; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4319 | if (nexprs == 1) |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4320 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4321 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4322 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4323 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4324 | return Owned(expr); |
| 4325 | } |
| 4326 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4327 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 4328 | /// constant and the other is not a pointer. |
| 4329 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 4330 | SourceLocation QuestionLoc) { |
| 4331 | Expr *NullExpr = LHS; |
| 4332 | Expr *NonPointerExpr = RHS; |
| 4333 | Expr::NullPointerConstantKind NullKind = |
| 4334 | NullExpr->isNullPointerConstant(Context, |
| 4335 | Expr::NPC_ValueDependentIsNotNull); |
| 4336 | |
| 4337 | if (NullKind == Expr::NPCK_NotNull) { |
| 4338 | NullExpr = RHS; |
| 4339 | NonPointerExpr = LHS; |
| 4340 | NullKind = |
| 4341 | NullExpr->isNullPointerConstant(Context, |
| 4342 | Expr::NPC_ValueDependentIsNotNull); |
| 4343 | } |
| 4344 | |
| 4345 | if (NullKind == Expr::NPCK_NotNull) |
| 4346 | return false; |
| 4347 | |
| 4348 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4349 | // In this case, check to make sure that we got here from a "NULL" |
| 4350 | // string in the source code. |
| 4351 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4352 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4353 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4354 | return false; |
| 4355 | } |
| 4356 | |
| 4357 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4358 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4359 | << NonPointerExpr->getType() << DiagType |
| 4360 | << NonPointerExpr->getSourceRange(); |
| 4361 | return true; |
| 4362 | } |
| 4363 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4364 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4365 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4366 | /// C99 6.5.15 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4367 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4368 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4369 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4370 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4371 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4372 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4373 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4374 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4375 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4376 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4377 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4378 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4379 | // C++ is sufficiently different to merit its own checker. |
| 4380 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4381 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4382 | |
| 4383 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4384 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4385 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4386 | Cond = UsualUnaryConversions(Cond.take()); |
| 4387 | if (Cond.isInvalid()) |
| 4388 | return QualType(); |
| 4389 | LHS = UsualUnaryConversions(LHS.take()); |
| 4390 | if (LHS.isInvalid()) |
| 4391 | return QualType(); |
| 4392 | RHS = UsualUnaryConversions(RHS.take()); |
| 4393 | if (RHS.isInvalid()) |
| 4394 | return QualType(); |
| 4395 | |
| 4396 | QualType CondTy = Cond.get()->getType(); |
| 4397 | QualType LHSTy = LHS.get()->getType(); |
| 4398 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4400 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4401 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4402 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4403 | // Throw an error if its not either. |
| 4404 | if (getLangOptions().OpenCL) { |
| 4405 | if (!CondTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4406 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4407 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4408 | << CondTy; |
| 4409 | return QualType(); |
| 4410 | } |
| 4411 | } |
| 4412 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4413 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4414 | << CondTy; |
| 4415 | return QualType(); |
| 4416 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4417 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4418 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4419 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4420 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4421 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4422 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4423 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4424 | // attempt to implicity convert them to the vector type to act like the |
| 4425 | // built in select. |
| 4426 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 4427 | // Both operands should be of scalar type. |
| 4428 | if (!LHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4429 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4430 | << CondTy; |
| 4431 | return QualType(); |
| 4432 | } |
| 4433 | if (!RHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4434 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4435 | << CondTy; |
| 4436 | return QualType(); |
| 4437 | } |
| 4438 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4439 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4440 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4443 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4444 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4445 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4446 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4447 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4448 | return QualType(); |
| 4449 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4450 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4451 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4452 | // If both operands are the same structure or union type, the result is that |
| 4453 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4454 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4455 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4456 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4457 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4458 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4459 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4460 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4461 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4462 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4463 | // 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] | 4464 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4465 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4466 | if (!LHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4467 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4468 | << RHS.get()->getSourceRange(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4469 | if (!RHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4470 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4471 | << LHS.get()->getSourceRange(); |
| 4472 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 4473 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4474 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4475 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4476 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4477 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4478 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4479 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4480 | // promote the null to a pointer. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4481 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4482 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4483 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4484 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4485 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 4486 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4487 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4488 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4489 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4490 | // All objective-c pointer type analysis is done here. |
| 4491 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4492 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4493 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4494 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4495 | if (!compositeType.isNull()) |
| 4496 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4497 | |
| 4498 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4499 | // Handle block pointer types. |
| 4500 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4501 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4502 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4503 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4504 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4505 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4506 | return destType; |
| 4507 | } |
| 4508 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4509 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4510 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4511 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4512 | // We have 2 block pointer types. |
| 4513 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4514 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4515 | return LHSTy; |
| 4516 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4517 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4518 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4519 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4520 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4521 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4522 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4523 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4524 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4525 | // In this situation, we assume void* type. No especially good |
| 4526 | // reason, but this is what gcc does, and we do have to pick |
| 4527 | // to get a consistent AST. |
| 4528 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4529 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4530 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4531 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4532 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4533 | // The block pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4534 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4535 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4536 | return LHSTy; |
| 4537 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4538 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4539 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4540 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4541 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4542 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4543 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4544 | |
| 4545 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4546 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4547 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4548 | QualType destPointee |
| 4549 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4550 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4551 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4552 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4553 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4554 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4555 | return destType; |
| 4556 | } |
| 4557 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4558 | QualType destPointee |
| 4559 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4560 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4561 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4562 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4563 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4564 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4565 | return destType; |
| 4566 | } |
| 4567 | |
| 4568 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4569 | // Two identical pointer types are always compatible. |
| 4570 | return LHSTy; |
| 4571 | } |
| 4572 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4573 | rhptee.getUnqualifiedType())) { |
| 4574 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4575 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4576 | // In this situation, we assume void* type. No especially good |
| 4577 | // reason, but this is what gcc does, and we do have to pick |
| 4578 | // to get a consistent AST. |
| 4579 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4580 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4581 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4582 | return incompatTy; |
| 4583 | } |
| 4584 | // The pointer types are compatible. |
| 4585 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4586 | // differently qualified versions of compatible types, the result type is |
| 4587 | // a pointer to an appropriately qualified version of the *composite* |
| 4588 | // type. |
| 4589 | // FIXME: Need to calculate the composite type. |
| 4590 | // FIXME: Need to add qualifiers |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4591 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4592 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4593 | return LHSTy; |
| 4594 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4596 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4597 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4598 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4599 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4600 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 4601 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4602 | return RHSTy; |
| 4603 | } |
| 4604 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4605 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4606 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 4607 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4608 | return LHSTy; |
| 4609 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4610 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4611 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4612 | // constant and the other is not a pointer type. In this case, the user most |
| 4613 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4614 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4615 | return QualType(); |
| 4616 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4617 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4618 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4619 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4620 | return QualType(); |
| 4621 | } |
| 4622 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4623 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4624 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4625 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4626 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4627 | QualType LHSTy = LHS.get()->getType(); |
| 4628 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4629 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4630 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4631 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4632 | // redefinition type if an attempt is made to access its fields. |
| 4633 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4634 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4635 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4636 | return LHSTy; |
| 4637 | } |
| 4638 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4639 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4640 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4641 | return RHSTy; |
| 4642 | } |
| 4643 | // And the same for struct objc_object* / id |
| 4644 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4645 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4646 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4647 | return LHSTy; |
| 4648 | } |
| 4649 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4650 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4651 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4652 | return RHSTy; |
| 4653 | } |
| 4654 | // And the same for struct objc_selector* / SEL |
| 4655 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4656 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4657 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4658 | return LHSTy; |
| 4659 | } |
| 4660 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4661 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4662 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4663 | return RHSTy; |
| 4664 | } |
| 4665 | // Check constraints for Objective-C object pointers types. |
| 4666 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4667 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4668 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4669 | // Two identical object pointer types are always compatible. |
| 4670 | return LHSTy; |
| 4671 | } |
| 4672 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4673 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4674 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4675 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4676 | // If both operands are interfaces and either operand can be |
| 4677 | // assigned to the other, use that type as the composite |
| 4678 | // type. This allows |
| 4679 | // xxx ? (A*) a : (B*) b |
| 4680 | // where B is a subclass of A. |
| 4681 | // |
| 4682 | // Additionally, as for assignment, if either type is 'id' |
| 4683 | // allow silent coercion. Finally, if the types are |
| 4684 | // incompatible then make sure to use 'id' as the composite |
| 4685 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4686 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4687 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4688 | // It could return the composite type. |
| 4689 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4690 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4691 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4692 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4693 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4694 | RHSTy->isObjCQualifiedIdType()) && |
| 4695 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4696 | // Need to handle "id<xx>" explicitly. |
| 4697 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4698 | // id. Currently localizing to here until clear this should be |
| 4699 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4700 | compositeType = Context.getObjCIdType(); |
| 4701 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4702 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4703 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4704 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4705 | ; |
| 4706 | else { |
| 4707 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4708 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4709 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4710 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4711 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4712 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4713 | return incompatTy; |
| 4714 | } |
| 4715 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4716 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4717 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4718 | return compositeType; |
| 4719 | } |
| 4720 | // Check Objective-C object pointer types and 'void *' |
| 4721 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4722 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4723 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4724 | QualType destPointee |
| 4725 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4726 | QualType destType = Context.getPointerType(destPointee); |
| 4727 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4728 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4729 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4730 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4731 | return destType; |
| 4732 | } |
| 4733 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4734 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4735 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4736 | QualType destPointee |
| 4737 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4738 | QualType destType = Context.getPointerType(destPointee); |
| 4739 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4740 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4741 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4742 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4743 | return destType; |
| 4744 | } |
| 4745 | return QualType(); |
| 4746 | } |
| 4747 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4748 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4749 | /// ParenRange in parentheses. |
| 4750 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4751 | const PartialDiagnostic &Note, |
| 4752 | SourceRange ParenRange) { |
| 4753 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4754 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4755 | EndLoc.isValid()) { |
| 4756 | Self.Diag(Loc, Note) |
| 4757 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4758 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4759 | } else { |
| 4760 | // We can't display the parentheses, so just show the bare note. |
| 4761 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4762 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4766 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4767 | } |
| 4768 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4769 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4770 | /// expression, either using a built-in or overloaded operator, |
| 4771 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 4772 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 4773 | Expr **RHS) { |
| 4774 | E = E->IgnoreParenImpCasts(); |
| 4775 | E = E->IgnoreConversionOperator(); |
| 4776 | E = E->IgnoreParenImpCasts(); |
| 4777 | |
| 4778 | // Built-in binary operator. |
| 4779 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4780 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4781 | *Opcode = OP->getOpcode(); |
| 4782 | *RHS = OP->getRHS(); |
| 4783 | return true; |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | // Overloaded operator. |
| 4788 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4789 | if (Call->getNumArgs() != 2) |
| 4790 | return false; |
| 4791 | |
| 4792 | // Make sure this is really a binary operator that is safe to pass into |
| 4793 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4794 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4795 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4796 | return false; |
| 4797 | |
| 4798 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4799 | if (IsArithmeticOp(OpKind)) { |
| 4800 | *Opcode = OpKind; |
| 4801 | *RHS = Call->getArg(1); |
| 4802 | return true; |
| 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | return false; |
| 4807 | } |
| 4808 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4809 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4810 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4811 | } |
| 4812 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4813 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4814 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4815 | /// commonly interpreted as boolean. |
| 4816 | static bool ExprLooksBoolean(Expr *E) { |
| 4817 | E = E->IgnoreParenImpCasts(); |
| 4818 | |
| 4819 | if (E->getType()->isBooleanType()) |
| 4820 | return true; |
| 4821 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4822 | return IsLogicOp(OP->getOpcode()); |
| 4823 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4824 | return OP->getOpcode() == UO_LNot; |
| 4825 | |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4829 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4830 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4831 | /// the conditional operator has higher precedence, for example: |
| 4832 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4833 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4834 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4835 | Expr *Condition, |
| 4836 | Expr *LHS, |
| 4837 | Expr *RHS) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4838 | BinaryOperatorKind CondOpcode; |
| 4839 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4840 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4841 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4842 | return; |
| 4843 | if (!ExprLooksBoolean(CondRHS)) |
| 4844 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4845 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4846 | // The condition is an arithmetic binary expression, with a right- |
| 4847 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4848 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4849 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4850 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4851 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4852 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4853 | SuggestParentheses(Self, OpLoc, |
| 4854 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 4855 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 4856 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 4857 | |
| 4858 | SuggestParentheses(Self, OpLoc, |
| 4859 | Self.PDiag(diag::note_precedence_conditional_first), |
| 4860 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4863 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4864 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4865 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4866 | SourceLocation ColonLoc, |
| 4867 | Expr *CondExpr, Expr *LHSExpr, |
| 4868 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4869 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4870 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4871 | OpaqueValueExpr *opaqueValue = 0; |
| 4872 | Expr *commonExpr = 0; |
| 4873 | if (LHSExpr == 0) { |
| 4874 | commonExpr = CondExpr; |
| 4875 | |
| 4876 | // We usually want to apply unary conversions *before* saving, except |
| 4877 | // in the special case of a C++ l-value conditional. |
| 4878 | if (!(getLangOptions().CPlusPlus |
| 4879 | && !commonExpr->isTypeDependent() |
| 4880 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 4881 | && commonExpr->isGLValue() |
| 4882 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 4883 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 4884 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4885 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 4886 | if (commonRes.isInvalid()) |
| 4887 | return ExprError(); |
| 4888 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 4892 | commonExpr->getType(), |
| 4893 | commonExpr->getValueKind(), |
| 4894 | commonExpr->getObjectKind()); |
| 4895 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4896 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4897 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4898 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4899 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4900 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 4901 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4902 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4903 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 4904 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4905 | return ExprError(); |
| 4906 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4907 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 4908 | RHS.get()); |
| 4909 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4910 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4911 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 4912 | LHS.take(), ColonLoc, |
| 4913 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4914 | |
| 4915 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4916 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 4917 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4920 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4921 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4922 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4923 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4924 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4925 | static Sema::AssignConvertType |
| 4926 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 4927 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 4928 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4929 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4930 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4931 | const Type *lhptee, *rhptee; |
| 4932 | Qualifiers lhq, rhq; |
| 4933 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 4934 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4935 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4936 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4937 | |
| 4938 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4939 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4940 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4941 | Qualifiers lq; |
| 4942 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4943 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 4944 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 4945 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 4946 | // Ignore lifetime for further calculation. |
| 4947 | lhq.removeObjCLifetime(); |
| 4948 | rhq.removeObjCLifetime(); |
| 4949 | } |
| 4950 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4951 | if (!lhq.compatiblyIncludes(rhq)) { |
| 4952 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 4953 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 4954 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 4955 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4956 | // 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] | 4957 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4958 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 4959 | .compatiblyIncludes( |
| 4960 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 4961 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 4962 | ; // keep old |
| 4963 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4964 | // Treat lifetime mismatches as fatal. |
| 4965 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 4966 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 4967 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4968 | // For GCC compatibility, other qualifier mismatches are treated |
| 4969 | // as still compatible in C. |
| 4970 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 4971 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4972 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4973 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4974 | // 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] | 4975 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4976 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4977 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4978 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4979 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4980 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4981 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4982 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4983 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4984 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4985 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4986 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4987 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4988 | |
| 4989 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4990 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 4991 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4992 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4993 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4994 | // 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] | 4995 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 4996 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 4997 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4998 | // Check if the pointee types are compatible ignoring the sign. |
| 4999 | // We explicitly check for char so that we catch "char" vs |
| 5000 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5001 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5002 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5003 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5004 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5005 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5006 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5007 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5008 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5009 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5010 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5011 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5012 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5013 | // takes priority over sign incompatibility because the sign |
| 5014 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5015 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5016 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5017 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5018 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5019 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5020 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5021 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5022 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5023 | // the eventual target type is the same and the pointers have the same |
| 5024 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5025 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5026 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5027 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5028 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5029 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5030 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5031 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5032 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5033 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5034 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5035 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5036 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5037 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5038 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5041 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5042 | /// block pointer types are compatible or whether a block and normal pointer |
| 5043 | /// are compatible. It is more restrict than comparing two function pointer |
| 5044 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5045 | static Sema::AssignConvertType |
| 5046 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5047 | QualType rhsType) { |
| 5048 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5049 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5050 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5051 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5052 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5053 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5054 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5055 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5056 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5057 | // In C++, the types have to match exactly. |
| 5058 | if (S.getLangOptions().CPlusPlus) |
| 5059 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5060 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5061 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5062 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5063 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5064 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5065 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5066 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5067 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5068 | return Sema::IncompatibleBlockPointer; |
| 5069 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5070 | return ConvTy; |
| 5071 | } |
| 5072 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5073 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5074 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5075 | static Sema::AssignConvertType |
| 5076 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5077 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5078 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5079 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5080 | if (lhsType->isObjCBuiltinType()) { |
| 5081 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5082 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5083 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5084 | return Sema::IncompatiblePointer; |
| 5085 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5086 | } |
| 5087 | if (rhsType->isObjCBuiltinType()) { |
| 5088 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5089 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5090 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5091 | return Sema::IncompatiblePointer; |
| 5092 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5093 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5094 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5095 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5096 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5097 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5098 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5099 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5100 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5101 | |
| 5102 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5103 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5104 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5105 | return Sema::IncompatibleObjCQualifiedId; |
| 5106 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5109 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5110 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5111 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5112 | // Fake up an opaque expression. We don't actually care about what |
| 5113 | // cast operations are required, so if CheckAssignmentConstraints |
| 5114 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5115 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5116 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5117 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5118 | CastKind K = CK_Invalid; |
| 5119 | |
| 5120 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5121 | } |
| 5122 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5123 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5124 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5125 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5126 | /// |
| 5127 | /// int a, *pint; |
| 5128 | /// short *pshort; |
| 5129 | /// struct foo *pfoo; |
| 5130 | /// |
| 5131 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5132 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5133 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5134 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5135 | /// |
| 5136 | /// 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] | 5137 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5138 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5139 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5140 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5141 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5142 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5143 | QualType rhsType = rhs.get()->getType(); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame^] | 5144 | QualType origLhsType = lhsType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5145 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5146 | // Get canonical types. We're not formatting these types, just comparing |
| 5147 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5148 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5149 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5150 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5151 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5152 | if (lhsType == rhsType) { |
| 5153 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5154 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5155 | } |
| 5156 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5157 | // If the left-hand side is a reference type, then we are in a |
| 5158 | // (rare!) case where we've allowed the use of references in C, |
| 5159 | // e.g., as a parameter type in a built-in function. In this case, |
| 5160 | // just make sure that the type referenced is compatible with the |
| 5161 | // right-hand side type. The caller is responsible for adjusting |
| 5162 | // lhsType so that the resulting expression does not have reference |
| 5163 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5164 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5165 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5166 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5167 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5168 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5169 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5170 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5171 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5172 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5173 | // to the same ExtVector type. |
| 5174 | if (lhsType->isExtVectorType()) { |
| 5175 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5176 | return Incompatible; |
| 5177 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5178 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5179 | // element type. |
| 5180 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5181 | if (elType != rhsType) { |
| 5182 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5183 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5184 | } |
| 5185 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5186 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5187 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5189 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5190 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5191 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5192 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5193 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5194 | // vector type and vice versa |
| 5195 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5196 | Kind = CK_BitCast; |
| 5197 | return Compatible; |
| 5198 | } |
| 5199 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5200 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5201 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5202 | // no bits are changed but the result type is different. |
| 5203 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5204 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5205 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5206 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5207 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5208 | } |
| 5209 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5210 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5211 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5212 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5213 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5214 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5215 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5216 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5217 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5218 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5219 | // Conversions to normal pointers. |
| 5220 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 5221 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5222 | if (isa<PointerType>(rhsType)) { |
| 5223 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5224 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5225 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5226 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5227 | // int -> T* |
| 5228 | if (rhsType->isIntegerType()) { |
| 5229 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5230 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5231 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5232 | |
| 5233 | // C pointers are not compatible with ObjC object pointers, |
| 5234 | // with two exceptions: |
| 5235 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5236 | // - conversions to void* |
| 5237 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5238 | Kind = CK_AnyPointerToObjCPointerCast; |
| 5239 | return Compatible; |
| 5240 | } |
| 5241 | |
| 5242 | // - conversions from 'Class' to the redefinition type |
| 5243 | if (rhsType->isObjCClassType() && |
| 5244 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5245 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5246 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5247 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5248 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5249 | Kind = CK_BitCast; |
| 5250 | return IncompatiblePointer; |
| 5251 | } |
| 5252 | |
| 5253 | // U^ -> void* |
| 5254 | if (rhsType->getAs<BlockPointerType>()) { |
| 5255 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5256 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5257 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5258 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5259 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5260 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5261 | return Incompatible; |
| 5262 | } |
| 5263 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5264 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5265 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5266 | // U^ -> T^ |
| 5267 | if (rhsType->isBlockPointerType()) { |
| 5268 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5269 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5273 | if (rhsType->isIntegerType()) { |
| 5274 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5275 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5278 | // id -> T^ |
| 5279 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 5280 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5281 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5282 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5283 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5284 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5285 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5286 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5287 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5288 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5289 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5290 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5291 | return Incompatible; |
| 5292 | } |
| 5293 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5294 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5295 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5296 | // A* -> B* |
| 5297 | if (rhsType->isObjCObjectPointerType()) { |
| 5298 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame^] | 5299 | Sema::AssignConvertType result = |
| 5300 | checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
| 5301 | if (getLangOptions().ObjCAutoRefCount && |
| 5302 | result == Compatible && |
| 5303 | origLhsType.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 5304 | if (const ObjCObjectPointerType *ObjT = |
| 5305 | rhsType->getAs<ObjCObjectPointerType>()) |
| 5306 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) |
| 5307 | result = IncompatibleObjCWeakRef; |
| 5308 | } |
| 5309 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5313 | if (rhsType->isIntegerType()) { |
| 5314 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5315 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5318 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5319 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5320 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5321 | // - conversions from 'void*' |
| 5322 | if (rhsType->isVoidPointerType()) { |
| 5323 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5324 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | // - conversions to 'Class' from its redefinition type |
| 5328 | if (lhsType->isObjCClassType() && |
| 5329 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 5330 | Kind = CK_BitCast; |
| 5331 | return Compatible; |
| 5332 | } |
| 5333 | |
| 5334 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5335 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5336 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5337 | |
| 5338 | // T^ -> A* |
| 5339 | if (rhsType->isBlockPointerType()) { |
| 5340 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5341 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5344 | return Incompatible; |
| 5345 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5346 | |
| 5347 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5348 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5349 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5350 | if (lhsType == Context.BoolTy) { |
| 5351 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5352 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5353 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5354 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5355 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5356 | if (lhsType->isIntegerType()) { |
| 5357 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5358 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5359 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5360 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5361 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5362 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5363 | |
| 5364 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5365 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5366 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5367 | if (lhsType == Context.BoolTy) { |
| 5368 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5369 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5370 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5371 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5372 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5373 | if (lhsType->isIntegerType()) { |
| 5374 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5375 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5378 | return Incompatible; |
| 5379 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5380 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5381 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5382 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5383 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 5384 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5385 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5386 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5387 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5389 | return Incompatible; |
| 5390 | } |
| 5391 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5392 | /// \brief Constructs a transparent union from an expression that is |
| 5393 | /// used to initialize the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5394 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5395 | QualType UnionType, FieldDecl *Field) { |
| 5396 | // Build an initializer list that designates the appropriate member |
| 5397 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5398 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5399 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5400 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5401 | SourceLocation()); |
| 5402 | Initializer->setType(UnionType); |
| 5403 | Initializer->setInitializedFieldInUnion(Field); |
| 5404 | |
| 5405 | // Build a compound literal constructing a value of the transparent |
| 5406 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5407 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5408 | EResult = S.Owned( |
| 5409 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5410 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5414 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 5415 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5417 | // 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] | 5418 | // transparent_union GCC extension. |
| 5419 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5420 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5421 | return Incompatible; |
| 5422 | |
| 5423 | // The field to initialize within the transparent union. |
| 5424 | RecordDecl *UD = UT->getDecl(); |
| 5425 | FieldDecl *InitField = 0; |
| 5426 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5427 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5428 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5429 | it != itend; ++it) { |
| 5430 | if (it->getType()->isPointerType()) { |
| 5431 | // If the transparent union contains a pointer type, we allow: |
| 5432 | // 1) void pointer |
| 5433 | // 2) null pointer constant |
| 5434 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5435 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5436 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5437 | InitField = *it; |
| 5438 | break; |
| 5439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5440 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5441 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5442 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5443 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5444 | InitField = *it; |
| 5445 | break; |
| 5446 | } |
| 5447 | } |
| 5448 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5449 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5450 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5451 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5452 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5453 | InitField = *it; |
| 5454 | break; |
| 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | if (!InitField) |
| 5459 | return Incompatible; |
| 5460 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5461 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5462 | return Compatible; |
| 5463 | } |
| 5464 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5465 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5466 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5467 | if (getLangOptions().CPlusPlus) { |
| 5468 | if (!lhsType->isRecordType()) { |
| 5469 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5470 | // expression is implicitly converted (C++ 4) to the |
| 5471 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5472 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 5473 | lhsType.getUnqualifiedType(), |
| 5474 | AA_Assigning); |
| 5475 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5476 | return Incompatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5477 | rExpr = move(Res); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 5478 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5482 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5483 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5484 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5485 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5486 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | if ((lhsType->isPointerType() || |
| 5488 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5489 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5490 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5491 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5492 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5493 | return Compatible; |
| 5494 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5495 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5496 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5497 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5498 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5499 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5500 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5501 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5502 | if (!lhsType->isReferenceType()) { |
| 5503 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 5504 | if (rExpr.isInvalid()) |
| 5505 | return Incompatible; |
| 5506 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5507 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5508 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5509 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5510 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5511 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5512 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5513 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5514 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5515 | // so that we can use references in built-in functions even in C. |
| 5516 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5517 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5518 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 5519 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5520 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5523 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5524 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5525 | << lex.get()->getType() << rex.get()->getType() |
| 5526 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5527 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5530 | QualType Sema::CheckVectorOperands(ExprResult &lex, ExprResult &rex, |
| 5531 | SourceLocation Loc, bool isCompAssign) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5532 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5533 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5534 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5535 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5536 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5537 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5538 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5539 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5540 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5541 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5543 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5544 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5545 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5546 | if (lhsType->isExtVectorType()) { |
| 5547 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5548 | return lhsType; |
| 5549 | } |
| 5550 | |
| 5551 | if (!isCompAssign) |
| 5552 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5553 | return rhsType; |
| 5554 | } |
| 5555 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5556 | if (getLangOptions().LaxVectorConversions && |
| 5557 | Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) { |
| 5558 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5559 | // vectors, the total size only needs to be the same. This is a |
| 5560 | // bitcast; no bits are changed but the result type is different. |
| 5561 | // FIXME: Should we really be allowing this? |
| 5562 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5563 | return lhsType; |
| 5564 | } |
| 5565 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5566 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5567 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5568 | bool swapped = false; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5569 | if (rhsType->isExtVectorType() && !isCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5570 | swapped = true; |
| 5571 | std::swap(rex, lex); |
| 5572 | std::swap(rhsType, lhsType); |
| 5573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5574 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5575 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5576 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5577 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5578 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5579 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 5580 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5581 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5582 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5583 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5584 | if (swapped) std::swap(rex, lex); |
| 5585 | return lhsType; |
| 5586 | } |
| 5587 | } |
| 5588 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5589 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5590 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 5591 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5592 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5593 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5595 | if (swapped) std::swap(rex, lex); |
| 5596 | return lhsType; |
| 5597 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5598 | } |
| 5599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5601 | // Vectors of different size or scalar and non-ext-vector are errors. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5602 | if (swapped) std::swap(rex, lex); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5603 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5604 | << lex.get()->getType() << rex.get()->getType() |
| 5605 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5606 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5609 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5610 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 5611 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5612 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5613 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5614 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5615 | if (lex.isInvalid() || rex.isInvalid()) |
| 5616 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5617 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5618 | if (!lex.get()->getType()->isArithmeticType() || |
| 5619 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5620 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5621 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5622 | // Check for division by zero. |
| 5623 | if (isDiv && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5624 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 5625 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 5626 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5627 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5628 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5631 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5632 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 5633 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 5634 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 5635 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5636 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5637 | return InvalidOperands(Loc, lex, rex); |
| 5638 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5639 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5640 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | if (lex.isInvalid() || rex.isInvalid()) |
| 5642 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5643 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5644 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5645 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5646 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5647 | // Check for remainder by zero. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5648 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 5649 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 5650 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5651 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5652 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5655 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5656 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
| 5657 | Expr *LHS, Expr *RHS) { |
| 5658 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5659 | ? diag::err_typecheck_pointer_arith_void_type |
| 5660 | : diag::ext_gnu_void_ptr) |
| 5661 | << 1 /* two pointers */ << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5662 | } |
| 5663 | |
| 5664 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5665 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5666 | Expr *Pointer) { |
| 5667 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5668 | ? diag::err_typecheck_pointer_arith_void_type |
| 5669 | : diag::ext_gnu_void_ptr) |
| 5670 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5671 | } |
| 5672 | |
| 5673 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5674 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5675 | Expr *LHS, Expr *RHS) { |
| 5676 | assert(LHS->getType()->isAnyPointerType()); |
| 5677 | assert(RHS->getType()->isAnyPointerType()); |
| 5678 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5679 | ? diag::err_typecheck_pointer_arith_function_type |
| 5680 | : diag::ext_gnu_ptr_func_arith) |
| 5681 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5682 | // We only show the second type if it differs from the first. |
| 5683 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5684 | RHS->getType()) |
| 5685 | << RHS->getType()->getPointeeType() |
| 5686 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5687 | } |
| 5688 | |
| 5689 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5690 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5691 | Expr *Pointer) { |
| 5692 | assert(Pointer->getType()->isAnyPointerType()); |
| 5693 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5694 | ? diag::err_typecheck_pointer_arith_function_type |
| 5695 | : diag::ext_gnu_ptr_func_arith) |
| 5696 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5697 | << 0 /* one pointer, so only one type */ |
| 5698 | << Pointer->getSourceRange(); |
| 5699 | } |
| 5700 | |
| 5701 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5702 | /// |
| 5703 | /// If the operand has pointer type, this code will check for pointer types |
| 5704 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5705 | /// appropriately, including whether or not the use is supported as an |
| 5706 | /// extension. |
| 5707 | /// |
| 5708 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5709 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5710 | Expr *Operand) { |
| 5711 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5712 | |
| 5713 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5714 | if (PointeeTy->isVoidType()) { |
| 5715 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5716 | return !S.getLangOptions().CPlusPlus; |
| 5717 | } |
| 5718 | if (PointeeTy->isFunctionType()) { |
| 5719 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5720 | return !S.getLangOptions().CPlusPlus; |
| 5721 | } |
| 5722 | |
| 5723 | if ((Operand->getType()->isPointerType() && |
| 5724 | !Operand->getType()->isDependentType()) || |
| 5725 | Operand->getType()->isObjCObjectPointerType()) { |
| 5726 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5727 | if (S.RequireCompleteType( |
| 5728 | Loc, PointeeTy, |
| 5729 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5730 | << PointeeTy << Operand->getSourceRange())) |
| 5731 | return false; |
| 5732 | } |
| 5733 | |
| 5734 | return true; |
| 5735 | } |
| 5736 | |
| 5737 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5738 | /// operands. |
| 5739 | /// |
| 5740 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5741 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5742 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5743 | /// are (potentially problematic) pointers. |
| 5744 | /// |
| 5745 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5746 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
| 5747 | Expr *LHS, Expr *RHS) { |
| 5748 | bool isLHSPointer = LHS->getType()->isAnyPointerType(); |
| 5749 | bool isRHSPointer = RHS->getType()->isAnyPointerType(); |
| 5750 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5751 | |
| 5752 | QualType LHSPointeeTy, RHSPointeeTy; |
| 5753 | if (isLHSPointer) LHSPointeeTy = LHS->getType()->getPointeeType(); |
| 5754 | if (isRHSPointer) RHSPointeeTy = RHS->getType()->getPointeeType(); |
| 5755 | |
| 5756 | // Check for arithmetic on pointers to incomplete types. |
| 5757 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5758 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5759 | if (isLHSVoidPtr || isRHSVoidPtr) { |
| 5760 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHS); |
| 5761 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHS); |
| 5762 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHS, RHS); |
| 5763 | |
| 5764 | return !S.getLangOptions().CPlusPlus; |
| 5765 | } |
| 5766 | |
| 5767 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5768 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5769 | if (isLHSFuncPtr || isRHSFuncPtr) { |
| 5770 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHS); |
| 5771 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, RHS); |
| 5772 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHS, RHS); |
| 5773 | |
| 5774 | return !S.getLangOptions().CPlusPlus; |
| 5775 | } |
| 5776 | |
| 5777 | Expr *Operands[] = { LHS, RHS }; |
| 5778 | for (unsigned i = 0; i < 2; ++i) { |
| 5779 | Expr *Operand = Operands[i]; |
| 5780 | if ((Operand->getType()->isPointerType() && |
| 5781 | !Operand->getType()->isDependentType()) || |
| 5782 | Operand->getType()->isObjCObjectPointerType()) { |
| 5783 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5784 | if (S.RequireCompleteType( |
| 5785 | Loc, PointeeTy, |
| 5786 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5787 | << PointeeTy << Operand->getSourceRange())) |
| 5788 | return false; |
| 5789 | } |
| 5790 | } |
| 5791 | return true; |
| 5792 | } |
| 5793 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5794 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5795 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 5796 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5797 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5798 | if (CompLHSTy) *CompLHSTy = compType; |
| 5799 | return compType; |
| 5800 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5801 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5802 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5803 | if (lex.isInvalid() || rex.isInvalid()) |
| 5804 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5805 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5806 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5807 | if (lex.get()->getType()->isArithmeticType() && |
| 5808 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5809 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5810 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5811 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5812 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5813 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5814 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5815 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5816 | std::swap(PExp, IExp); |
| 5817 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5818 | if (PExp->getType()->isAnyPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5819 | if (IExp->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5820 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 5821 | return QualType(); |
| 5822 | |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5823 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5824 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5825 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5826 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5827 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5828 | << PointeeTy << PExp->getSourceRange(); |
| 5829 | return QualType(); |
| 5830 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5831 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5832 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5833 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5834 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5835 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5836 | if (LHSTy->isPromotableIntegerType()) |
| 5837 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5838 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5839 | *CompLHSTy = LHSTy; |
| 5840 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5841 | return PExp->getType(); |
| 5842 | } |
| 5843 | } |
| 5844 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5845 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5848 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5849 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5850 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5851 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5852 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5853 | if (CompLHSTy) *CompLHSTy = compType; |
| 5854 | return compType; |
| 5855 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5856 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5857 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5858 | if (lex.isInvalid() || rex.isInvalid()) |
| 5859 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5860 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5861 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5862 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5863 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5864 | if (lex.get()->getType()->isArithmeticType() && |
| 5865 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5866 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5867 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5868 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5869 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5870 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5871 | if (lex.get()->getType()->isAnyPointerType()) { |
| 5872 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5873 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5874 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5875 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5876 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5877 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5878 | return QualType(); |
| 5879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5880 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5881 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5882 | if (rex.get()->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5883 | if (!checkArithmeticOpPointerOperand(*this, Loc, lex.get())) |
| 5884 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5885 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5886 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 5887 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5888 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5889 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5890 | // Handle pointer-pointer subtractions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5891 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 5892 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5893 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5894 | if (getLangOptions().CPlusPlus) { |
| 5895 | // Pointee types must be the same: C++ [expr.add] |
| 5896 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 5897 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5898 | << lex.get()->getType() << rex.get()->getType() |
| 5899 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5900 | return QualType(); |
| 5901 | } |
| 5902 | } else { |
| 5903 | // Pointee types must be compatible C99 6.5.6p3 |
| 5904 | if (!Context.typesAreCompatible( |
| 5905 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 5906 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 5907 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5908 | << lex.get()->getType() << rex.get()->getType() |
| 5909 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5910 | return QualType(); |
| 5911 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5912 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5913 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5914 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
| 5915 | lex.get(), rex.get())) |
| 5916 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5917 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5918 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5919 | return Context.getPointerDiffType(); |
| 5920 | } |
| 5921 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5922 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5923 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5926 | static bool isScopedEnumerationType(QualType T) { |
| 5927 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 5928 | return ET->getDecl()->isScoped(); |
| 5929 | return false; |
| 5930 | } |
| 5931 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5932 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5933 | SourceLocation Loc, unsigned Opc, |
| 5934 | QualType LHSTy) { |
| 5935 | llvm::APSInt Right; |
| 5936 | // Check right/shifter operand |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5937 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5938 | return; |
| 5939 | |
| 5940 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5941 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 5942 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5943 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5944 | return; |
| 5945 | } |
| 5946 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5947 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5948 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5949 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 5950 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5951 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5952 | return; |
| 5953 | } |
| 5954 | if (Opc != BO_Shl) |
| 5955 | return; |
| 5956 | |
| 5957 | // When left shifting an ICE which is signed, we can check for overflow which |
| 5958 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 5959 | // integers have defined behavior modulo one more than the maximum value |
| 5960 | // representable in the result type, so never warn for those. |
| 5961 | llvm::APSInt Left; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5962 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5963 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 5964 | return; |
| 5965 | llvm::APInt ResultBits = |
| 5966 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 5967 | if (LeftBits.uge(ResultBits)) |
| 5968 | return; |
| 5969 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 5970 | Result = Result.shl(Right); |
| 5971 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 5972 | // Print the bit representation of the signed integer as an unsigned |
| 5973 | // hexadecimal number. |
| 5974 | llvm::SmallString<40> HexResult; |
| 5975 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 5976 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5977 | // If we are only missing a sign bit, this is less likely to result in actual |
| 5978 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 5979 | // expected value. Thus we place this behind a different warning that can be |
| 5980 | // turned off separately if needed. |
| 5981 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 5982 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 5983 | << HexResult.str() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5984 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5985 | return; |
| 5986 | } |
| 5987 | |
| 5988 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 5989 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5990 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5993 | // C99 6.5.7 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5994 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5995 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5996 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5997 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 5998 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5999 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6000 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6001 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6002 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6003 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 6004 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6005 | return InvalidOperands(Loc, lex, rex); |
| 6006 | } |
| 6007 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6008 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6009 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6010 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6011 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6012 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6013 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6014 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6015 | // For the LHS, do usual unary conversions, but then reset them away |
| 6016 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6017 | ExprResult old_lex = lex; |
| 6018 | lex = UsualUnaryConversions(lex.take()); |
| 6019 | if (lex.isInvalid()) |
| 6020 | return QualType(); |
| 6021 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6022 | if (isCompAssign) lex = old_lex; |
| 6023 | |
| 6024 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6025 | rex = UsualUnaryConversions(rex.take()); |
| 6026 | if (rex.isInvalid()) |
| 6027 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6028 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6029 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6030 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6031 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6032 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6033 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6036 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6037 | if (DeclContext *DC = D->getDeclContext()) { |
| 6038 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6039 | return true; |
| 6040 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6041 | return FD->isFunctionTemplateSpecialization(); |
| 6042 | } |
| 6043 | return false; |
| 6044 | } |
| 6045 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6046 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6047 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6048 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6049 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6050 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6051 | // Handle vector comparisons separately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6052 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6053 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6054 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6055 | QualType lType = lex.get()->getType(); |
| 6056 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6057 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6058 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 6059 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6060 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6061 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6062 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6063 | |
| 6064 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6065 | // Two different enums will raise a warning when compared. |
| 6066 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 6067 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 6068 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 6069 | RHSEnumType->getDecl()->getIdentifier() && |
| 6070 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 6071 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6072 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6073 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6074 | } |
| 6075 | } |
| 6076 | } |
| 6077 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6078 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6079 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6080 | !lex.get()->getLocStart().isMacroID() && |
| 6081 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6082 | // For non-floating point types, check for self-comparisons of the form |
| 6083 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6084 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6085 | // |
| 6086 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6087 | // expansion. Also don't warn about comparisons which are only self |
| 6088 | // comparisons within a template specialization. The warnings should catch |
| 6089 | // obvious cases in the definition of the template anyways. The idea is to |
| 6090 | // warn when the typed comparison operator will always evaluate to the same |
| 6091 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6092 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6093 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6094 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6095 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6096 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6097 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6098 | << (Opc == BO_EQ |
| 6099 | || Opc == BO_LE |
| 6100 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6101 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6102 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6103 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6104 | // what is it always going to eval to? |
| 6105 | char always_evals_to; |
| 6106 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6107 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6108 | always_evals_to = 0; // false |
| 6109 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6110 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6111 | always_evals_to = 1; // true |
| 6112 | break; |
| 6113 | default: |
| 6114 | // best we can say is 'a constant' |
| 6115 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6116 | break; |
| 6117 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6118 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6119 | << 1 // array |
| 6120 | << always_evals_to); |
| 6121 | } |
| 6122 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6124 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6125 | if (isa<CastExpr>(LHSStripped)) |
| 6126 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6127 | if (isa<CastExpr>(RHSStripped)) |
| 6128 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6129 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6130 | // Warn about comparisons against a string constant (unless the other |
| 6131 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6132 | Expr *literalString = 0; |
| 6133 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6134 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6135 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6136 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6137 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6138 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6139 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6140 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6141 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6142 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6143 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6144 | literalStringStripped = RHSStripped; |
| 6145 | } |
| 6146 | |
| 6147 | if (literalString) { |
| 6148 | std::string resultComparison; |
| 6149 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6150 | case BO_LT: resultComparison = ") < 0"; break; |
| 6151 | case BO_GT: resultComparison = ") > 0"; break; |
| 6152 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6153 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6154 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6155 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6156 | default: assert(false && "Invalid comparison operator"); |
| 6157 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6158 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6159 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6160 | PDiag(diag::warn_stringcompare) |
| 6161 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6162 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6163 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6164 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6165 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6166 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6167 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6168 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6169 | if (lex.isInvalid() || rex.isInvalid()) |
| 6170 | return QualType(); |
| 6171 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6172 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6173 | lex = UsualUnaryConversions(lex.take()); |
| 6174 | if (lex.isInvalid()) |
| 6175 | return QualType(); |
| 6176 | |
| 6177 | rex = UsualUnaryConversions(rex.take()); |
| 6178 | if (rex.isInvalid()) |
| 6179 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6182 | lType = lex.get()->getType(); |
| 6183 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6185 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6186 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6187 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6188 | if (isRelational) { |
| 6189 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6190 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6191 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6192 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6193 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6194 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6195 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6196 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6197 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6198 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6199 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6200 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6201 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6202 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6203 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6205 | // All of the following pointer-related warnings are GCC extensions, except |
| 6206 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6207 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6208 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6209 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6210 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6211 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6213 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6214 | if (LCanPointeeTy == RCanPointeeTy) |
| 6215 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6216 | if (!isRelational && |
| 6217 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6218 | // Valid unless comparison between non-null pointer and function pointer |
| 6219 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6220 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6221 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6222 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6223 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6224 | Diag(Loc, |
| 6225 | isSFINAEContext()? |
| 6226 | diag::err_typecheck_comparison_of_fptr_to_void |
| 6227 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6228 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6229 | |
| 6230 | if (isSFINAEContext()) |
| 6231 | return QualType(); |
| 6232 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6233 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6234 | return ResultTy; |
| 6235 | } |
| 6236 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6237 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6238 | // C++ [expr.rel]p2: |
| 6239 | // [...] Pointer conversions (4.10) and qualification |
| 6240 | // conversions (4.4) are performed on pointer operands (or on |
| 6241 | // a pointer operand and a null pointer constant) to bring |
| 6242 | // them to their composite pointer type. [...] |
| 6243 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6244 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6245 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6246 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6247 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6248 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6249 | if (T.isNull()) { |
| 6250 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6251 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6252 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6253 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6254 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6255 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6256 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6257 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6260 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6261 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6262 | return ResultTy; |
| 6263 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6264 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6265 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6266 | RCanPointeeTy.getUnqualifiedType())) { |
| 6267 | // Valid unless a relational comparison of function pointers |
| 6268 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6269 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6270 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6271 | } |
| 6272 | } else if (!isRelational && |
| 6273 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6274 | // Valid unless comparison between non-null pointer and function pointer |
| 6275 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6276 | && !LHSIsNull && !RHSIsNull) { |
| 6277 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6278 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6279 | } |
| 6280 | } else { |
| 6281 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6282 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6283 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6284 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6285 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6286 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6287 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6288 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6289 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6290 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6291 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6293 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6294 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6295 | // Comparison of nullptr_t with itself. |
| 6296 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6297 | return ResultTy; |
| 6298 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6299 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6300 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6301 | if (RHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6302 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6303 | (!isRelational && |
| 6304 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6305 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6306 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6307 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6308 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6309 | return ResultTy; |
| 6310 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6311 | if (LHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6312 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6313 | (!isRelational && |
| 6314 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6315 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6316 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6317 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6318 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6319 | return ResultTy; |
| 6320 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6321 | |
| 6322 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6323 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6324 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 6325 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | // In addition, pointers to members can be compared, or a pointer to |
| 6327 | // member and a null pointer constant. Pointer to member conversions |
| 6328 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6329 | // them to a common type. If one operand is a null pointer constant, |
| 6330 | // the common type is the type of the other operand. Otherwise, the |
| 6331 | // common type is a pointer to member type similar (4.4) to the type |
| 6332 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6333 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6334 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6335 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6336 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6337 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6338 | if (T.isNull()) { |
| 6339 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6340 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6341 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6342 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6343 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6344 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6345 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6346 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6347 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6348 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6349 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6350 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6351 | return ResultTy; |
| 6352 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6353 | |
| 6354 | // Handle scoped enumeration types specifically, since they don't promote |
| 6355 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6356 | if (lex.get()->getType()->isEnumeralType() && |
| 6357 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6358 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6360 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6361 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6362 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6363 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6364 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6365 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6366 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6367 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6368 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6369 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6370 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6371 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6372 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6373 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6374 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6375 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6376 | if (!isRelational |
| 6377 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6378 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6379 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6380 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6381 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6382 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6383 | ->getPointeeType()->isVoidType()))) |
| 6384 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6385 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6386 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6387 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6388 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6389 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6390 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6391 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6392 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6393 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6394 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 6395 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 6396 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 6397 | if (LPT || RPT) { |
| 6398 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6399 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6400 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6401 | if (!LPtrToVoid && !RPtrToVoid && |
| 6402 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6403 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6404 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6405 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6406 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6407 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6408 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6409 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6410 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6411 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6412 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6413 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6414 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6415 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6416 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6417 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6418 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6419 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6420 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6421 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6422 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6423 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 6424 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6425 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6426 | bool isError = false; |
| 6427 | if ((LHSIsNull && lType->isIntegerType()) || |
| 6428 | (RHSIsNull && rType->isIntegerType())) { |
| 6429 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6430 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6431 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6432 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6433 | else if (getLangOptions().CPlusPlus) { |
| 6434 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6435 | isError = true; |
| 6436 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6437 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6438 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6439 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6440 | Diag(Loc, DiagID) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6441 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6442 | if (isError) |
| 6443 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6444 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6445 | |
| 6446 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6447 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6448 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6449 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6450 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6451 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6452 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6453 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6454 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6455 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6456 | if (!isRelational && RHSIsNull |
| 6457 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6458 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6459 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6460 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6461 | if (!isRelational && LHSIsNull |
| 6462 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6463 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6464 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6465 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6466 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6467 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6468 | } |
| 6469 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6470 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6471 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6472 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6473 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6474 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6475 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6476 | bool isRelational) { |
| 6477 | // Check to make sure we're operating on vectors of the same type and width, |
| 6478 | // Allowing one side to be a scalar of element type. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6479 | QualType vType = CheckVectorOperands(lex, rex, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6480 | if (vType.isNull()) |
| 6481 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6482 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6483 | QualType lType = lex.get()->getType(); |
| 6484 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6485 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6486 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6487 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6488 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6489 | return Context.getLogicalOperationType(); |
| 6490 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6491 | // For non-floating point types, check for self-comparisons of the form |
| 6492 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6493 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6494 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6495 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 6496 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6497 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6498 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6499 | PDiag(diag::warn_comparison_always) |
| 6500 | << 0 // self- |
| 6501 | << 2 // "a constant" |
| 6502 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6503 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6504 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6505 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6506 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 6507 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6508 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6509 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6510 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6511 | // Return the type for the comparison, which is the same as vector type for |
| 6512 | // integer vectors, or an integer type of identical size and number of |
| 6513 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6514 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6515 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6516 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6517 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6518 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6519 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6520 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6521 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6522 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6523 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6524 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6525 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6526 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6527 | } |
| 6528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6529 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6530 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 6531 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 6532 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6533 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6534 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6535 | |
| 6536 | return InvalidOperands(Loc, lex, rex); |
| 6537 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6538 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6539 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 6540 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 6541 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 6542 | return QualType(); |
| 6543 | lex = lexResult.take(); |
| 6544 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6545 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6546 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6547 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6548 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6549 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6553 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6554 | |
| 6555 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6556 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6557 | // is a constant. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6558 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 6559 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 6560 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6561 | !Loc.isMacroID()) { |
| 6562 | // If the RHS can be constant folded, and if it constant folds to something |
| 6563 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6564 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6565 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6566 | Expr::EvalResult Result; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6567 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6568 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 6569 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6570 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 6571 | << rex.get()->getSourceRange() |
| 6572 | << (Opc == BO_LAnd ? "&&" : "||") |
| 6573 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6574 | } |
| 6575 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6576 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6577 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6578 | lex = UsualUnaryConversions(lex.take()); |
| 6579 | if (lex.isInvalid()) |
| 6580 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6581 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6582 | rex = UsualUnaryConversions(rex.take()); |
| 6583 | if (rex.isInvalid()) |
| 6584 | return QualType(); |
| 6585 | |
| 6586 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6587 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6588 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6589 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6590 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6591 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6592 | // The following is safe because we only use this method for |
| 6593 | // non-overloadable operands. |
| 6594 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6595 | // C++ [expr.log.and]p1 |
| 6596 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6597 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6598 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 6599 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6600 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6601 | lex = move(lexRes); |
| 6602 | |
| 6603 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 6604 | if (rexRes.isInvalid()) |
| 6605 | return InvalidOperands(Loc, lex, rex); |
| 6606 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6607 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6608 | // C++ [expr.log.and]p2 |
| 6609 | // C++ [expr.log.or]p2 |
| 6610 | // The result is a bool. |
| 6611 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6612 | } |
| 6613 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6614 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6615 | /// is a read-only property; return true if so. A readonly property expression |
| 6616 | /// depends on various declarations and thus must be treated specially. |
| 6617 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6618 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6619 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6620 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6621 | if (PropExpr->isImplicitProperty()) return false; |
| 6622 | |
| 6623 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6624 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6625 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6626 | PropExpr->getBase()->getType(); |
| 6627 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6628 | if (const ObjCObjectPointerType *OPT = |
| 6629 | BaseType->getAsObjCInterfacePointerType()) |
| 6630 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6631 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6632 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6633 | } |
| 6634 | return false; |
| 6635 | } |
| 6636 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6637 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6638 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6639 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6640 | if (PropExpr->isImplicitProperty()) return false; |
| 6641 | |
| 6642 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6643 | QualType T = PDecl->getType(); |
| 6644 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6645 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6646 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6647 | return CT.isConstQualified(); |
| 6648 | } |
| 6649 | return false; |
| 6650 | } |
| 6651 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6652 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6653 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6654 | return false; |
| 6655 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6656 | NamedDecl *Member = ME->getMemberDecl(); |
| 6657 | if (isa<FieldDecl>(Member)) { |
| 6658 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6659 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 6660 | return false; |
| 6661 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 6662 | } |
| 6663 | return false; |
| 6664 | } |
| 6665 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6666 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6667 | /// emit an error and return true. If so, return false. |
| 6668 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6669 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6671 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6672 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6673 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6674 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6675 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6676 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6677 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6678 | if (IsLV == Expr::MLV_Valid) |
| 6679 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6680 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6681 | unsigned Diag = 0; |
| 6682 | bool NeedType = false; |
| 6683 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6684 | case Expr::MLV_ConstQualified: |
| 6685 | Diag = diag::err_typecheck_assign_const; |
| 6686 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6687 | // In ARC, use some specialized diagnostics for occasions where we |
| 6688 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6689 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6690 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6691 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6692 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6693 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6694 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6695 | // user actually wrote 'const'. |
| 6696 | if (var->isARCPseudoStrong() && |
| 6697 | (!var->getTypeSourceInfo() || |
| 6698 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6699 | // There are two pseudo-strong cases: |
| 6700 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6701 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6702 | if (method && var == method->getSelfDecl()) |
| 6703 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6704 | |
| 6705 | // - fast enumeration variables |
| 6706 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6707 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6708 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6709 | SourceRange Assign; |
| 6710 | if (Loc != OrigLoc) |
| 6711 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6712 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6713 | // We need to preserve the AST regardless, so migration tool |
| 6714 | // can do its job. |
| 6715 | return false; |
| 6716 | } |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6721 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6722 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6723 | NeedType = true; |
| 6724 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6725 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6726 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6727 | NeedType = true; |
| 6728 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6729 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6730 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6731 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6732 | case Expr::MLV_Valid: |
| 6733 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6734 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6735 | case Expr::MLV_MemberFunction: |
| 6736 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6737 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 6738 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6739 | case Expr::MLV_IncompleteType: |
| 6740 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 6741 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6742 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 6743 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6744 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6745 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 6746 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 6747 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6748 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 6749 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 6750 | case Expr::MLV_ReadonlyProperty: |
| 6751 | Diag = diag::error_readonly_property_assignment; |
| 6752 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 6753 | case Expr::MLV_NoSetterProperty: |
| 6754 | Diag = diag::error_nosetter_property_assignment; |
| 6755 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6756 | case Expr::MLV_InvalidMessageExpression: |
| 6757 | Diag = diag::error_readonly_message_assignment; |
| 6758 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 6759 | case Expr::MLV_SubObjCPropertySetting: |
| 6760 | Diag = diag::error_no_subobject_property_setting; |
| 6761 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6762 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6763 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6764 | SourceRange Assign; |
| 6765 | if (Loc != OrigLoc) |
| 6766 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6767 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6768 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6769 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6770 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6771 | return true; |
| 6772 | } |
| 6773 | |
| 6774 | |
| 6775 | |
| 6776 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6777 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6778 | SourceLocation Loc, |
| 6779 | QualType CompoundType) { |
| 6780 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 6781 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6782 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6783 | |
| 6784 | QualType LHSType = LHS->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6785 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6786 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6787 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6788 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6789 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6790 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 6791 | ExprResult LHSResult = Owned(LHS); |
| 6792 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 6793 | if (LHSResult.isInvalid()) |
| 6794 | return QualType(); |
| 6795 | LHS = LHSResult.take(); |
| 6796 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6797 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6798 | if (RHS.isInvalid()) |
| 6799 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6800 | // Special case of NSObject attributes on c-style pointer types. |
| 6801 | if (ConvTy == IncompatiblePointer && |
| 6802 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6803 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6804 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6805 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6806 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6807 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6808 | if (ConvTy == Compatible && |
| 6809 | getLangOptions().ObjCNonFragileABI && |
| 6810 | LHSType->isObjCObjectType()) |
| 6811 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 6812 | << LHSType; |
| 6813 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6814 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 6815 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 6816 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6817 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6818 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 6819 | RHSCheck = ICE->getSubExpr(); |
| 6820 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6821 | if ((UO->getOpcode() == UO_Plus || |
| 6822 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6823 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6824 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6825 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 6826 | // And there is a space or other character before the subexpr of the |
| 6827 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 6828 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 6829 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6830 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6831 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6832 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6833 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6834 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6835 | |
| 6836 | if (ConvTy == Compatible) { |
| 6837 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 6838 | checkRetainCycles(LHS, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6839 | else if (getLangOptions().ObjCAutoRefCount) |
| 6840 | checkUnsafeExprAssigns(Loc, LHS, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6841 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6842 | } else { |
| 6843 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6844 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6845 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6846 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6847 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6848 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6849 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6850 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 6851 | CheckForNullPointerDereference(*this, LHS); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 6852 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 6853 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 6854 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6855 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 6856 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6857 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6858 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 6859 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6860 | // 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] | 6861 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 6862 | return (getLangOptions().CPlusPlus |
| 6863 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6866 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6867 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6868 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6869 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 6870 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 6871 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 6872 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6873 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 6874 | return QualType(); |
| 6875 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 6876 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 6877 | // operands, but not unary promotions. |
| 6878 | // 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] | 6879 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6880 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 6881 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6882 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 6883 | if (LHS.isInvalid()) |
| 6884 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6885 | |
| 6886 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6887 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 6888 | if (RHS.isInvalid()) |
| 6889 | return QualType(); |
| 6890 | if (!RHS.get()->getType()->isVoidType()) |
| 6891 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 6892 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6893 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6894 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6897 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 6898 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6899 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 6900 | ExprValueKind &VK, |
| 6901 | SourceLocation OpLoc, |
| 6902 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6903 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6904 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6905 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6906 | QualType ResType = Op->getType(); |
| 6907 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6908 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6909 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6910 | // Decrement of bool is not allowed. |
| 6911 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6912 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6913 | return QualType(); |
| 6914 | } |
| 6915 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6916 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6917 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6918 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6919 | } else if (ResType->isAnyPointerType()) { |
| 6920 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6921 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6922 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6923 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6924 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6925 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 6926 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6927 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 6928 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 6929 | << PointeeTy << Op->getSourceRange(); |
| 6930 | return QualType(); |
| 6931 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 6932 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6933 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6934 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6935 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 6936 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 6937 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 6938 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6939 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 6940 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 6941 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 6942 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6943 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6944 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 6945 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6946 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6947 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6948 | // 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] | 6949 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6950 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6951 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 6952 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 6953 | // (in C or with postfix), the increment is the unqualified type of the |
| 6954 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6955 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 6956 | VK = VK_LValue; |
| 6957 | return ResType; |
| 6958 | } else { |
| 6959 | VK = VK_RValue; |
| 6960 | return ResType.getUnqualifiedType(); |
| 6961 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6964 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6965 | assert(E->getValueKind() == VK_LValue && |
| 6966 | E->getObjectKind() == OK_ObjCProperty); |
| 6967 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 6968 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 6969 | QualType T = E->getType(); |
| 6970 | QualType ReceiverType; |
| 6971 | if (PRE->isObjectReceiver()) |
| 6972 | ReceiverType = PRE->getBase()->getType(); |
| 6973 | else if (PRE->isSuperReceiver()) |
| 6974 | ReceiverType = PRE->getSuperReceiverType(); |
| 6975 | else |
| 6976 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 6977 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6978 | ExprValueKind VK = VK_RValue; |
| 6979 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 6980 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 6981 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 6982 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 6983 | PRE->isClassReceiver(), |
| 6984 | PRE->isSuperReceiver()); |
| 6985 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 6986 | } |
| 6987 | else { |
| 6988 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 6989 | << PRE->getBase()->getType(); |
| 6990 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6991 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 6992 | |
| 6993 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6994 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 6995 | |
| 6996 | ExprResult Result = MaybeBindToTemporary(E); |
| 6997 | if (!Result.isInvalid()) |
| 6998 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6999 | |
| 7000 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7003 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 7004 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7005 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7006 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7007 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7008 | bool Consumed = false; |
| 7009 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7010 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7011 | // If using property-dot syntax notation for assignment, and there is a |
| 7012 | // setter, RHS expression is being passed to the setter argument. So, |
| 7013 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7014 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7015 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7016 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7017 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7018 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7019 | |
| 7020 | // Otherwise, if the getter returns an l-value, just call that. |
| 7021 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7022 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7023 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7024 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7025 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7026 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7027 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7028 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7029 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7030 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7031 | const ObjCMethodDecl *setter |
| 7032 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7033 | if (setter) { |
| 7034 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7035 | LHSTy = (*P)->getType(); |
| 7036 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7037 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7038 | } |
| 7039 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7040 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7041 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7042 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7043 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7044 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7045 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7046 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7047 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7048 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7049 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7050 | } |
| 7051 | } |
| 7052 | |
| 7053 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7054 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7055 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7056 | /// where the declaration is needed for type checking. We only need to |
| 7057 | /// handle cases when the expression references a function designator |
| 7058 | /// or is an lvalue. Here are some examples: |
| 7059 | /// - &(x) => x |
| 7060 | /// - &*****f => f for f a function designator. |
| 7061 | /// - &s.xx => s |
| 7062 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7063 | /// - *(x + 1) -> x, if x is an array |
| 7064 | /// - &"123"[2] -> 0 |
| 7065 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7066 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7067 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7068 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7069 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7070 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7071 | // If this is an arrow operator, the address is an offset from |
| 7072 | // the base's value, so the object the base refers to is |
| 7073 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7074 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7075 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7076 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7077 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7078 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7079 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7080 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7081 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7082 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7083 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7084 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7085 | } |
| 7086 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7087 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7088 | case Stmt::UnaryOperatorClass: { |
| 7089 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7090 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7091 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7092 | case UO_Real: |
| 7093 | case UO_Imag: |
| 7094 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7095 | return getPrimaryDecl(UO->getSubExpr()); |
| 7096 | default: |
| 7097 | return 0; |
| 7098 | } |
| 7099 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7100 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7101 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7102 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7103 | // If the result of an implicit cast is an l-value, we care about |
| 7104 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7105 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7106 | default: |
| 7107 | return 0; |
| 7108 | } |
| 7109 | } |
| 7110 | |
| 7111 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7112 | /// 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] | 7113 | /// 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] | 7114 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7115 | /// 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] | 7116 | /// 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] | 7117 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7118 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7119 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7120 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7121 | return S.Context.DependentTy; |
| 7122 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7123 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7124 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7125 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7126 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7127 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7128 | << OrigOp->getSourceRange(); |
| 7129 | return QualType(); |
| 7130 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7131 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7132 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7133 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7134 | // Make sure to ignore parentheses in subsequent checks |
| 7135 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7136 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7137 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7138 | // Implement C99-only parts of addressof rules. |
| 7139 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7140 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7141 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7142 | // (assuming the deref expression is valid). |
| 7143 | return uOp->getSubExpr()->getType(); |
| 7144 | } |
| 7145 | // Technically, there should be a check for array subscript |
| 7146 | // expressions here, but the result of one is always an lvalue anyway. |
| 7147 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7148 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7149 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7150 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7151 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7152 | bool sfinae = S.isSFINAEContext(); |
| 7153 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7154 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7155 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7156 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7157 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7158 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7159 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7160 | } else if (lval == Expr::LV_MemberFunction) { |
| 7161 | // If it's an instance method, make a member pointer. |
| 7162 | // The expression must have exactly the form &A::foo. |
| 7163 | |
| 7164 | // If the underlying expression isn't a decl ref, give up. |
| 7165 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7166 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7167 | << OrigOp->getSourceRange(); |
| 7168 | return QualType(); |
| 7169 | } |
| 7170 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7171 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7172 | |
| 7173 | // The id-expression was parenthesized. |
| 7174 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7175 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7176 | << OrigOp->getSourceRange(); |
| 7177 | |
| 7178 | // The method was named without a qualifier. |
| 7179 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7180 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7181 | << op->getSourceRange(); |
| 7182 | } |
| 7183 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7184 | return S.Context.getMemberPointerType(op->getType(), |
| 7185 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7186 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7187 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7188 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7189 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7190 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7191 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7192 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7193 | return QualType(); |
| 7194 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7195 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7196 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7197 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7198 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7199 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7200 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7201 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7202 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7203 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7204 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7205 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7206 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7207 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7208 | << "property expression" << op->getSourceRange(); |
| 7209 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7210 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7211 | // 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] | 7212 | // with the register storage-class specifier. |
| 7213 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7214 | // in C++ it is not error to take address of a register |
| 7215 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7216 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7217 | !S.getLangOptions().CPlusPlus) { |
| 7218 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7219 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7220 | return QualType(); |
| 7221 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7222 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7223 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7224 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7225 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7226 | // Could be a pointer to member, though, if there is an explicit |
| 7227 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7228 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7229 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7230 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7231 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7232 | S.Diag(OpLoc, |
| 7233 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7234 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7235 | return QualType(); |
| 7236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7237 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7238 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7239 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7240 | return S.Context.getMemberPointerType(op->getType(), |
| 7241 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7242 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7243 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7244 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7245 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7246 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7247 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7248 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7249 | // Taking the address of a void variable is technically illegal, but we |
| 7250 | // allow it in cases which are otherwise valid. |
| 7251 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7252 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7255 | // 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] | 7256 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7257 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7258 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7259 | } |
| 7260 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7261 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7262 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7263 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7264 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7265 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7266 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7267 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7268 | if (ConvResult.isInvalid()) |
| 7269 | return QualType(); |
| 7270 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7271 | QualType OpTy = Op->getType(); |
| 7272 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7273 | |
| 7274 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7275 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7276 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7277 | Op->getSourceRange()); |
| 7278 | } |
| 7279 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7280 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7281 | // is an incomplete type or void. It would be possible to warn about |
| 7282 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7283 | // warning is unlikely to catch any mistakes. |
| 7284 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7285 | Result = PT->getPointeeType(); |
| 7286 | else if (const ObjCObjectPointerType *OPT = |
| 7287 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7288 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7289 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7290 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7291 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7292 | if (PR.take() != Op) |
| 7293 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7294 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7295 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7296 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7297 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7298 | << OpTy << Op->getSourceRange(); |
| 7299 | return QualType(); |
| 7300 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7301 | |
| 7302 | // Dereferences are usually l-values... |
| 7303 | VK = VK_LValue; |
| 7304 | |
| 7305 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7306 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7307 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7308 | |
| 7309 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7312 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7313 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7314 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7315 | switch (Kind) { |
| 7316 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7317 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7318 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7319 | case tok::star: Opc = BO_Mul; break; |
| 7320 | case tok::slash: Opc = BO_Div; break; |
| 7321 | case tok::percent: Opc = BO_Rem; break; |
| 7322 | case tok::plus: Opc = BO_Add; break; |
| 7323 | case tok::minus: Opc = BO_Sub; break; |
| 7324 | case tok::lessless: Opc = BO_Shl; break; |
| 7325 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7326 | case tok::lessequal: Opc = BO_LE; break; |
| 7327 | case tok::less: Opc = BO_LT; break; |
| 7328 | case tok::greaterequal: Opc = BO_GE; break; |
| 7329 | case tok::greater: Opc = BO_GT; break; |
| 7330 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7331 | case tok::equalequal: Opc = BO_EQ; break; |
| 7332 | case tok::amp: Opc = BO_And; break; |
| 7333 | case tok::caret: Opc = BO_Xor; break; |
| 7334 | case tok::pipe: Opc = BO_Or; break; |
| 7335 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7336 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7337 | case tok::equal: Opc = BO_Assign; break; |
| 7338 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7339 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7340 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7341 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7342 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7343 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7344 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7345 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7346 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7347 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7348 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7349 | } |
| 7350 | return Opc; |
| 7351 | } |
| 7352 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7353 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7354 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7355 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7356 | switch (Kind) { |
| 7357 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7358 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7359 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7360 | case tok::amp: Opc = UO_AddrOf; break; |
| 7361 | case tok::star: Opc = UO_Deref; break; |
| 7362 | case tok::plus: Opc = UO_Plus; break; |
| 7363 | case tok::minus: Opc = UO_Minus; break; |
| 7364 | case tok::tilde: Opc = UO_Not; break; |
| 7365 | case tok::exclaim: Opc = UO_LNot; break; |
| 7366 | case tok::kw___real: Opc = UO_Real; break; |
| 7367 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7368 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7369 | } |
| 7370 | return Opc; |
| 7371 | } |
| 7372 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7373 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7374 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7375 | /// suppressed in the event of macro expansions. |
| 7376 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7377 | SourceLocation OpLoc) { |
| 7378 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7379 | return; |
| 7380 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7381 | return; |
| 7382 | lhs = lhs->IgnoreParenImpCasts(); |
| 7383 | rhs = rhs->IgnoreParenImpCasts(); |
| 7384 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7385 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7386 | if (!LeftDeclRef || !RightDeclRef || |
| 7387 | LeftDeclRef->getLocation().isMacroID() || |
| 7388 | RightDeclRef->getLocation().isMacroID()) |
| 7389 | return; |
| 7390 | const ValueDecl *LeftDecl = |
| 7391 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7392 | const ValueDecl *RightDecl = |
| 7393 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7394 | if (LeftDecl != RightDecl) |
| 7395 | return; |
| 7396 | if (LeftDecl->getType().isVolatileQualified()) |
| 7397 | return; |
| 7398 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7399 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7400 | return; |
| 7401 | |
| 7402 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7403 | << LeftDeclRef->getType() |
| 7404 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7405 | } |
| 7406 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7407 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7408 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7409 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7410 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7411 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7412 | Expr *lhsExpr, Expr *rhsExpr) { |
| 7413 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7414 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7415 | // The following two variables are used for compound assignment operators |
| 7416 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7417 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7418 | ExprValueKind VK = VK_RValue; |
| 7419 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7420 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7421 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7422 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7423 | // But since an assignment can trigger target based overload, exclude it in |
| 7424 | // our blind search. i.e: |
| 7425 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7426 | // f<int> == 0; // resolve f<int> blindly |
| 7427 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7428 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7429 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7430 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7431 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7432 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7433 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7434 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7435 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Eli Friedman | ed3b256 | 2011-06-17 20:52:22 +0000 | [diff] [blame] | 7438 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 7439 | // but we use a bit of a hack here for speed; this is a relatively |
| 7440 | // hot path, and isNullPointerConstant is slow. |
| 7441 | bool LeftNull = isa<GNUNullExpr>(lhs.get()->IgnoreParenImpCasts()); |
| 7442 | bool RightNull = isa<GNUNullExpr>(rhs.get()->IgnoreParenImpCasts()); |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7443 | |
| 7444 | // Detect when a NULL constant is used improperly in an expression. These |
| 7445 | // are mainly cases where the null pointer is used as an integer instead |
| 7446 | // of a pointer. |
| 7447 | if (LeftNull || RightNull) { |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7448 | // Avoid analyzing cases where the result will either be invalid (and |
| 7449 | // diagnosed as such) or entirely valid and not something to warn about. |
| 7450 | QualType LeftType = lhs.get()->getType(); |
| 7451 | QualType RightType = rhs.get()->getType(); |
| 7452 | if (!LeftType->isBlockPointerType() && !LeftType->isMemberPointerType() && |
| 7453 | !LeftType->isFunctionType() && |
| 7454 | !RightType->isBlockPointerType() && |
| 7455 | !RightType->isMemberPointerType() && |
| 7456 | !RightType->isFunctionType()) { |
| 7457 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 7458 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 7459 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 7460 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 7461 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 7462 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) { |
| 7463 | // These are the operations that would not make sense with a null pointer |
| 7464 | // no matter what the other expression is. |
Chandler Carruth | 2af68e4 | 2011-06-19 09:05:14 +0000 | [diff] [blame] | 7465 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7466 | << (LeftNull ? lhs.get()->getSourceRange() : SourceRange()) |
| 7467 | << (RightNull ? rhs.get()->getSourceRange() : SourceRange()); |
| 7468 | } else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 7469 | Opc == BO_EQ || Opc == BO_NE) { |
| 7470 | // These are the operations that would not make sense with a null pointer |
| 7471 | // if the other expression the other expression is not a pointer. |
| 7472 | if (LeftNull != RightNull && |
| 7473 | !LeftType->isAnyPointerType() && |
| 7474 | !LeftType->canDecayToPointerType() && |
| 7475 | !RightType->isAnyPointerType() && |
| 7476 | !RightType->canDecayToPointerType()) { |
| 7477 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 7478 | << (LeftNull ? lhs.get()->getSourceRange() |
| 7479 | : rhs.get()->getSourceRange()); |
| 7480 | } |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7481 | } |
| 7482 | } |
| 7483 | } |
| 7484 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7485 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7486 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7487 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7488 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7489 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 7490 | VK = lhs.get()->getValueKind(); |
| 7491 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7492 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7493 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7494 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7495 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7496 | case BO_PtrMemD: |
| 7497 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7498 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7499 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7500 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7501 | case BO_Mul: |
| 7502 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7503 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7504 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7505 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7506 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7507 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7508 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7509 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7510 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7511 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7512 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7513 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7514 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7515 | case BO_Shl: |
| 7516 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7517 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7518 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7519 | case BO_LE: |
| 7520 | case BO_LT: |
| 7521 | case BO_GE: |
| 7522 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7523 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7524 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7525 | case BO_EQ: |
| 7526 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7527 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7528 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7529 | case BO_And: |
| 7530 | case BO_Xor: |
| 7531 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7532 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7533 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7534 | case BO_LAnd: |
| 7535 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7536 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7537 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7538 | case BO_MulAssign: |
| 7539 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7540 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7541 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7542 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7543 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7544 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7545 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7546 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7547 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7548 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7549 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7550 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7551 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7552 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7553 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7554 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7555 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7556 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7557 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7558 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7559 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7560 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7561 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7562 | case BO_ShlAssign: |
| 7563 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7564 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7565 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7566 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7567 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7568 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7569 | case BO_AndAssign: |
| 7570 | case BO_XorAssign: |
| 7571 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7572 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 7573 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7574 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7575 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7576 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7577 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7578 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7579 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 7580 | VK = rhs.get()->getValueKind(); |
| 7581 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7582 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7583 | break; |
| 7584 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7585 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7586 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7587 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7588 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 7589 | ResultTy, VK, OK, OpLoc)); |
| 7590 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7591 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7592 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7593 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7594 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 7595 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7596 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7597 | } |
| 7598 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7599 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7600 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7601 | /// comparison operators have higher precedence. The most typical example of |
| 7602 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7603 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7604 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7605 | typedef BinaryOperator BinOp; |
| 7606 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 7607 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 7608 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7609 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7610 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7611 | rhsopc = BO->getOpcode(); |
| 7612 | |
| 7613 | // Subs are not binary operators. |
| 7614 | if (lhsopc == -1 && rhsopc == -1) |
| 7615 | return; |
| 7616 | |
| 7617 | // Bitwise operations are sometimes used as eager logical ops. |
| 7618 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7619 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 7620 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7621 | return; |
| 7622 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7623 | if (BinOp::isComparisonOp(lhsopc)) { |
| 7624 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7625 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 7626 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7627 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7628 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7629 | << BinOp::getOpcodeStr(lhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7630 | lhs->getSourceRange()); |
| 7631 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7632 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 7633 | << BinOp::getOpcodeStr(Opc), |
| 7634 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7635 | } else if (BinOp::isComparisonOp(rhsopc)) { |
| 7636 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7637 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 7638 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7639 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7640 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7641 | << BinOp::getOpcodeStr(rhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7642 | rhs->getSourceRange()); |
| 7643 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7644 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7645 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | b27c7a1 | 2011-06-22 18:41:08 +0000 | [diff] [blame] | 7646 | SourceRange(lhs->getLocStart(), |
| 7647 | cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7648 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7649 | } |
| 7650 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7651 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7652 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7653 | /// in parentheses. |
| 7654 | static void |
| 7655 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7656 | BinaryOperator *Bop) { |
| 7657 | assert(Bop->getOpcode() == BO_And); |
| 7658 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7659 | << Bop->getSourceRange() << OpLoc; |
| 7660 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7661 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7662 | Bop->getSourceRange()); |
| 7663 | } |
| 7664 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7665 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7666 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7667 | /// in parentheses. |
| 7668 | static void |
| 7669 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7670 | BinaryOperator *Bop) { |
| 7671 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7672 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7673 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7674 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7675 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7676 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7680 | /// 'true'. |
| 7681 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7682 | bool Res; |
| 7683 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7684 | } |
| 7685 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7686 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7687 | /// 'false'. |
| 7688 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7689 | bool Res; |
| 7690 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7691 | } |
| 7692 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7693 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7694 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7695 | Expr *OrLHS, Expr *OrRHS) { |
| 7696 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7697 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7698 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 7699 | if (EvaluatesAsFalse(S, OrRHS)) |
| 7700 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7701 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7702 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7703 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7704 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7705 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7706 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7707 | // "a || b && 1", but warn now. |
| 7708 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7709 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7710 | } |
| 7711 | } |
| 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7716 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7717 | Expr *OrLHS, Expr *OrRHS) { |
| 7718 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7719 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7720 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 7721 | if (EvaluatesAsFalse(S, OrLHS)) |
| 7722 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7723 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7724 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7725 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7726 | } |
| 7727 | } |
| 7728 | } |
| 7729 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7730 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 7731 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 7732 | Expr *OrArg) { |
| 7733 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 7734 | if (Bop->getOpcode() == BO_And) |
| 7735 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 7736 | } |
| 7737 | } |
| 7738 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7739 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7740 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7741 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7742 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7743 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7744 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7745 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 7746 | |
| 7747 | // Diagnose "arg1 & arg2 | arg3" |
| 7748 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| 7749 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, lhs); |
| 7750 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, rhs); |
| 7751 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7752 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7753 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7754 | // 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] | 7755 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7756 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 7757 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7758 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7759 | } |
| 7760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7761 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7762 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7763 | tok::TokenKind Kind, |
| 7764 | Expr *lhs, Expr *rhs) { |
| 7765 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 7766 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 7767 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7768 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7769 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 7770 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 7771 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7772 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 7773 | } |
| 7774 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7775 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7776 | BinaryOperatorKind Opc, |
| 7777 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7778 | if (getLangOptions().CPlusPlus) { |
| 7779 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7780 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7781 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 7782 | UseBuiltinOperator = false; |
| 7783 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 7784 | UseBuiltinOperator = true; |
| 7785 | } else { |
| 7786 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 7787 | !rhs->getType()->isOverloadableType(); |
| 7788 | } |
| 7789 | |
| 7790 | if (!UseBuiltinOperator) { |
| 7791 | // Find all of the overloaded operators visible from this |
| 7792 | // point. We perform both an operator-name lookup from the local |
| 7793 | // scope and an argument-dependent lookup based on the types of |
| 7794 | // the arguments. |
| 7795 | UnresolvedSet<16> Functions; |
| 7796 | OverloadedOperatorKind OverOp |
| 7797 | = BinaryOperator::getOverloadedOperator(Opc); |
| 7798 | if (S && OverOp != OO_None) |
| 7799 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 7800 | Functions); |
| 7801 | |
| 7802 | // Build the (potentially-overloaded, potentially-dependent) |
| 7803 | // binary operation. |
| 7804 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 7805 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7806 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7807 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7808 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7809 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7812 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7813 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7814 | Expr *InputExpr) { |
| 7815 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7816 | ExprValueKind VK = VK_RValue; |
| 7817 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7818 | QualType resultType; |
| 7819 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7820 | case UO_PreInc: |
| 7821 | case UO_PreDec: |
| 7822 | case UO_PostInc: |
| 7823 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7824 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7825 | Opc == UO_PreInc || |
| 7826 | Opc == UO_PostInc, |
| 7827 | Opc == UO_PreInc || |
| 7828 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7829 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7830 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7831 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7832 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7833 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7834 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7835 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7836 | Input = move(resolved); |
| 7837 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 7838 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7839 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7840 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7841 | case UO_Plus: |
| 7842 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7843 | Input = UsualUnaryConversions(Input.take()); |
| 7844 | if (Input.isInvalid()) return ExprError(); |
| 7845 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7846 | if (resultType->isDependentType()) |
| 7847 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 7848 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 7849 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7850 | break; |
| 7851 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 7852 | resultType->isEnumeralType()) |
| 7853 | break; |
| 7854 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7855 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7856 | resultType->isPointerType()) |
| 7857 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7858 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7859 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7860 | if (Input.isInvalid()) return ExprError(); |
| 7861 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7862 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7863 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7864 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7865 | << resultType << Input.get()->getSourceRange()); |
| 7866 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7867 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7868 | Input = UsualUnaryConversions(Input.take()); |
| 7869 | if (Input.isInvalid()) return ExprError(); |
| 7870 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7871 | if (resultType->isDependentType()) |
| 7872 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 7873 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 7874 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 7875 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7876 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7877 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7878 | else if (resultType->hasIntegerRepresentation()) |
| 7879 | break; |
| 7880 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7881 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7882 | if (Input.isInvalid()) return ExprError(); |
| 7883 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7884 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7885 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7886 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7887 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7888 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7889 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7890 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7891 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7892 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 7893 | if (Input.isInvalid()) return ExprError(); |
| 7894 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7895 | if (resultType->isDependentType()) |
| 7896 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 7897 | if (resultType->isScalarType()) { |
| 7898 | // C99 6.5.3.3p1: ok, fallthrough; |
| 7899 | if (Context.getLangOptions().CPlusPlus) { |
| 7900 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 7901 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7902 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 7903 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 7904 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7905 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7906 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7907 | if (Input.isInvalid()) return ExprError(); |
| 7908 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7909 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7910 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7911 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7912 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 7913 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7914 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7915 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7916 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7917 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7918 | case UO_Real: |
| 7919 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7920 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7921 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7922 | if (Input.isInvalid()) return ExprError(); |
| 7923 | if (Input.get()->getValueKind() != VK_RValue && |
| 7924 | Input.get()->getObjectKind() == OK_Ordinary) |
| 7925 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 7926 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7927 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7928 | resultType = Input.get()->getType(); |
| 7929 | VK = Input.get()->getValueKind(); |
| 7930 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7931 | break; |
| 7932 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7933 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7934 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7935 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7936 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7937 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7938 | } |
| 7939 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7940 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7941 | UnaryOperatorKind Opc, |
| 7942 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 7943 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 7944 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7945 | // Find all of the overloaded operators visible from this |
| 7946 | // point. We perform both an operator-name lookup from the local |
| 7947 | // scope and an argument-dependent lookup based on the types of |
| 7948 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7949 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7950 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7951 | if (S && OverOp != OO_None) |
| 7952 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 7953 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7954 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7955 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7956 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7957 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7958 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7959 | } |
| 7960 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7961 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7962 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7963 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7964 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 7967 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 7968 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 7969 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 7970 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7971 | // 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] | 7972 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7973 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7974 | } |
| 7975 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7976 | /// Given the last statement in a statement-expression, check whether |
| 7977 | /// the result is a producing expression (like a call to an |
| 7978 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 7979 | /// release out of the full-expression. Otherwise, return null. |
| 7980 | /// Cannot fail. |
| 7981 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 7982 | // Should always be wrapped with one of these. |
| 7983 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 7984 | if (!cleanups) return 0; |
| 7985 | |
| 7986 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 7987 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 7988 | return 0; |
| 7989 | |
| 7990 | // Splice out the cast. This shouldn't modify any interesting |
| 7991 | // features of the statement. |
| 7992 | Expr *producer = cast->getSubExpr(); |
| 7993 | assert(producer->getType() == cast->getType()); |
| 7994 | assert(producer->getValueKind() == cast->getValueKind()); |
| 7995 | cleanups->setSubExpr(producer); |
| 7996 | return cleanups; |
| 7997 | } |
| 7998 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7999 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8000 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8001 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8002 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8003 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8004 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8005 | bool isFileScope |
| 8006 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8007 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8008 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8009 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8010 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8011 | // example, it is not possible to goto into a stmt expression apparently. |
| 8012 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8013 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8014 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8015 | // as the type of the stmtexpr. |
| 8016 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8017 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8018 | if (!Compound->body_empty()) { |
| 8019 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8020 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8021 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8022 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8023 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8024 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8025 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8026 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8027 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8028 | // Do function/array conversion on the last expression, but not |
| 8029 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8030 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8031 | if (LastExpr.isInvalid()) |
| 8032 | return ExprError(); |
| 8033 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8034 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8035 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8036 | // In ARC, if the final expression ends in a consume, splice |
| 8037 | // the consume out and bind it later. In the alternate case |
| 8038 | // (when dealing with a retainable type), the result |
| 8039 | // initialization will create a produce. In both cases the |
| 8040 | // result will be +1, and we'll need to balance that out with |
| 8041 | // a bind. |
| 8042 | if (Expr *rebuiltLastStmt |
| 8043 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8044 | LastExpr = rebuiltLastStmt; |
| 8045 | } else { |
| 8046 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8047 | InitializedEntity::InitializeResult(LPLoc, |
| 8048 | Ty, |
| 8049 | false), |
| 8050 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8051 | LastExpr); |
| 8052 | } |
| 8053 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8054 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8055 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8056 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8057 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8058 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8059 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8060 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8061 | StmtExprMayBindToTemp = true; |
| 8062 | } |
| 8063 | } |
| 8064 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8065 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8066 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8067 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8068 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8069 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8070 | if (StmtExprMayBindToTemp) |
| 8071 | return MaybeBindToTemporary(ResStmtExpr); |
| 8072 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8073 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8074 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8075 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8076 | TypeSourceInfo *TInfo, |
| 8077 | OffsetOfComponent *CompPtr, |
| 8078 | unsigned NumComponents, |
| 8079 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8080 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8081 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8082 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8083 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8084 | // We must have at least one component that refers to the type, and the first |
| 8085 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8086 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8087 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8088 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8089 | << ArgTy << TypeRange); |
| 8090 | |
| 8091 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8092 | // with an incomplete type would be ill-formed. |
| 8093 | if (!Dependent |
| 8094 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8095 | PDiag(diag::err_offsetof_incomplete_type) |
| 8096 | << TypeRange)) |
| 8097 | return ExprError(); |
| 8098 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8099 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8100 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8101 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8102 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8103 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8104 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8105 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8106 | |
| 8107 | bool DidWarnAboutNonPOD = false; |
| 8108 | QualType CurrentType = ArgTy; |
| 8109 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 8110 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 8111 | llvm::SmallVector<Expr*, 4> Exprs; |
| 8112 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8113 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8114 | if (OC.isBrackets) { |
| 8115 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8116 | if (!CurrentType->isDependentType()) { |
| 8117 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8118 | if(!AT) |
| 8119 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8120 | << CurrentType); |
| 8121 | CurrentType = AT->getElementType(); |
| 8122 | } else |
| 8123 | CurrentType = Context.DependentTy; |
| 8124 | |
| 8125 | // The expression must be an integral expression. |
| 8126 | // FIXME: An integral constant expression? |
| 8127 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8128 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8129 | !Idx->getType()->isIntegerType()) |
| 8130 | return ExprError(Diag(Idx->getLocStart(), |
| 8131 | diag::err_typecheck_subscript_not_integer) |
| 8132 | << Idx->getSourceRange()); |
| 8133 | |
| 8134 | // Record this array index. |
| 8135 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8136 | Exprs.push_back(Idx); |
| 8137 | continue; |
| 8138 | } |
| 8139 | |
| 8140 | // Offset of a field. |
| 8141 | if (CurrentType->isDependentType()) { |
| 8142 | // We have the offset of a field, but we can't look into the dependent |
| 8143 | // type. Just record the identifier of the field. |
| 8144 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8145 | CurrentType = Context.DependentTy; |
| 8146 | continue; |
| 8147 | } |
| 8148 | |
| 8149 | // We need to have a complete type to look into. |
| 8150 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8151 | diag::err_offsetof_incomplete_type)) |
| 8152 | return ExprError(); |
| 8153 | |
| 8154 | // Look for the designated field. |
| 8155 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8156 | if (!RC) |
| 8157 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8158 | << CurrentType); |
| 8159 | RecordDecl *RD = RC->getDecl(); |
| 8160 | |
| 8161 | // C++ [lib.support.types]p5: |
| 8162 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8163 | // International Standard. type shall be a POD structure or a POD union |
| 8164 | // (clause 9). |
| 8165 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8166 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8167 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8168 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8169 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8170 | << CurrentType)) |
| 8171 | DidWarnAboutNonPOD = true; |
| 8172 | } |
| 8173 | |
| 8174 | // Look for the field. |
| 8175 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8176 | LookupQualifiedName(R, RD); |
| 8177 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8178 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8179 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8180 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8181 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8182 | } |
| 8183 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8184 | if (!MemberDecl) |
| 8185 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8186 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8187 | OC.LocEnd)); |
| 8188 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8189 | // C99 7.17p3: |
| 8190 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8191 | // |
| 8192 | // We diagnose this as an error. |
| 8193 | if (MemberDecl->getBitWidth()) { |
| 8194 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8195 | << MemberDecl->getDeclName() |
| 8196 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8197 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8198 | return ExprError(); |
| 8199 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8200 | |
| 8201 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8202 | if (IndirectMemberDecl) |
| 8203 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8204 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8205 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8206 | // the base class indirections. |
| 8207 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8208 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8209 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8210 | CXXBasePath &Path = Paths.front(); |
| 8211 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8212 | B != BEnd; ++B) |
| 8213 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8214 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8215 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8216 | if (IndirectMemberDecl) { |
| 8217 | for (IndirectFieldDecl::chain_iterator FI = |
| 8218 | IndirectMemberDecl->chain_begin(), |
| 8219 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8220 | assert(isa<FieldDecl>(*FI)); |
| 8221 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8222 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8223 | } |
| 8224 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8225 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8226 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8227 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8228 | } |
| 8229 | |
| 8230 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8231 | TInfo, Comps.data(), Comps.size(), |
| 8232 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8233 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8234 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8235 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8236 | SourceLocation BuiltinLoc, |
| 8237 | SourceLocation TypeLoc, |
| 8238 | ParsedType argty, |
| 8239 | OffsetOfComponent *CompPtr, |
| 8240 | unsigned NumComponents, |
| 8241 | SourceLocation RPLoc) { |
| 8242 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8243 | TypeSourceInfo *ArgTInfo; |
| 8244 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8245 | if (ArgTy.isNull()) |
| 8246 | return ExprError(); |
| 8247 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8248 | if (!ArgTInfo) |
| 8249 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8250 | |
| 8251 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8252 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8253 | } |
| 8254 | |
| 8255 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8256 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8257 | Expr *CondExpr, |
| 8258 | Expr *LHSExpr, Expr *RHSExpr, |
| 8259 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8260 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8261 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8262 | ExprValueKind VK = VK_RValue; |
| 8263 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8264 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8265 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8266 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8267 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8268 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8269 | } else { |
| 8270 | // The conditional expression is required to be a constant expression. |
| 8271 | llvm::APSInt condEval(32); |
| 8272 | SourceLocation ExpLoc; |
| 8273 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8274 | return ExprError(Diag(ExpLoc, |
| 8275 | diag::err_typecheck_choose_expr_requires_constant) |
| 8276 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8277 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8278 | // 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] | 8279 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8280 | |
| 8281 | resType = ActiveExpr->getType(); |
| 8282 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8283 | VK = ActiveExpr->getValueKind(); |
| 8284 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8287 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8288 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8289 | resType->isDependentType(), |
| 8290 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8291 | } |
| 8292 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8293 | //===----------------------------------------------------------------------===// |
| 8294 | // Clang Extensions. |
| 8295 | //===----------------------------------------------------------------------===// |
| 8296 | |
| 8297 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8298 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8299 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8300 | PushBlockScope(BlockScope, Block); |
| 8301 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8302 | if (BlockScope) |
| 8303 | PushDeclContext(BlockScope, Block); |
| 8304 | else |
| 8305 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8306 | } |
| 8307 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8308 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8309 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8310 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8311 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8312 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8313 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8314 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8315 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8316 | // GetTypeForDeclarator always produces a function type for a block |
| 8317 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8318 | // unless the function was written with a typedef. |
| 8319 | assert(T->isFunctionType() && |
| 8320 | "GetTypeForDeclarator made a non-function block signature"); |
| 8321 | |
| 8322 | // Look for an explicit signature in that function type. |
| 8323 | FunctionProtoTypeLoc ExplicitSignature; |
| 8324 | |
| 8325 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8326 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8327 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8328 | |
| 8329 | // Check whether that explicit signature was synthesized by |
| 8330 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8331 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8332 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8333 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8334 | // This would be much cheaper if we stored TypeLocs instead of |
| 8335 | // TypeSourceInfos. |
| 8336 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8337 | unsigned Size = Result.getFullDataSize(); |
| 8338 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8339 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8340 | |
| 8341 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8342 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8344 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8345 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8346 | CurBlock->FunctionType = T; |
| 8347 | |
| 8348 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8349 | QualType RetTy = Fn->getResultType(); |
| 8350 | bool isVariadic = |
| 8351 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8352 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8353 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8354 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8355 | // Don't allow returning a objc interface by value. |
| 8356 | if (RetTy->isObjCObjectType()) { |
| 8357 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8358 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8359 | return; |
| 8360 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8361 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8362 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8363 | // return type. TODO: what should we do with declarators like: |
| 8364 | // ^ * { ... } |
| 8365 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8366 | if (RetTy != Context.DependentTy) |
| 8367 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8368 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8369 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8370 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8371 | if (ExplicitSignature) { |
| 8372 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8373 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8374 | if (Param->getIdentifier() == 0 && |
| 8375 | !Param->isImplicit() && |
| 8376 | !Param->isInvalidDecl() && |
| 8377 | !getLangOptions().CPlusPlus) |
| 8378 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8379 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8380 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8381 | |
| 8382 | // Fake up parameter variables if we have a typedef, like |
| 8383 | // ^ fntype { ... } |
| 8384 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8385 | for (FunctionProtoType::arg_type_iterator |
| 8386 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8387 | ParmVarDecl *Param = |
| 8388 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8389 | ParamInfo.getSourceRange().getBegin(), |
| 8390 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8391 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8392 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8393 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8394 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8395 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8396 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8397 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8398 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8399 | CurBlock->TheDecl->param_end(), |
| 8400 | /*CheckParameterNames=*/false); |
| 8401 | } |
| 8402 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8403 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8404 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8405 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8406 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8407 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8408 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8409 | // FIXME: remove the attribute. |
| 8410 | } |
| 8411 | |
| 8412 | // Put the parameter variables in scope. We can bail out immediately |
| 8413 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8414 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8415 | return; |
| 8416 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8417 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8418 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8419 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8420 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8421 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8422 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8423 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8424 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8425 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8426 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8427 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
| 8430 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8431 | /// is invoked to pop the information about the block from the action impl. |
| 8432 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8433 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8434 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8435 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8436 | } |
| 8437 | |
| 8438 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8439 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8440 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8441 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8442 | // If blocks are disabled, emit an error. |
| 8443 | if (!LangOpts.Blocks) |
| 8444 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8445 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8446 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8447 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8448 | PopDeclContext(); |
| 8449 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8450 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8451 | if (!BSI->ReturnType.isNull()) |
| 8452 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8453 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8454 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8455 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8456 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8457 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8458 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8459 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8460 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8461 | // If the user wrote a function type in some form, try to use that. |
| 8462 | if (!BSI->FunctionType.isNull()) { |
| 8463 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8464 | |
| 8465 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8466 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8467 | |
| 8468 | // Turn protoless block types into nullary block types. |
| 8469 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8470 | FunctionProtoType::ExtProtoInfo EPI; |
| 8471 | EPI.ExtInfo = Ext; |
| 8472 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8473 | |
| 8474 | // Otherwise, if we don't need to change anything about the function type, |
| 8475 | // preserve its sugar structure. |
| 8476 | } else if (FTy->getResultType() == RetTy && |
| 8477 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8478 | BlockTy = BSI->FunctionType; |
| 8479 | |
| 8480 | // Otherwise, make the minimal modifications to the function type. |
| 8481 | } else { |
| 8482 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8483 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8484 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8485 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8486 | BlockTy = Context.getFunctionType(RetTy, |
| 8487 | FPT->arg_type_begin(), |
| 8488 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8489 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
| 8492 | // If we don't have a function type, just build one from nothing. |
| 8493 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8494 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8495 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8496 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8497 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8498 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8499 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8500 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8501 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8502 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8503 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8504 | if (getCurFunction()->NeedsScopeChecking() && |
| 8505 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8506 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8507 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8508 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8509 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8510 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 8511 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 8512 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8513 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8514 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8515 | } |
| 8516 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8517 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8518 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8519 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8520 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8521 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8522 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8523 | } |
| 8524 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8525 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8526 | Expr *E, TypeSourceInfo *TInfo, |
| 8527 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8528 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8529 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8530 | // Get the va_list type |
| 8531 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8532 | if (VaListType->isArrayType()) { |
| 8533 | // Deal with implicit array decay; for example, on x86-64, |
| 8534 | // va_list is an array, but it's supposed to decay to |
| 8535 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8536 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8537 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8538 | ExprResult Result = UsualUnaryConversions(E); |
| 8539 | if (Result.isInvalid()) |
| 8540 | return ExprError(); |
| 8541 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8542 | } else { |
| 8543 | // Otherwise, the va_list argument must be an l-value because |
| 8544 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8545 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8546 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8547 | return ExprError(); |
| 8548 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8549 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8550 | if (!E->isTypeDependent() && |
| 8551 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8552 | return ExprError(Diag(E->getLocStart(), |
| 8553 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8554 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8555 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8556 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8557 | if (!TInfo->getType()->isDependentType()) { |
| 8558 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8559 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8560 | << TInfo->getTypeLoc().getSourceRange())) |
| 8561 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8562 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8563 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8564 | TInfo->getType(), |
| 8565 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8566 | << TInfo->getTypeLoc().getSourceRange())) |
| 8567 | return ExprError(); |
| 8568 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8569 | if (!TInfo->getType().isPODType(Context)) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8570 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8571 | diag::warn_second_parameter_to_va_arg_not_pod) |
| 8572 | << TInfo->getType() |
| 8573 | << TInfo->getTypeLoc().getSourceRange(); |
| 8574 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8575 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8576 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8577 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8578 | } |
| 8579 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8580 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8581 | // The type of __null will be int or long, depending on the size of |
| 8582 | // pointers on the target. |
| 8583 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8584 | unsigned pw = Context.Target.getPointerWidth(0); |
| 8585 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8586 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8587 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8588 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8589 | else if (pw == Context.Target.getLongLongWidth()) |
| 8590 | Ty = Context.LongLongTy; |
| 8591 | else { |
| 8592 | assert(!"I don't know size of pointer!"); |
| 8593 | Ty = Context.IntTy; |
| 8594 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8595 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8596 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8599 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8600 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8601 | if (!SemaRef.getLangOptions().ObjC1) |
| 8602 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8603 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8604 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8605 | if (!PT) |
| 8606 | return; |
| 8607 | |
| 8608 | // Check if the destination is of type 'id'. |
| 8609 | if (!PT->isObjCIdType()) { |
| 8610 | // Check if the destination is the 'NSString' interface. |
| 8611 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8612 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8613 | return; |
| 8614 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8615 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8616 | // Strip off any parens and casts. |
| 8617 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 8618 | if (!SL || SL->isWide()) |
| 8619 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8620 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8621 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8622 | } |
| 8623 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8624 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8625 | SourceLocation Loc, |
| 8626 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8627 | Expr *SrcExpr, AssignmentAction Action, |
| 8628 | bool *Complained) { |
| 8629 | if (Complained) |
| 8630 | *Complained = false; |
| 8631 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8632 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8633 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8634 | bool isInvalid = false; |
| 8635 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8636 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8637 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8638 | switch (ConvTy) { |
| 8639 | default: assert(0 && "Unknown conversion type"); |
| 8640 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8641 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8642 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 8643 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8644 | case IntToPointer: |
| 8645 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 8646 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8647 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8648 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8649 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8650 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 8651 | SrcType->isObjCObjectPointerType(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8652 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8653 | case IncompatiblePointerSign: |
| 8654 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8655 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8656 | case FunctionVoidPointer: |
| 8657 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8658 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8659 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 8660 | // Perform array-to-pointer decay if necessary. |
| 8661 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 8662 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8663 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 8664 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 8665 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 8666 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 8667 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8668 | |
| 8669 | |
| 8670 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8671 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8672 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 8676 | // fallthrough |
| 8677 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8678 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8679 | // If the qualifiers lost were because we were applying the |
| 8680 | // (deprecated) C++ conversion from a string literal to a char* |
| 8681 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 8682 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8683 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8684 | // bit of refactoring (so that the second argument is an |
| 8685 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8686 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8687 | // C++ semantics. |
| 8688 | if (getLangOptions().CPlusPlus && |
| 8689 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 8690 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8691 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 8692 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 8693 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 8694 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 8695 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8696 | case IntToBlockPointer: |
| 8697 | DiagKind = diag::err_int_to_block_pointer; |
| 8698 | break; |
| 8699 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 8700 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8701 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8702 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8703 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8704 | // it can give a more specific diagnostic. |
| 8705 | DiagKind = diag::warn_incompatible_qualified_id; |
| 8706 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 8707 | case IncompatibleVectors: |
| 8708 | DiagKind = diag::warn_incompatible_vectors; |
| 8709 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame^] | 8710 | case IncompatibleObjCWeakRef: |
| 8711 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 8712 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8713 | case Incompatible: |
| 8714 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 8715 | isInvalid = true; |
| 8716 | break; |
| 8717 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8718 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8719 | QualType FirstType, SecondType; |
| 8720 | switch (Action) { |
| 8721 | case AA_Assigning: |
| 8722 | case AA_Initializing: |
| 8723 | // The destination type comes first. |
| 8724 | FirstType = DstType; |
| 8725 | SecondType = SrcType; |
| 8726 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8727 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8728 | case AA_Returning: |
| 8729 | case AA_Passing: |
| 8730 | case AA_Converting: |
| 8731 | case AA_Sending: |
| 8732 | case AA_Casting: |
| 8733 | // The source type comes first. |
| 8734 | FirstType = SrcType; |
| 8735 | SecondType = DstType; |
| 8736 | break; |
| 8737 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8738 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8739 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8740 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8741 | if (CheckInferredResultType) |
| 8742 | EmitRelatedResultTypeNote(SrcExpr); |
| 8743 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8744 | if (Complained) |
| 8745 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8746 | return isInvalid; |
| 8747 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8748 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 8749 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8750 | llvm::APSInt ICEResult; |
| 8751 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 8752 | if (Result) |
| 8753 | *Result = ICEResult; |
| 8754 | return false; |
| 8755 | } |
| 8756 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8757 | Expr::EvalResult EvalResult; |
| 8758 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8759 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8760 | EvalResult.HasSideEffects) { |
| 8761 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 8762 | |
| 8763 | if (EvalResult.Diag) { |
| 8764 | // We only show the note if it's not the usual "invalid subexpression" |
| 8765 | // or if it's actually in a subexpression. |
| 8766 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 8767 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 8768 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 8769 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8770 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8771 | return true; |
| 8772 | } |
| 8773 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8774 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 8775 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8776 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8777 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8778 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 8779 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8780 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8781 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8782 | if (Result) |
| 8783 | *Result = EvalResult.Val.getInt(); |
| 8784 | return false; |
| 8785 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8786 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8787 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8788 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8789 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8790 | ExpressionEvaluationContextRecord(NewContext, |
| 8791 | ExprTemporaries.size(), |
| 8792 | ExprNeedsCleanups)); |
| 8793 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8796 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8797 | Sema::PopExpressionEvaluationContext() { |
| 8798 | // Pop the current expression evaluation context off the stack. |
| 8799 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 8800 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8801 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 8802 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 8803 | if (Rec.PotentiallyReferenced) { |
| 8804 | // Mark any remaining declarations in the current position of the stack |
| 8805 | // as "referenced". If they were not meant to be referenced, semantic |
| 8806 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8807 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 8808 | I = Rec.PotentiallyReferenced->begin(), |
| 8809 | IEnd = Rec.PotentiallyReferenced->end(); |
| 8810 | I != IEnd; ++I) |
| 8811 | MarkDeclarationReferenced(I->first, I->second); |
| 8812 | } |
| 8813 | |
| 8814 | if (Rec.PotentiallyDiagnosed) { |
| 8815 | // Emit any pending diagnostics. |
| 8816 | for (PotentiallyEmittedDiagnostics::iterator |
| 8817 | I = Rec.PotentiallyDiagnosed->begin(), |
| 8818 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 8819 | I != IEnd; ++I) |
| 8820 | Diag(I->first, I->second); |
| 8821 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8822 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8823 | |
| 8824 | // When are coming out of an unevaluated context, clear out any |
| 8825 | // temporaries that we may have created as part of the evaluation of |
| 8826 | // the expression in that context: they aren't relevant because they |
| 8827 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8828 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8829 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 8830 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8831 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 8832 | |
| 8833 | // Otherwise, merge the contexts together. |
| 8834 | } else { |
| 8835 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 8836 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8837 | |
| 8838 | // Destroy the popped expression evaluation record. |
| 8839 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8840 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8841 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8842 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 8843 | ExprTemporaries.erase( |
| 8844 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 8845 | ExprTemporaries.end()); |
| 8846 | ExprNeedsCleanups = false; |
| 8847 | } |
| 8848 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8849 | /// \brief Note that the given declaration was referenced in the source code. |
| 8850 | /// |
| 8851 | /// This routine should be invoke whenever a given declaration is referenced |
| 8852 | /// in the source code, and where that reference occurred. If this declaration |
| 8853 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 8854 | /// C99 6.9p3), then the declaration will be marked as used. |
| 8855 | /// |
| 8856 | /// \param Loc the location where the declaration was referenced. |
| 8857 | /// |
| 8858 | /// \param D the declaration that has been referenced by the source code. |
| 8859 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 8860 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8861 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 8862 | D->setReferenced(); |
| 8863 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8864 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 8865 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8866 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 8867 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 8868 | // template or not. The reason for this is that unevaluated expressions |
| 8869 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 8870 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8871 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8872 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 8873 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8874 | return; |
| 8875 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8876 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8877 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 8878 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8879 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8880 | // Do not mark anything as "used" within a dependent context; wait for |
| 8881 | // an instantiation. |
| 8882 | if (CurContext->isDependentContext()) |
| 8883 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8884 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8885 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8886 | case Unevaluated: |
| 8887 | // We are in an expression that is not potentially evaluated; do nothing. |
| 8888 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8889 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8890 | case PotentiallyEvaluated: |
| 8891 | // We are in a potentially-evaluated expression, so this declaration is |
| 8892 | // "used"; handle this below. |
| 8893 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8894 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8895 | case PotentiallyPotentiallyEvaluated: |
| 8896 | // We are in an expression that may be potentially evaluated; queue this |
| 8897 | // declaration reference until we know whether the expression is |
| 8898 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8899 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8900 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 8901 | |
| 8902 | case PotentiallyEvaluatedIfUsed: |
| 8903 | // Referenced declarations will only be used if the construct in the |
| 8904 | // containing expression is used. |
| 8905 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8907 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8908 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 8909 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8910 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 8911 | if (Constructor->isTrivial()) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 8912 | return; |
| 8913 | if (!Constructor->isUsed(false)) |
| 8914 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Sean Hunt | 509f048 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 8915 | } else if (Constructor->isDefaulted() && |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8916 | Constructor->isCopyConstructor()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8917 | if (!Constructor->isUsed(false)) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8918 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8919 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8920 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8921 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8922 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8923 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8924 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8925 | if (Destructor->isVirtual()) |
| 8926 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8927 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 8928 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8929 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8930 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 8931 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8932 | } else if (MethodDecl->isVirtual()) |
| 8933 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8934 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 8935 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 8936 | // Recursive functions should be marked when used from another function. |
| 8937 | if (CurContext == Function) return; |
| 8938 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8939 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 8940 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 8941 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8942 | bool AlreadyInstantiated = false; |
| 8943 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 8944 | = Function->getTemplateSpecializationInfo()) { |
| 8945 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 8946 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8947 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 8948 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8949 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8950 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8951 | = Function->getMemberSpecializationInfo()) { |
| 8952 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 8953 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8954 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 8955 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8956 | AlreadyInstantiated = true; |
| 8957 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8958 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 8959 | if (!AlreadyInstantiated) { |
| 8960 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 8961 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 8962 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 8963 | Loc)); |
| 8964 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 8965 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 8966 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 8967 | } else { |
| 8968 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 8969 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 8970 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 8971 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 8972 | MarkDeclarationReferenced(Loc, *i); |
| 8973 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 8974 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8975 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 8976 | // Keep track of used but undefined functions. |
| 8977 | if (!Function->isPure() && !Function->hasBody() && |
| 8978 | Function->getLinkage() != ExternalLinkage) { |
| 8979 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 8980 | if (old.isInvalid()) old = Loc; |
| 8981 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 8982 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 8983 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8984 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 8985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8986 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8987 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 8988 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8989 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8990 | Var->getInstantiatedFromStaticDataMember()) { |
| 8991 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 8992 | assert(MSInfo && "Missing member specialization information?"); |
| 8993 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 8994 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 8995 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 8996 | // This is a modification of an existing AST node. Notify listeners. |
| 8997 | if (ASTMutationListener *L = getASTMutationListener()) |
| 8998 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 8999 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9000 | } |
| 9001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9002 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9003 | // Keep track of used but undefined variables. We make a hole in |
| 9004 | // the warning for static const data members with in-line |
| 9005 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9006 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9007 | && Var->getLinkage() != ExternalLinkage |
| 9008 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9009 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9010 | if (old.isInvalid()) old = Loc; |
| 9011 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9012 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9013 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9014 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9015 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9016 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9017 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9018 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9019 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9020 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9021 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9022 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9023 | Sema &S; |
| 9024 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9025 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9026 | public: |
| 9027 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9028 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9029 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9030 | |
| 9031 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9032 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9033 | }; |
| 9034 | } |
| 9035 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9036 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9037 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9038 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9039 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9040 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9041 | |
| 9042 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9043 | } |
| 9044 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9045 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9046 | if (ClassTemplateSpecializationDecl *Spec |
| 9047 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9048 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9049 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9050 | } |
| 9051 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9052 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9053 | } |
| 9054 | |
| 9055 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9056 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9057 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9058 | } |
| 9059 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9060 | namespace { |
| 9061 | /// \brief Helper class that marks all of the declarations referenced by |
| 9062 | /// potentially-evaluated subexpressions as "referenced". |
| 9063 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9064 | Sema &S; |
| 9065 | |
| 9066 | public: |
| 9067 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9068 | |
| 9069 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9070 | |
| 9071 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9072 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9073 | } |
| 9074 | |
| 9075 | void VisitMemberExpr(MemberExpr *E) { |
| 9076 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9077 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9078 | } |
| 9079 | |
| 9080 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9081 | if (E->getConstructor()) |
| 9082 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9083 | if (E->getOperatorNew()) |
| 9084 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9085 | if (E->getOperatorDelete()) |
| 9086 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9087 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9088 | } |
| 9089 | |
| 9090 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9091 | if (E->getOperatorDelete()) |
| 9092 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9093 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9094 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9095 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9096 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9097 | S.LookupDestructor(Record)); |
| 9098 | } |
| 9099 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9100 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9101 | } |
| 9102 | |
| 9103 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9104 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9105 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9106 | } |
| 9107 | |
| 9108 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9109 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9110 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9111 | |
| 9112 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9113 | Visit(E->getExpr()); |
| 9114 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9115 | }; |
| 9116 | } |
| 9117 | |
| 9118 | /// \brief Mark any declarations that appear within this expression or any |
| 9119 | /// potentially-evaluated subexpressions as "referenced". |
| 9120 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9121 | EvaluatedExprMarker(*this).Visit(E); |
| 9122 | } |
| 9123 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9124 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9125 | /// of the program being compiled. |
| 9126 | /// |
| 9127 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9128 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9129 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9130 | /// expressions, code used only during overload resolution, etc., are not |
| 9131 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9132 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9133 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9134 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9135 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9136 | /// This routine should be used for all diagnostics that describe the run-time |
| 9137 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9138 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9139 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9140 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9141 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9142 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9143 | case Unevaluated: |
| 9144 | // The argument will never be evaluated, so don't complain. |
| 9145 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9146 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9147 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9148 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9149 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9150 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9151 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9152 | } |
| 9153 | else |
| 9154 | Diag(Loc, PD); |
| 9155 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9156 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9157 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9158 | case PotentiallyPotentiallyEvaluated: |
| 9159 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9160 | break; |
| 9161 | } |
| 9162 | |
| 9163 | return false; |
| 9164 | } |
| 9165 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9166 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9167 | CallExpr *CE, FunctionDecl *FD) { |
| 9168 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9169 | return false; |
| 9170 | |
| 9171 | PartialDiagnostic Note = |
| 9172 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9173 | << FD->getDeclName() : PDiag(); |
| 9174 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9175 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9176 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9177 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9178 | PDiag(diag::err_call_function_incomplete_return) |
| 9179 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9180 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9181 | << CE->getSourceRange(), |
| 9182 | std::make_pair(NoteLoc, Note))) |
| 9183 | return true; |
| 9184 | |
| 9185 | return false; |
| 9186 | } |
| 9187 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9188 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9189 | // will prevent this condition from triggering, which is what we want. |
| 9190 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9191 | SourceLocation Loc; |
| 9192 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9193 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9194 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9195 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9196 | if (isa<BinaryOperator>(E)) { |
| 9197 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9198 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9199 | return; |
| 9200 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9201 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9202 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9203 | // Greylist some idioms by putting them into a warning subcategory. |
| 9204 | if (ObjCMessageExpr *ME |
| 9205 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9206 | Selector Sel = ME->getSelector(); |
| 9207 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9208 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9209 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9210 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9211 | |
| 9212 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9213 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9214 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9215 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9216 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9217 | Loc = Op->getOperatorLoc(); |
| 9218 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9219 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9220 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9221 | return; |
| 9222 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9223 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9224 | Loc = Op->getOperatorLoc(); |
| 9225 | } else { |
| 9226 | // Not an assignment. |
| 9227 | return; |
| 9228 | } |
| 9229 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9230 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9231 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9232 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9233 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9234 | Diag(Loc, diag::note_condition_assign_silence) |
| 9235 | << FixItHint::CreateInsertion(Open, "(") |
| 9236 | << FixItHint::CreateInsertion(Close, ")"); |
| 9237 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9238 | if (IsOrAssign) |
| 9239 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9240 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9241 | else |
| 9242 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9243 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9244 | } |
| 9245 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9246 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9247 | /// that the user intended an assignment used as condition. |
| 9248 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9249 | // Don't warn if the parens came from a macro. |
| 9250 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9251 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9252 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9253 | // Don't warn for dependent expressions. |
| 9254 | if (parenE->isTypeDependent()) |
| 9255 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9256 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9257 | Expr *E = parenE->IgnoreParens(); |
| 9258 | |
| 9259 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9260 | if (opE->getOpcode() == BO_EQ && |
| 9261 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9262 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9263 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9264 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9265 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9266 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9267 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9268 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9269 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9270 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9271 | } |
| 9272 | } |
| 9273 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9274 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9275 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9276 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9277 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9278 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9279 | ExprResult result = CheckPlaceholderExpr(E); |
| 9280 | if (result.isInvalid()) return ExprError(); |
| 9281 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9282 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9283 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9284 | if (getLangOptions().CPlusPlus) |
| 9285 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9286 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9287 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9288 | if (ERes.isInvalid()) |
| 9289 | return ExprError(); |
| 9290 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9291 | |
| 9292 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9293 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9294 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9295 | << T << E->getSourceRange(); |
| 9296 | return ExprError(); |
| 9297 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9298 | } |
| 9299 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9300 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9301 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9302 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9303 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9304 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9305 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9306 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9307 | |
| 9308 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9309 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9310 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9311 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9312 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9313 | /// to have an appropriate type. |
| 9314 | struct RebuildUnknownAnyFunction |
| 9315 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9316 | |
| 9317 | Sema &S; |
| 9318 | |
| 9319 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9320 | |
| 9321 | ExprResult VisitStmt(Stmt *S) { |
| 9322 | llvm_unreachable("unexpected statement!"); |
| 9323 | return ExprError(); |
| 9324 | } |
| 9325 | |
| 9326 | ExprResult VisitExpr(Expr *expr) { |
| 9327 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9328 | << expr->getSourceRange(); |
| 9329 | return ExprError(); |
| 9330 | } |
| 9331 | |
| 9332 | /// Rebuild an expression which simply semantically wraps another |
| 9333 | /// expression which it shares the type and value kind of. |
| 9334 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9335 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 9336 | if (subResult.isInvalid()) return ExprError(); |
| 9337 | |
| 9338 | Expr *subExpr = subResult.take(); |
| 9339 | expr->setSubExpr(subExpr); |
| 9340 | expr->setType(subExpr->getType()); |
| 9341 | expr->setValueKind(subExpr->getValueKind()); |
| 9342 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9343 | return expr; |
| 9344 | } |
| 9345 | |
| 9346 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9347 | return rebuildSugarExpr(paren); |
| 9348 | } |
| 9349 | |
| 9350 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9351 | return rebuildSugarExpr(op); |
| 9352 | } |
| 9353 | |
| 9354 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9355 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9356 | if (subResult.isInvalid()) return ExprError(); |
| 9357 | |
| 9358 | Expr *subExpr = subResult.take(); |
| 9359 | op->setSubExpr(subExpr); |
| 9360 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 9361 | assert(op->getValueKind() == VK_RValue); |
| 9362 | assert(op->getObjectKind() == OK_Ordinary); |
| 9363 | return op; |
| 9364 | } |
| 9365 | |
| 9366 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 9367 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 9368 | |
| 9369 | expr->setType(decl->getType()); |
| 9370 | |
| 9371 | assert(expr->getValueKind() == VK_RValue); |
| 9372 | if (S.getLangOptions().CPlusPlus && |
| 9373 | !(isa<CXXMethodDecl>(decl) && |
| 9374 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 9375 | expr->setValueKind(VK_LValue); |
| 9376 | |
| 9377 | return expr; |
| 9378 | } |
| 9379 | |
| 9380 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9381 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9382 | } |
| 9383 | |
| 9384 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 9385 | return resolveDecl(ref, ref->getDecl()); |
| 9386 | } |
| 9387 | }; |
| 9388 | } |
| 9389 | |
| 9390 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9391 | /// to have a function type. |
| 9392 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 9393 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 9394 | if (result.isInvalid()) return ExprError(); |
| 9395 | return S.DefaultFunctionArrayConversion(result.take()); |
| 9396 | } |
| 9397 | |
| 9398 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9399 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9400 | /// into one which resolves the type directly on the referring |
| 9401 | /// expression. Strict preservation of the original source |
| 9402 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9403 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9404 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9405 | |
| 9406 | Sema &S; |
| 9407 | |
| 9408 | /// The current destination type. |
| 9409 | QualType DestType; |
| 9410 | |
| 9411 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 9412 | : S(S), DestType(castType) {} |
| 9413 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9414 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9415 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9416 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9417 | } |
| 9418 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9419 | ExprResult VisitExpr(Expr *expr) { |
| 9420 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9421 | << expr->getSourceRange(); |
| 9422 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9425 | ExprResult VisitCallExpr(CallExpr *call); |
| 9426 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 9427 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9428 | /// Rebuild an expression which simply semantically wraps another |
| 9429 | /// expression which it shares the type and value kind of. |
| 9430 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9431 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9432 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9433 | Expr *subExpr = subResult.take(); |
| 9434 | expr->setSubExpr(subExpr); |
| 9435 | expr->setType(subExpr->getType()); |
| 9436 | expr->setValueKind(subExpr->getValueKind()); |
| 9437 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9438 | return expr; |
| 9439 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9440 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9441 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9442 | return rebuildSugarExpr(paren); |
| 9443 | } |
| 9444 | |
| 9445 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9446 | return rebuildSugarExpr(op); |
| 9447 | } |
| 9448 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9449 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9450 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 9451 | if (!ptr) { |
| 9452 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9453 | << op->getSourceRange(); |
| 9454 | return ExprError(); |
| 9455 | } |
| 9456 | assert(op->getValueKind() == VK_RValue); |
| 9457 | assert(op->getObjectKind() == OK_Ordinary); |
| 9458 | op->setType(DestType); |
| 9459 | |
| 9460 | // Build the sub-expression as if it were an object of the pointee type. |
| 9461 | DestType = ptr->getPointeeType(); |
| 9462 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9463 | if (subResult.isInvalid()) return ExprError(); |
| 9464 | op->setSubExpr(subResult.take()); |
| 9465 | return op; |
| 9466 | } |
| 9467 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9468 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9469 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9470 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9471 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9472 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9473 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9474 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9475 | |
| 9476 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9477 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9478 | } |
| 9479 | }; |
| 9480 | } |
| 9481 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9482 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 9483 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 9484 | Expr *callee = call->getCallee(); |
| 9485 | |
| 9486 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9487 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9488 | FK_FunctionPointer, |
| 9489 | FK_BlockPointer |
| 9490 | }; |
| 9491 | |
| 9492 | FnKind kind; |
| 9493 | QualType type = callee->getType(); |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9494 | if (type == S.Context.BoundMemberTy) { |
| 9495 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 9496 | kind = FK_MemberFunction; |
| 9497 | type = Expr::findBoundMemberType(callee); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9498 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9499 | type = ptr->getPointeeType(); |
| 9500 | kind = FK_FunctionPointer; |
| 9501 | } else { |
| 9502 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 9503 | kind = FK_BlockPointer; |
| 9504 | } |
| 9505 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 9506 | |
| 9507 | // Verify that this is a legal result type of a function. |
| 9508 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9509 | unsigned diagID = diag::err_func_returning_array_function; |
| 9510 | if (kind == FK_BlockPointer) |
| 9511 | diagID = diag::err_block_returning_array_function; |
| 9512 | |
| 9513 | S.Diag(call->getExprLoc(), diagID) |
| 9514 | << DestType->isFunctionType() << DestType; |
| 9515 | return ExprError(); |
| 9516 | } |
| 9517 | |
| 9518 | // Otherwise, go ahead and set DestType as the call's result. |
| 9519 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 9520 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 9521 | assert(call->getObjectKind() == OK_Ordinary); |
| 9522 | |
| 9523 | // Rebuild the function type, replacing the result type with DestType. |
| 9524 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 9525 | DestType = S.Context.getFunctionType(DestType, |
| 9526 | proto->arg_type_begin(), |
| 9527 | proto->getNumArgs(), |
| 9528 | proto->getExtProtoInfo()); |
| 9529 | else |
| 9530 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 9531 | fnType->getExtInfo()); |
| 9532 | |
| 9533 | // Rebuild the appropriate pointer-to-function type. |
| 9534 | switch (kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9535 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9536 | // Nothing to do. |
| 9537 | break; |
| 9538 | |
| 9539 | case FK_FunctionPointer: |
| 9540 | DestType = S.Context.getPointerType(DestType); |
| 9541 | break; |
| 9542 | |
| 9543 | case FK_BlockPointer: |
| 9544 | DestType = S.Context.getBlockPointerType(DestType); |
| 9545 | break; |
| 9546 | } |
| 9547 | |
| 9548 | // Finally, we can recurse. |
| 9549 | ExprResult calleeResult = Visit(callee); |
| 9550 | if (!calleeResult.isUsable()) return ExprError(); |
| 9551 | call->setCallee(calleeResult.take()); |
| 9552 | |
| 9553 | // Bind a temporary if necessary. |
| 9554 | return S.MaybeBindToTemporary(call); |
| 9555 | } |
| 9556 | |
| 9557 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9558 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 9559 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9560 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9561 | // Verify that this is a legal result type of a call. |
| 9562 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9563 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 9564 | << DestType->isFunctionType() << DestType; |
| 9565 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9568 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 9569 | method->setResultType(DestType); |
| 9570 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9571 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9572 | msg->setType(DestType.getNonReferenceType()); |
| 9573 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9574 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9575 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9576 | } |
| 9577 | |
| 9578 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9579 | // 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] | 9580 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9581 | assert(ice->getValueKind() == VK_RValue); |
| 9582 | assert(ice->getObjectKind() == OK_Ordinary); |
| 9583 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9584 | ice->setType(DestType); |
| 9585 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9586 | // Rebuild the sub-expression as the pointee (function) type. |
| 9587 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9588 | |
| 9589 | ExprResult result = Visit(ice->getSubExpr()); |
| 9590 | if (!result.isUsable()) return ExprError(); |
| 9591 | |
| 9592 | ice->setSubExpr(result.take()); |
| 9593 | return S.Owned(ice); |
| 9594 | } |
| 9595 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9596 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9597 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9598 | QualType type = DestType; |
| 9599 | |
| 9600 | // We know how to make this work for certain kinds of decls: |
| 9601 | |
| 9602 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9603 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9604 | // This is true because FunctionDecls must always have function |
| 9605 | // type, so we can't be resolving the entire thing at once. |
| 9606 | assert(type->isFunctionType()); |
| 9607 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9608 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 9609 | if (method->isInstance()) { |
| 9610 | valueKind = VK_RValue; |
| 9611 | type = S.Context.BoundMemberTy; |
| 9612 | } |
| 9613 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9614 | // Function references aren't l-values in C. |
| 9615 | if (!S.getLangOptions().CPlusPlus) |
| 9616 | valueKind = VK_RValue; |
| 9617 | |
| 9618 | // - variables |
| 9619 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9620 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 9621 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9622 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9623 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 9624 | << decl << expr->getSourceRange(); |
| 9625 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
| 9628 | // - nothing else |
| 9629 | } else { |
| 9630 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 9631 | << decl << expr->getSourceRange(); |
| 9632 | return ExprError(); |
| 9633 | } |
| 9634 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9635 | decl->setType(DestType); |
| 9636 | expr->setType(type); |
| 9637 | expr->setValueKind(valueKind); |
| 9638 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9639 | } |
| 9640 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9641 | /// Check a cast of an unknown-any type. We intentionally only |
| 9642 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9643 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 9644 | Expr *castExpr, CastKind &castKind, |
| 9645 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9646 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9647 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 9648 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9649 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9650 | castExpr = result.take(); |
| 9651 | VK = castExpr->getValueKind(); |
| 9652 | castKind = CK_NoOp; |
| 9653 | |
| 9654 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9655 | } |
| 9656 | |
| 9657 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 9658 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9659 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9660 | while (true) { |
| 9661 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9662 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9663 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9664 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9665 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9666 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9667 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9670 | SourceLocation loc; |
| 9671 | NamedDecl *d; |
| 9672 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 9673 | loc = ref->getLocation(); |
| 9674 | d = ref->getDecl(); |
| 9675 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 9676 | loc = mem->getMemberLoc(); |
| 9677 | d = mem->getMemberDecl(); |
| 9678 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 9679 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9680 | loc = msg->getSelectorLoc(); |
| 9681 | d = msg->getMethodDecl(); |
| 9682 | assert(d && "unknown method returning __unknown_any?"); |
| 9683 | } else { |
| 9684 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9685 | << e->getSourceRange(); |
| 9686 | return ExprError(); |
| 9687 | } |
| 9688 | |
| 9689 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9690 | |
| 9691 | // Never recoverable. |
| 9692 | return ExprError(); |
| 9693 | } |
| 9694 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9695 | /// Check for operands with placeholder types and complain if found. |
| 9696 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9697 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9698 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 9699 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9700 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9701 | // Overloaded expressions. |
| 9702 | if (type == Context.OverloadTy) |
| 9703 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9704 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9705 | QualType(), |
| 9706 | diag::err_ovl_unresolvable); |
| 9707 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9708 | // Bound member functions. |
| 9709 | if (type == Context.BoundMemberTy) { |
| 9710 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9711 | << E->getSourceRange(); |
| 9712 | return ExprError(); |
| 9713 | } |
| 9714 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9715 | // Expressions of unknown type. |
| 9716 | if (type == Context.UnknownAnyTy) |
| 9717 | return diagnoseUnknownAnyExpr(*this, E); |
| 9718 | |
| 9719 | assert(!type->isPlaceholderType()); |
| 9720 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9721 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 9722 | |
| 9723 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 9724 | if (expr->isTypeDependent()) |
| 9725 | return true; |
| 9726 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 9727 | return expr->getType()->isIntegralOrEnumerationType(); |
| 9728 | return false; |
| 9729 | } |